allowed classloader-sharing between ports
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Port.java
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()) +