tons of changes
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Port.java
index 8a92dd7..b4d1039 100644 (file)
@@ -14,93 +14,39 @@ import java.util.zip.*;
 // Feature: port-level redirects
 public class Port extends Loader {
 
+    final ThreadPool tp;
     final InetAddress bindTo;
     final int port;
 
     private static Hash cache = new Hash();
-    public static Port newPort(String path, InetAddress bindTo, int port) throws IOException {
+    public static Port newPort(String path, InetAddress bindTo, int port, ThreadPool tp) 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));
+        if (p == null) cache.put(canonical, p = new Port(path, bindTo, port, tp));
         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;
-    }
+    private Port(String path, InetAddress bindTo, int port, ThreadPool tp) { 
+        super(path); this.bindTo = bindTo; this.port = port; this.tp = tp; }
 
     public void changed(Watched w) {
         //Log.warn(this, "Port: noticed change in " + w);
         super.changed(w);
     }
 
-
-    Class isListener(String name) throws ClassNotFoundException {
-        final ClassLoader cl = getClassLoader();
-        final Class c = cl.loadClass(name);
-        if (c == null) return null;
-        if (Listener.class.isAssignableFrom(c) && c != Listener.class) return c;
-        return null;
-    }
-
-    Class findListener() throws Exception {
-        getClassLoader();
-        String[] list = list();
-        for(int i=0; i<list.length; i++) {
-            if (!list[i].endsWith(".jar")) continue;
-            //Log.warn(this, "checking " + (this.path + File.separatorChar + list[i]));
-            File f = new File(this.path + File.separatorChar + list[i]);
-            FileInputStream fis = null;
-            try {
-                fis = new FileInputStream(f);
-                ZipInputStream zis = new ZipInputStream(fis);
-                for(ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
-                    String name = ze.getName();
-                    if (name.endsWith(".class")) {
-                        String classname = name.substring(0, name.length() - ".class".length()).replace('/', '.');
-                        Class c = isListener(classname);
-                        if (c != null) return c;
-                    }
-                }
-            } finally { if (fis != null) fis.close(); }
-        }
-        return findListener(new File(getAbsolutePath() + File.separatorChar + "BIN"));
-    }
-
-    Class findListener(File f) throws Exception {
-        if (!f.exists()) return null;
-        if (!f.isDirectory()) {
-            if (!f.getAbsolutePath().endsWith(".class")) return null;
-            String name = f.getAbsolutePath().substring(getAbsolutePath().length() + 5);
-            name = name.substring(0, name.length() - ".class".length()).replace(File.separatorChar, '.');
-            Class c = isListener(name);
-            if (c != null) return c;
-        } else {
-            String[] list = f.list();
-            for(int i=0; i<list.length; i++) {
-                String classname = f.getAbsolutePath() + File.separatorChar + list[i];
-                Class c = findListener(new File(classname));
-                if (c != null) return c;
-            }
-        }
-        return null;
-    }
-
     void dispatch(final Connection conn) throws Exception {
-        new Thread(tg, new Runnable() { public void run() {
+        tp.appendTask(new Runnable() { public void run() {
             String local = conn.getLocalAddress() + ":" + conn.getLocalPort();
             String remote = conn.getRemoteHostname() + ":" + conn.getRemotePort();
             try {
-                Class c = findListener();
+                final ClassLoader cl = getClassLoader();
+                Class c = cl.loadClass("org.ibex.mail.Main");
                 if (c == null) throw new RuntimeException("couldn't find listener");
                 Log.info("["+local+"]", "connection from " + remote + " => " + c.getName());
                 Log.clearnotes();
-                final ClassLoader cl = getClassLoader();
                 Thread.currentThread().setContextClassLoader(cl);
                 Listener l = (Listener)c.newInstance();
                 l.accept(conn);
@@ -113,7 +59,7 @@ public class Port extends Loader {
             } finally {
                 conn.close();
             }
-        } }).start();
+        } });
     }
 
     private class PortThread extends Thread {