cleanup
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Root.java
index 49bcf39..5dd7168 100644 (file)
@@ -8,64 +8,59 @@ import java.io.*;
 import java.util.*;
 import java.net.*;
 
-public class Root extends Loader {
+public class Root extends Watcher {
 
-    public static String root = System.getProperty("jinetd.root", null);
-
-    private final Host host;
-    private final Watched port;
+    private final PortDir port;
 
     private static final ThreadPool tp = new ThreadPool(10, 100);
 
-    public Root(String path) {
+    public Root(String path) throws IOException {
         super(path);
-        host = new Host(path + File.separatorChar + "host", null);
+        if (!new File(path + File.separatorChar + "host").exists()) new File(path + File.separatorChar + "host").mkdirs();
+        if (!new File(path + File.separatorChar + "port").exists()) new File(path + File.separatorChar + "port").mkdirs();
         port = new PortDir(path + File.separatorChar + "port");
+        watch(path + File.separatorChar + "host");
+        watch(path + File.separatorChar + "port");
+        scan();
     }
 
-    public Watched slash(String part) {
-        if (part.equals("host")) return host;
-        if (part.equals("port")) return port;
-        if (part.equals("LIB")) return super.slash(part);
-        if (part.endsWith(".jar")) return super.slash(part);
-        return null;
-    }
-
-    public static void reboot() {
-        Log.flush();
-        System.exit(0);
-    }
-
-    public void changed(Watched w) {
-        if (w.part.equals("host")) {
-            Log.debug(this, "/host changed");
-        } else if (w.part.equals("port")) {
-            Log.debug(this, "/port changed");
-        } else if (w.getAbsolutePath().endsWith(".jar")) {
+    public void changed(File w) throws IOException {
+        if (w.getName().equals("host"))        Log.debug(this, "/host changed");
+        else if (w.getName().equals("port")) Log.debug(this, "/port changed");
+        /*
+        else if (w.getAbsolutePath().endsWith(".jar")) {
             if (w.lastModifiedAtLastScan != -1) {
                 Log.error(this, "jinetd upgraded; bouncing the JVM....");
-                reboot();
-            }
-        } else {
-            Log.debug(this, "unknown directory " + w.part + " changed");
-        }
+                Main.reboot();
+            } }
+        */
+        else Log.debug(this, "unknown directory " + w.getAbsolutePath() + " changed");
     }
 
-    private static class PortDir extends Watched {
-        public PortDir(String path) { super(path); }
-        public Watched slash(String part) {
+    private static class PortDir extends Watcher {
+        public PortDir(String path) throws IOException {
+            super(path);
+            String[] ports = new File(path).list();
+            for(int i=0; i<ports.length; i++) watch(path + File.separatorChar + ports[i]);
+            scan();
+        }
+
+        // FIXME: need to decommission old ports...
+        public void changed(File f) {
+            String part = f.getAbsolutePath();
+            part = f.getName();
+            // FIXME, use subdirs instead
             String ipaddr  = part.indexOf('_') == -1 ? null : part.substring(0, part.indexOf('_'));
             String portnum = part.indexOf('_') == -1 ? part : part.substring(part.indexOf('_') + 1);
             try {
-                return Port.newPort(this.path + File.separatorChar + part,
-                                    ipaddr == null ? null : InetAddress.getByName(ipaddr),
-                                    portnum.equals("*") ? 0 : Integer.parseInt(portnum),
-                                    tp);
+                Port.newPort(getAbsolutePath() + File.separatorChar + part,
+                             ipaddr == null ? null : InetAddress.getByName(ipaddr),
+                             portnum.equals("*") ? 0 : Integer.parseInt(portnum),
+                             tp);
             } catch (UnknownHostException e) {  Log.warn(this, "can't resolve host for port directory: " + part);
             } catch (NumberFormatException e) { Log.warn(this, "invalid port directory: " + part);
             } catch (Exception e) {             Log.warn(this, "error instantiating Port: " + part);
             }
-            return null;
         }
     }