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.
5 package org.ibex.jinetd;
6 import org.ibex.util.*;
11 public class Root extends Watcher {
13 private final PortDir port;
15 private static final ThreadPool tp = new ThreadPool(10, 100);
17 public Root(String path) throws IOException {
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");
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");
31 else if (w.getAbsolutePath().endsWith(".jar")) {
32 if (w.lastModifiedAtLastScan != -1) {
33 Log.error(this, "jinetd upgraded; bouncing the JVM....");
37 else Log.debug(this, "unknown directory " + w.getAbsolutePath() + " changed");
40 private static class PortDir extends Watcher {
41 public PortDir(String path) throws IOException {
43 String[] ports = new File(path).list();
44 for(int i=0; i<ports.length; i++) watch(path + File.separatorChar + ports[i]);
48 // FIXME: need to decommission old ports...
49 public void changed(File f) {
50 String part = f.getAbsolutePath();
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);
56 Port.newPort(getAbsolutePath() + File.separatorChar + part,
57 ipaddr == null ? null : InetAddress.getByName(ipaddr),
58 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);