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