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