allowed classloader-sharing between ports
authoradam <adam@megacz.com>
Fri, 3 Sep 2004 00:24:33 +0000 (00:24 +0000)
committeradam <adam@megacz.com>
Fri, 3 Sep 2004 00:24:33 +0000 (00:24 +0000)
darcs-hash:20040903002433-5007d-b5655e074a278a466b5bbf8ebdddfb2ac06da685.gz

src/org/ibex/jinetd/Loader.java
src/org/ibex/jinetd/Port.java
src/org/ibex/jinetd/Root.java
www/index.html

index 96093bd..8f9fef8 100644 (file)
@@ -59,7 +59,7 @@ public class Loader extends Watcher {
     public String getClassPath() {
         String classpath = System.getProperty("java.class.path");
         String [] l = new File(Root.root + "/LIB/").list();
-        for(int i=0; i<l.length; i++) {
+        for(int i=0; l != null && i<l.length; i++) {
             if (!l[i].endsWith(".jar")) continue;
             classpath += File.pathSeparatorChar;
             classpath += Root.root + "/LIB/" + l[i];
index d7f850d..470c0a0 100644 (file)
@@ -8,16 +8,24 @@ import java.util.zip.*;
 
 public class Port extends Loader {
 
-    private final InetAddress bindTo;
-    private final int port;
-    private final Thread listener;
+    final InetAddress bindTo;
+    final int port;
 
-    public Port(String path, InetAddress bindTo, int port) {
+    private static Hash cache = new Hash();
+    public static Port newPort(String path, InetAddress bindTo, int port) throws IOException {
+        String canonical = new File(path).getCanonicalPath();
+        Port p = (Port)cache.get(canonical);
+        if (p == null) cache.put(canonical, p = new Port(path, bindTo, port));
+        else Log.warn(Port.class, "   sharing " + bindTo+":"+port+" -> "+ (p.bindTo+":"+p.port));
+        p.spawn(bindTo, port);
+        return p;
+    }
+
+    void spawn(InetAddress bindTo, int port) { new PortThread(bindTo, port).start(); }
+    private Port(String path, InetAddress bindTo, int port) {
         super(path);
         this.bindTo = bindTo;
         this.port = port;
-        this.listener = new PortThread();
-        listener.start();
     }
 
     public void changed(Watched w) {
@@ -90,6 +98,9 @@ public class Port extends Loader {
     }
 
     private class PortThread extends Thread {
+        InetAddress bindTo;
+        int port;
+        public PortThread(InetAddress bindTo, int port) { this.bindTo = bindTo; this.port = port; }
         public void run() {
             try {
                 Log.warn(this, "Now listening on address " + (bindTo == null ? "all interfaces" : bindTo.toString()) +
index 9c2fa93..078ec05 100644 (file)
@@ -50,9 +50,9 @@ public class Root extends Loader {
             String ipaddr  = part.indexOf('_') == -1 ? null : part.substring(0, part.indexOf('_'));
             String portnum = part.indexOf('_') == -1 ? part : part.substring(part.indexOf('_') + 1);
             try {
-                return new Port(this.path + File.separatorChar + part,
-                                ipaddr == null ? null : InetAddress.getByName(ipaddr),
-                                portnum.equals("*") ? 0 : Integer.parseInt(portnum));
+                return Port.newPort(this.path + File.separatorChar + part,
+                                    ipaddr == null ? null : InetAddress.getByName(ipaddr),
+                                    portnum.equals("*") ? 0 : Integer.parseInt(portnum));
             } 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);
index 943fbe9..2f99367 100644 (file)
@@ -79,4 +79,4 @@ different network protocol handlers to share the following facilities:
 </ul>
 
 </body>
-</html>
\ No newline at end of file
+</html>