cleanup
[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 Watcher {
12
13     private final PortDir port;
14
15     private static final ThreadPool tp = new ThreadPool(10, 100);
16
17     public Root(String path) throws IOException {
18         super(path);
19         if (!new File(path + File.separatorChar + "host").exists()) new File(path + File.separatorChar + "host").mkdirs();
20         if (!new File(path + File.separatorChar + "port").exists()) new File(path + File.separatorChar + "port").mkdirs();
21         port = new PortDir(path + File.separatorChar + "port");
22         watch(path + File.separatorChar + "host");
23         watch(path + File.separatorChar + "port");
24         scan();
25     }
26
27     public void changed(File w) throws IOException {
28         if (w.getName().equals("host"))        Log.debug(this, "/host changed");
29         else if (w.getName().equals("port")) Log.debug(this, "/port changed");
30         /*
31         else if (w.getAbsolutePath().endsWith(".jar")) {
32             if (w.lastModifiedAtLastScan != -1) {
33                 Log.error(this, "jinetd upgraded; bouncing the JVM....");
34                 Main.reboot();
35             } }
36         */
37         else Log.debug(this, "unknown directory " + w.getAbsolutePath() + " changed");
38     }
39
40     private static class PortDir extends Watcher {
41         public PortDir(String path) throws IOException {
42             super(path);
43             String[] ports = new File(path).list();
44             for(int i=0; i<ports.length; i++) watch(path + File.separatorChar + ports[i]);
45             scan();
46         }
47
48         // FIXME: need to decommission old ports...
49         public void changed(File f) {
50             String part = f.getAbsolutePath();
51             part = f.getName();
52             // FIXME, use subdirs instead
53             String ipaddr  = part.indexOf('_') == -1 ? null : part.substring(0, part.indexOf('_'));
54             String portnum = part.indexOf('_') == -1 ? part : part.substring(part.indexOf('_') + 1);
55             try {
56                 Port.newPort(getAbsolutePath() + File.separatorChar + part,
57                              ipaddr == null ? null : InetAddress.getByName(ipaddr),
58                              portnum.equals("*") ? 0 : Integer.parseInt(portnum),
59                              tp);
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         }
65     }
66
67 }