hackage
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Host.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.jinetd;
6 import org.ibex.util.*;
7 import java.io.*;
8 import java.util.*;
9 import java.net.*;
10 import java.lang.reflect.*;
11
12 public class Host extends TreeClassLoader {
13
14     public final String hostname;
15     public final String path;
16
17     private static final Hashtable hosts = new Hashtable();
18     private static boolean initted = false;
19
20     public static Host getHost(String hostname) { return (Host)hosts.get(hostname); }
21     public static Enumeration enumerateHosts() { return hosts.elements(); }
22
23     public Host(String path, String hostname, ClassLoader pcl) {
24         super(new File(path + File.separatorChar + "WEB-INF"), pcl);
25         this.hostname = hostname;
26         this.path = path;
27     }
28
29     public static void init() {
30         if (initted) return;
31         initted = true;
32         init(org.ibex.jinetd.Main.ROOT + "/host", "", 0, Main.getRootClassLoader());
33     }
34     public static void init(String path, String host, int depth, ClassLoader pcl) {
35         if (!new File(path).exists() || !new File(path).isDirectory()) return;
36         try {
37             InetAddress.getByName(host);
38             String pad = "";
39             while(pad.length() + host.length() < 30) pad += " ";
40             Log.info(Main.class, pad + host + " => " + path);
41             hosts.put(host, pcl = new Host(path, host, pcl));
42         } catch (UnknownHostException e) {
43             if (depth >= 3) return;
44         }
45         String[] subdirs = new File(path).list();
46         if (subdirs != null)
47             for(int i=0; i<subdirs.length; i++)
48                 init(path + File.separatorChar + subdirs[i], host.equals("") ? subdirs[i] : subdirs[i]+"."+host, depth+1, pcl);
49     }
50
51 }