check for WEB-INF file before creating a host
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Host.java
index 1b10beb..96dcf13 100644 (file)
@@ -6,27 +6,58 @@ package org.ibex.jinetd;
 import org.ibex.util.*;
 import java.io.*;
 import java.util.*;
+import java.net.*;
 import java.lang.reflect.*;
 
-public class Host extends Loader {
+public class Host extends TreeClassLoader {
 
-    final String hostname;
-    public Host(String path, String hostname) { super(path); this.hostname = hostname; }
+    public final String hostname;
+    public final String path;
 
-    public void changed(Watched w) {
-        super.changed(w);
-        /*
-        Log.debug(this, "changed(" + w + ")");
+    private static final Hashtable hosts = new Hashtable();
+    private static boolean initted = false;
+
+    public static Host getHost(String hostname) { return (Host)hosts.get(hostname); }
+    public static Enumeration enumerateHosts() { return hosts.elements(); }
+
+    public Host(String path, String hostname, ClassLoader pcl) {
+        super(new File(path + File.separatorChar + "WEB-INF"), pcl);
+        this.hostname = hostname;
+        this.path = path;
+    }
+
+    public static void init() {
+        if (initted) return;
+        initted = true;
+        init(org.ibex.jinetd.Main.ROOT + "/host", "", 0, Main.getRootClassLoader());
+    }
+    public static void init(String path, String host, int depth, ClassLoader pcl) {
         try {
-            ClassLoader cl = getClassLoader();
-            if (cl == null) return;
-            Class c = cl.loadClass("Main");
-            if (c == null) return;
-            Method m = c.getMethod("main", new Class[] { });
-            m.invoke(null, new Object[] { });
-        } catch (Exception e) {
-            Log.warn(this, e);
+            InetAddress addr = InetAddress.getByName(host);
+            /*
+            boolean good = false;
+            try {
+                if (NetworkInterface.getByInetAddress(addr) != null) good = true;
+                if (!good) Log.warn(Host.class, "host " + host + "resolves to " + addr + " which is not local");
+            } catch (Exception e) {
+                Log.warn(Host.class, "host " + host + "resolves to " + addr + " which is not local because:");
+                Log.warn(Host.class, e);
+            }
+            if (!good) return;
+            */
+            if (new File(path + File.separatorChar + "WEB-INF").exists()) {
+                String pad = "";
+                while(pad.length() + host.length() < 30) pad += " ";
+                Log.info(Main.class, pad + host + " => " + path);
+                hosts.put(host, pcl = new Host(path, host, pcl));
+            }
+        } catch (UnknownHostException e) {
+            if (depth >= 3) return;
         }
-        */
+        String[] subdirs = new File(path).list();
+        if (subdirs != null)
+            for(int i=0; i<subdirs.length; i++)
+                init(path + File.separatorChar + subdirs[i], host.equals("") ? subdirs[i] : subdirs[i]+"."+host, depth+1, pcl);
     }
+
 }