1 package org.ibex.jinetd;
2 import org.ibex.util.*;
7 import java.util.zip.*;
9 public class Port extends Loader {
11 private final InetAddress bindTo;
12 private final int port;
13 private final Thread listener;
15 public Port(String path, InetAddress bindTo, int port) {
19 this.listener = new PortThread();
23 public void changed(Watched w) {
24 //Log.warn(this, "Port: noticed change in " + w);
28 void dispatch(final Connection conn) throws Exception {
30 String[] list = list();
31 for(int i=0; i<list.length; i++) {
32 if (!list[i].endsWith(".jar")) continue;
33 //Log.warn(this, "checking " + (this.path + File.separatorChar + list[i]));
34 File f = new File(this.path + File.separatorChar + list[i]);
35 ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
36 for(ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
37 String name = ze.getName();
38 if (name.endsWith(".class"))
39 dispatch(conn, name.substring(0, name.length() - ".class".length()).replace('/', '.'));
42 check(conn, new File(getAbsolutePath() + File.separatorChar + "BIN"));
45 void check(Connection conn, File f) throws Exception {
46 //Log.warn(this, "check(" + f.getAbsolutePath() + ")");
47 if (!f.exists()) return;
48 if (!f.isDirectory()) {
49 if (!f.getAbsolutePath().endsWith(".class")) return;
50 String name = f.getAbsolutePath().substring(getAbsolutePath().length() + 5);
51 name = name.substring(0, name.length() - ".class".length()).replace(File.separatorChar, '.');
54 String[] list = f.list();
55 for(int i=0; i<list.length; i++) check(conn, new File(f.getAbsolutePath() + File.separatorChar + list[i]));
58 void dispatch(final Connection conn, String name) throws Exception {
59 //Log.info(this, "attempting class " + name);
61 Class c = getClassLoader().loadClass(name);
63 if (Listener.class.isAssignableFrom(c)) {
64 Log.error(this, "dispatching connection on port " + port + " to " +
66 final Listener l = (Listener)c.newInstance();
67 new Thread() { public void run() {
71 } catch (Exception e) {
72 Log.error(l.getClass(), "Listener threw exception");
73 Log.error(l.getClass(), e);
81 } catch (Exception e) {
86 private class PortThread extends Thread {
89 Log.warn(this, "Now listening on address " + bindTo + ", port " + port);
90 ServerSocket ss = bindTo == null ? new ServerSocket(port) : new ServerSocket(port, 0, bindTo);
91 for(Socket s = ss.accept(); ; s = ss.accept()) try {
92 Log.warn(this, "accepted connection on port " + port);
93 dispatch(new Connection(s, "megacz.com"));
94 Log.warn(this, "done searching for service on port " + port);
95 } catch (Exception e) { Log.warn(Port.class, e); }
96 } catch (Exception e) { Log.error(Port.class, e);
97 } catch (Throwable t) {
98 Log.error(this, "serious error, aborting VM");