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