public String getClassPath() {
String classpath = System.getProperty("java.class.path");
String [] l = new File(Root.root + "/LIB/").list();
- for(int i=0; i<l.length; i++) {
+ for(int i=0; l != null && i<l.length; i++) {
if (!l[i].endsWith(".jar")) continue;
classpath += File.pathSeparatorChar;
classpath += Root.root + "/LIB/" + l[i];
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) {
}
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()) +
String ipaddr = part.indexOf('_') == -1 ? null : part.substring(0, part.indexOf('_'));
String portnum = part.indexOf('_') == -1 ? part : part.substring(part.indexOf('_') + 1);
try {
- return new Port(this.path + File.separatorChar + part,
- ipaddr == null ? null : InetAddress.getByName(ipaddr),
- portnum.equals("*") ? 0 : Integer.parseInt(portnum));
+ return Port.newPort(this.path + File.separatorChar + part,
+ ipaddr == null ? null : InetAddress.getByName(ipaddr),
+ portnum.equals("*") ? 0 : Integer.parseInt(portnum));
} catch (UnknownHostException e) { Log.warn(this, "can't resolve host for port directory: " + part);
} catch (NumberFormatException e) { Log.warn(this, "invalid port directory: " + part);
} catch (Exception e) { Log.warn(this, "error instantiating Port: " + part);