resolve
[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 ia = InetAddress.getByName(host);
39             String pad = "";
40             while(pad.length() + host.length() < 30) pad += " ";
41             if (ia.equals(InetAddress.getLocalHost())) {
42                 Log.info(Main.class, pad + host + " => " + path);
43                 hosts.put(host, pcl = new Host(path, host, pcl));
44             }
45         } catch (UnknownHostException e) {
46             if (depth >= 3) return;
47         }
48         String[] subdirs = new File(path).list();
49         if (subdirs != null)
50             for(int i=0; i<subdirs.length; i++)
51                 init(path + File.separatorChar + subdirs[i], host.equals("") ? subdirs[i] : subdirs[i]+"."+host, depth+1, pcl);
52     }
53
54 }