super.changed(w);
}
- void dispatch(final Connection conn) throws Exception {
+ boolean dispatch(final Connection conn) 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]);
- ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
- for(ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
- String name = ze.getName();
- if (name.endsWith(".class"))
- dispatch(conn, name.substring(0, name.length() - ".class".length()).replace('/', '.'));
- }
+ 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"))
+ if (dispatch(conn, name.substring(0, name.length() - ".class".length()).replace('/', '.')))
+ return true;
+ }
+ } finally { if (fis != null) fis.close(); }
}
- check(conn, new File(getAbsolutePath() + File.separatorChar + "BIN"));
+ if (check(conn, new File(getAbsolutePath() + File.separatorChar + "BIN"))) return true;
+ return false;
}
- void check(Connection conn, File f) throws Exception {
- //Log.warn(this, "check(" + f.getAbsolutePath() + ")");
- if (!f.exists()) return;
+ boolean check(Connection conn, File f) throws Exception {
+ if (!f.exists()) return false;
if (!f.isDirectory()) {
- if (!f.getAbsolutePath().endsWith(".class")) return;
+ if (!f.getAbsolutePath().endsWith(".class")) return false;
String name = f.getAbsolutePath().substring(getAbsolutePath().length() + 5);
name = name.substring(0, name.length() - ".class".length()).replace(File.separatorChar, '.');
- dispatch(conn, name);
+ if (dispatch(conn, name)) return true;
} else {
String[] list = f.list();
- for(int i=0; i<list.length; i++) check(conn, new File(f.getAbsolutePath() + File.separatorChar + list[i]));
+ for(int i=0; i<list.length; i++)
+ if (check(conn, new File(f.getAbsolutePath() + File.separatorChar + list[i])))
+ return true;
}
+ return false;
}
- void dispatch(final Connection conn, String name) throws Exception {
- //Log.info(this, "attempting class " + name);
+
+ boolean dispatch(final Connection conn, String name) throws Exception {
try {
- Class c = getClassLoader().loadClass(name);
- if (c != null) {
- if (Listener.class.isAssignableFrom(c)) {
- Log.error(this, "dispatching connection on port " + port + " to " +
- c.getName());
- final Listener l = (Listener)c.newInstance();
- new Thread() { public void run() {
- Log.clearnotes();
- try {
- l.accept(conn);
- } catch (Exception e) {
- Log.error(l.getClass(), "Listener threw exception");
- Log.error(l.getClass(), e);
- } finally {
- conn.close();
- }
- } }.start();
- return;
+ final ClassLoader cl = getClassLoader();
+ final Class c = cl.loadClass(name);
+ if (c == null) return false;
+ if (!(Listener.class.isAssignableFrom(c) && c != Listener.class)) return false;
+ Log.info(this, "dispatching connection on port " + port + " to " + c.getName());
+ new Thread() { public void run() {
+ Log.clearnotes();
+ Thread.currentThread().setContextClassLoader(cl);
+ try {
+ Listener l = (Listener)c.newInstance();
+ l.accept(conn);
+ } catch (Exception e) {
+ Log.error(c, "Listener threw exception");
+ Log.error(c, e);
+ } finally {
+ conn.close();
}
- }
- } catch (Exception e) {
- Log.error(this, e);
- }
+ } }.start();
+ return true;
+ } catch (Exception e) { Log.error(this, e); }
+ return false;
}
private class PortThread extends Thread {
public void run() {
try {
- Log.warn(this, "Now listening on address " + bindTo + ", port " + port);
+ Log.warn(this, "Now listening on address " + (bindTo == null ? "all interfaces" : bindTo.toString()) +
+ ", port " + port);
ServerSocket ss = bindTo == null ? new ServerSocket(port) : new ServerSocket(port, 0, bindTo);
for(Socket s = ss.accept(); ; s = ss.accept()) try {
- Log.warn(this, "accepted connection on port " + port);
- dispatch(new Connection(s, "megacz.com"));
- Log.warn(this, "done searching for service on port " + port);
+ if (!dispatch(new Connection(s, "megacz.com"))) {
+ Log.warn(this, "no handler for connection on port " + port);
+ s.close();
+ }
} catch (Exception e) { Log.warn(Port.class, e); }
} catch (Exception e) { Log.error(Port.class, e);
} catch (Throwable t) {