d745222a367109f16367001898ee9f4b9d92d8b0
[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             Log.warn(Host.class, "resolving " + host);
38             InetAddress addr = InetAddress.getByName(host);
39
40             /*
41             boolean good = false;
42             try {
43                 if (NetworkInterface.getByInetAddress(addr) != null) good = true;
44                 if (!good) Log.warn(Host.class, "host " + host + "resolves to " + addr + " which is not local");
45             } catch (Exception e) {
46                 Log.warn(Host.class, "host " + host + "resolves to " + addr + " which is not local because:");
47                 Log.warn(Host.class, e);
48             }
49             if (!good) return;
50             */        
51
52             if (new File(path + File.separatorChar + "WEB-INF").exists()) {
53                 String pad = "";
54                 while(pad.length() + host.length() < 30) pad += " ";
55                 Log.info(Main.class, pad + host + " => " + path);
56                 hosts.put(host, pcl = new Host(path, host, pcl));
57                 if (!"ibex.org".equals(host)) return;
58             }
59         } catch (UnknownHostException e) {
60             if (depth >= 3) return;
61         }
62         String[] subdirs = new File(path).list();
63         if (subdirs != null)
64             for(int i=0; i<subdirs.length; i++)
65                 init(path + File.separatorChar + subdirs[i], host.equals("") ? subdirs[i] : subdirs[i]+"."+host, depth+1, pcl);
66     }
67
68 }