allowed classloader-sharing between ports
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Root.java
1 package org.ibex.jinetd;
2 import org.ibex.util.*;
3 import java.io.*;
4 import java.util.*;
5 import java.net.*;
6
7 public class Root extends Loader {
8
9     public static String root = System.getProperty("jinetd.root", null);
10     private final Host host;
11     private final Watched port;
12
13     public Root(String path) {
14         super(path);
15         host = new Host(path + File.separatorChar + "host", null);
16         port = new PortDir(path + File.separatorChar + "port");
17     }
18
19     public Watched slash(String part) {
20         if (part.equals("host")) return host;
21         if (part.equals("port")) return port;
22         if (part.equals("LIB")) return super.slash(part);
23         if (part.endsWith(".jar")) return super.slash(part);
24         return null;
25     }
26
27     public static void reboot() {
28         Log.flush();
29         System.exit(0);
30     }
31
32     public void changed(Watched w) {
33         if (w.part.equals("host")) {
34             Log.debug(this, "/host changed");
35         } else if (w.part.equals("port")) {
36             Log.debug(this, "/port changed");
37         } else if (w.getAbsolutePath().endsWith(".jar")) {
38             if (w.lastModifiedAtLastScan != -1) {
39                 Log.error(this, "jinetd upgraded; bouncing the JVM....");
40                 reboot();
41             }
42         } else {
43             Log.debug(this, "unknown directory " + w.part + " changed");
44         }
45     }
46
47     private static class PortDir extends Watched {
48         public PortDir(String path) { super(path); }
49         public Watched slash(String part) {
50             String ipaddr  = part.indexOf('_') == -1 ? null : part.substring(0, part.indexOf('_'));
51             String portnum = part.indexOf('_') == -1 ? part : part.substring(part.indexOf('_') + 1);
52             try {
53                 return Port.newPort(this.path + File.separatorChar + part,
54                                     ipaddr == null ? null : InetAddress.getByName(ipaddr),
55                                     portnum.equals("*") ? 0 : Integer.parseInt(portnum));
56             } catch (UnknownHostException e) {  Log.warn(this, "can't resolve host for port directory: " + part);
57             } catch (NumberFormatException e) { Log.warn(this, "invalid port directory: " + part);
58             } catch (Exception e) {             Log.warn(this, "error instantiating Port: " + part);
59             }
60             return null;
61         }
62     }
63
64 }