Unnamed patch
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Main.java
index e32a32c..037527e 100644 (file)
@@ -1,17 +1,80 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.jinetd;
 import org.ibex.util.*;
 import java.io.*;
+import java.net.*;
 import java.util.*;
+import java.util.zip.*;
 
 public class Main {
 
-    public static Root root = new Root(System.getProperty("jinetd.root", "/jinetd"));
+    // Bootup //////////////////////////////////////////////////////////////////////////////
+
+    public static String ROOT;
+    public static String LOGFILE;
+    public static PrintStream LOGSTREAM;
+    public static String defaultDomain;
+
+    public static void init() {
+        try {
+            System.err.println("jinetd starting...");
+            ROOT = System.getProperty("jinetd.root", null);
+            if (ROOT == null) System.setProperty("jinetd.root", ROOT = autoDetectRoot());
+            System.err.println("    jinetd.root    = " + ROOT);
+            defaultDomain = System.getProperty("jinetd.hostname", null);
+            if (defaultDomain==null) try {
+                java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
+                defaultDomain = localMachine.getHostName();
+            } catch(java.net.UnknownHostException uhe) { defaultDomain = "localhost"; }
+            System.err.println("    jinetd.hostname = " + defaultDomain);
+            LOGFILE = System.getProperty("jinetd.logfile", ROOT + File.separatorChar+"log.txt");
+            System.err.println("    jinetd.logfile = " + LOGFILE);
+            System.err.println("    redirecting stdout/stderr to logfile." + LOGFILE);
+            LOGSTREAM = new PrintStream(new FileOutputStream(LOGFILE, true));
+            System.setErr(LOGSTREAM);
+            System.setOut(LOGSTREAM);
+        } catch (Throwable e) {
+            throw new Error(e);
+        }
+    }
+
+    private static String autoDetectRoot() throws Exception {
+        if (!(Main.class.getClassLoader() instanceof URLClassLoader))
+            throw new Error("unable to detect jinetd.root because my ClassLoader is not an instanceof URLClassLoader");
+        URL[] urls = ((URLClassLoader)Main.class.getClassLoader()).getURLs();
+        for(int i=0; i<urls.length; i++) {
+            if (!urls[i].getProtocol().equals("file")) continue;
+            File file = new File(urls[i].getPath());
+            if (file.isDirectory()) {
+                if (new File(file.getAbsolutePath() +
+                             File.separatorChar + 
+                             Main.class.getName().replace('.', File.separatorChar)+".class").exists())
+                    return file.getAbsolutePath();
+            } else if (file.getAbsolutePath().endsWith(".jar")) {
+                ZipFile zf = new ZipFile(file);
+                if (zf.getEntry(Main.class.getName().replace('.', File.separatorChar)+".class") != null)
+                    return new File(file.getParent()).getAbsolutePath();
+            }
+        }
+        throw new Error("unable to detect jinetd.root because " +
+                        Main.class.getName() +
+                        " was not in any of the ClassLoader URLs");
+    }
+
+    public static void reboot() {
+        Log.flush();
+        System.exit(0);
+    }
+
     public static void main(String[] s) throws Exception {
-        Log.color = true;
+        init();
+        Root root = new Root(ROOT);
         while(true) try {
-            Thread.sleep(1000);
-            //Log.info(Main.class, "scanning...");
-            if (root != null) root.scan();
+            if (root != null) { root.scan(); return; }
+            Thread.sleep(100);
         } catch (Exception e) { Log.error(Main.class, e); }
     }