1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
5 package org.ibex.jinetd;
6 import org.ibex.util.*;
11 import java.util.zip.*;
13 public class Port extends Loader {
15 final InetAddress bindTo;
18 private static Hash cache = new Hash();
19 public static Port newPort(String path, InetAddress bindTo, int port) throws IOException {
20 String canonical = new File(path).getCanonicalPath();
21 Port p = (Port)cache.get(canonical);
22 if (p == null) cache.put(canonical, p = new Port(path, bindTo, port));
23 else Log.warn(Port.class, " sharing " + bindTo+":"+port+" -> "+ (p.bindTo+":"+p.port));
24 p.spawn(bindTo, port);
28 void spawn(InetAddress bindTo, int port) { new PortThread(bindTo, port).start(); }
29 private Port(String path, InetAddress bindTo, int port) {
35 public void changed(Watched w) {
36 //Log.warn(this, "Port: noticed change in " + w);
41 Class isListener(String name) throws ClassNotFoundException {
42 final ClassLoader cl = getClassLoader();
43 final Class c = cl.loadClass(name);
44 if (c == null) return null;
45 if (Listener.class.isAssignableFrom(c) && c != Listener.class) return c;
49 Class findListener() throws Exception {
51 String[] list = list();
52 for(int i=0; i<list.length; i++) {
53 if (!list[i].endsWith(".jar")) continue;
54 //Log.warn(this, "checking " + (this.path + File.separatorChar + list[i]));
55 File f = new File(this.path + File.separatorChar + list[i]);
56 FileInputStream fis = null;
58 fis = new FileInputStream(f);
59 ZipInputStream zis = new ZipInputStream(fis);
60 for(ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
61 String name = ze.getName();
62 if (name.endsWith(".class")) {
63 String classname = name.substring(0, name.length() - ".class".length()).replace('/', '.');
64 Class c = isListener(classname);
65 if (c != null) return c;
68 } finally { if (fis != null) fis.close(); }
70 return findListener(new File(getAbsolutePath() + File.separatorChar + "BIN"));
73 Class findListener(File f) throws Exception {
74 if (!f.exists()) return null;
75 if (!f.isDirectory()) {
76 if (!f.getAbsolutePath().endsWith(".class")) return null;
77 String name = f.getAbsolutePath().substring(getAbsolutePath().length() + 5);
78 name = name.substring(0, name.length() - ".class".length()).replace(File.separatorChar, '.');
79 Class c = isListener(name);
80 if (c != null) return c;
82 String[] list = f.list();
83 for(int i=0; i<list.length; i++) {
84 String classname = f.getAbsolutePath() + File.separatorChar + list[i];
85 Class c = findListener(new File(classname));
86 if (c != null) return c;
92 void dispatch(final Connection conn) throws Exception {
93 new Thread(tg, new Runnable() { public void run() {
94 String local = conn.getLocalAddress() + ":" + conn.getLocalPort();
95 String remote = conn.getRemoteHostname() + ":" + conn.getRemotePort();
97 Class c = findListener();
98 if (c == null) throw new RuntimeException("couldn't find listener");
99 Log.info("["+local+"]", "connection from " + remote + " => " + c.getName());
101 final ClassLoader cl = getClassLoader();
102 Thread.currentThread().setContextClassLoader(cl);
103 Listener l = (Listener)c.newInstance();
105 } catch (org.ibex.io.Stream.EOF eof) {
106 Log.warn(this, "end of stream reached handling connection from " +
107 conn.getRemoteHostname() + ":" + conn.getRemotePort());
108 } catch (Exception e) {
117 private class PortThread extends Thread {
120 public PortThread(InetAddress bindTo, int port) { this.bindTo = bindTo; this.port = port; }
123 Log.warn(this, "Now listening on address " + (bindTo == null ? "all interfaces" : bindTo.toString()) +
125 ServerSocket ss = bindTo == null ? new ServerSocket(port) : new ServerSocket(port, 0, bindTo);
126 for(Socket s = ss.accept(); ; s = ss.accept()) try {
127 dispatch(new Connection(s, "megacz.com"));
128 } catch (Exception e) { Log.warn(Port.class, e); }
129 } catch (Exception e) { Log.error(Port.class, e);
130 } catch (Throwable t) {
131 Log.error(this, "serious error, aborting VM");