cleanup
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Main.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.net.*;
9 import java.util.*;
10 import java.util.zip.*;
11
12 public class Main {
13
14     // Bootup //////////////////////////////////////////////////////////////////////////////
15
16     public static String ROOT;
17     public static String LOGFILE;
18     public static PrintStream LOGSTREAM;
19     public static String defaultDomain;
20
21     static {
22         try {
23             System.err.println("jinetd starting...");
24             ROOT = System.getProperty("jinetd.root", null);
25             if (ROOT == null) System.setProperty("jinetd.root", ROOT = autoDetectRoot());
26             System.err.println("    jinetd.root    = " + ROOT);
27             defaultDomain = System.getProperty("jinetd.hostname", null);
28             if (defaultDomain==null) try {
29                 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
30                 defaultDomain = localMachine.getHostName();
31             } catch(java.net.UnknownHostException uhe) { defaultDomain = "localhost"; }
32             System.err.println("    jinetd.hostname = " + defaultDomain);
33             LOGFILE = System.getProperty("jinetd.logfile", ROOT + File.separatorChar+"log.txt");
34             System.err.println("    jinetd.logfile = " + LOGFILE);
35             System.err.println("    redirecting stdout/stderr to logfile." + LOGFILE);
36             LOGSTREAM = new PrintStream(new FileOutputStream(LOGFILE, true));
37             System.setErr(LOGSTREAM);
38             System.setOut(LOGSTREAM);
39         } catch (Throwable e) {
40             throw new Error(e);
41         }
42     }
43
44     private static String autoDetectRoot() throws Exception {
45         if (!(Main.class.getClassLoader() instanceof URLClassLoader))
46             throw new Error("unable to detect jinetd.root because my ClassLoader is not an instanceof URLClassLoader");
47         URL[] urls = ((URLClassLoader)Main.class.getClassLoader()).getURLs();
48         for(int i=0; i<urls.length; i++) {
49             if (!urls[i].getProtocol().equals("file")) continue;
50             File file = new File(urls[i].getPath());
51             if (file.isDirectory()) {
52                 if (new File(file.getAbsolutePath() +
53                              File.separatorChar + 
54                              Main.class.getName().replace('.', File.separatorChar)+".class").exists())
55                     return file.getAbsolutePath();
56             } else if (file.getAbsolutePath().endsWith(".jar")) {
57                 ZipFile zf = new ZipFile(file);
58                 if (zf.getEntry(Main.class.getName().replace('.', File.separatorChar)+".class") != null)
59                     return new File(file.getParent()).getAbsolutePath();
60             }
61         }
62         throw new Error("unable to detect jinetd.root because " +
63                         Main.class.getName() +
64                         " was not in any of the ClassLoader URLs");
65     }
66
67     public static void reboot() {
68         Log.flush();
69         System.exit(0);
70     }
71
72     public static void main(String[] s) throws Exception {
73         Root root = new Root(ROOT);
74         while(true) try {
75             if (root != null) { root.scan(); return; }
76             Thread.sleep(100);
77         } catch (Exception e) { Log.error(Main.class, e); }
78     }
79
80 }