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