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