d8ccc367d9c3bddf84812f06414444a3c543d972
[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", "/jinetd");
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         return null;
24     }
25
26     public static void reboot() {
27         Log.flush();
28         System.exit(0);
29     }
30
31     public void changed(Watched w) {
32         if (w.part.equals("host")) {
33             Log.debug(this, "/host changed");
34         } else if (w.part.equals("port")) {
35             Log.debug(this, "/port changed");
36         } else if (w.getAbsolutePath().startsWith(Root.root + "/LIB/")) {
37             if (w.lastModifiedAtLastScan != -1) {
38                 Log.error(this, "jinetd upgraded; bouncing the JVM....");
39                 reboot();
40             }
41         } else {
42             Log.debug(this, "unknown directory " + w.part + " changed");
43         }
44     }
45
46     private static class PortDir extends Watched {
47         public PortDir(String path) { super(path); }
48         public Watched slash(String part) {
49             String ipaddr  = part.indexOf('_') == -1 ? null : part.substring(0, part.indexOf('_'));
50             String portnum = part.indexOf('_') == -1 ? part : part.substring(part.indexOf('_') + 1);
51             try {
52                 return new Port(this.path + File.separatorChar + part,
53                                 ipaddr == null ? null : InetAddress.getByName(ipaddr),
54                                 portnum.equals("*") ? 0 : Integer.parseInt(portnum));
55             } catch (UnknownHostException e) {  Log.warn(this, "can't resolve host for port directory: " + part);
56             } catch (NumberFormatException e) { Log.warn(this, "invalid port directory: " + part);
57             } catch (Exception e) {             Log.warn(this, "error instantiating Port: " + part);
58             }
59             return null;
60         }
61     }
62
63 }