added auto-download for dependencies
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Main.java
index c2cb324..a939720 100644 (file)
@@ -1,3 +1,7 @@
+// 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.*;
@@ -7,23 +11,94 @@ import java.util.zip.*;
 
 public class Main {
 
+    // Bootup //////////////////////////////////////////////////////////////////////////////
+
+    static {
+        System.setProperty("java.awt.headless", "true");
+        System.setProperty("ibex.log.stackTraces", "true");
+        System.setProperty("ibex.log.notes.on", "false");
+        System.setProperty("org.mortbay.xml.XmlParser.NotValidating", "true");
+        System.setProperty("STOP.PORT", "0");
+    }
+
     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);
 
-    private static void configureRoot() throws Exception {
-        ROOT = System.getProperty("jinetd.root", null);
-        if (ROOT == null) {
-            ROOT = autoDetectRoot();
-            System.setProperty("jinetd.root", ROOT);
+            gatherDependencies();
+        } catch (Throwable e) {
+            throw new Error(e);
         }
     }
 
-    private static void configureLogging() throws Exception {
-        LOGFILE  = System.getProperty("jinetd.logfile", ROOT + File.separatorChar+"log.txt");
-        LOGSTREAM = new PrintStream(new FileOutputStream(LOGFILE, true));
-        System.setErr(LOGSTREAM);
-        System.setOut(LOGSTREAM);
+    private static void depend(String name, String url) throws Exception {
+        File f = new File(ROOT + File.separatorChar + "lib" + File.separatorChar + name);
+        if (f.exists()) return;
+        Log.warn(Main.class, "attempting to fetch " + name);
+        File fminus = new File(ROOT + File.separatorChar + "lib" + File.separatorChar + name + "-");
+        GetDep.fetch(fminus.getAbsolutePath(), url);
+        fminus.renameTo(f);
+    }
+
+    // FIXME: really need some hashes in here for security
+    private static void gatherDependencies() throws Exception {
+        depend("bcel-5.1.jar",
+               "tgz:http://apache.cs.utah.edu/jakarta/bcel/binaries/bcel-5.1.tar.gz!bcel-5.1/bcel-5.1.jar");
+
+        depend("commons-el.jar",
+               "tgz:http://www.signal42.com/mirrors/apache/jakarta/commons/el/binaries/commons-el-1.0.tar.gz!"+
+               "commons-el-1.0/commons-el.jar");
+
+        depend("commons-logging.jar",
+               "tgz:http://apache.towardex.com/jakarta/commons/logging/binaries/commons-logging-1.0.4.tar.gz!"+
+               "commons-logging-1.0.4/commons-logging.jar");
+
+        depend("org.mortbay.jetty.jar",
+               "zip:http://voxel.dl.sourceforge.net/sourceforge/jetty/jetty-5.1.2.zip!jetty-5.1.2/lib/org.mortbay.jetty.jar");
+        depend("javax.servlet.jar",
+               "zip:http://voxel.dl.sourceforge.net/sourceforge/jetty/jetty-5.1.2.zip!jetty-5.1.2/lib/javax.servlet.jar");
+
+        depend("prevayler-2.02.005.jar",
+               "tgz:http://unc.dl.sourceforge.net/sourceforge/prevayler/prevayler-2.02.005.tar.gz!"+
+               "prevayler-2.02.005/prevayler-2.02.005.jar");
+
+        depend("jasper-runtime.jar",
+               "tgz:http://www.reverse.net/pub/apache/jakarta/tomcat-5/v5.5.8/bin/jakarta-tomcat-5.5.8.tar.gz!"+
+               "jakarta-tomcat-5.5.8/common/lib/jasper-runtime.jar");
+        depend("jasper-compiler.jar",
+               "tgz:http://www.reverse.net/pub/apache/jakarta/tomcat-5/v5.5.8/bin/jakarta-tomcat-5.5.8.tar.gz!"+
+               "jakarta-tomcat-5.5.8/common/lib/jasper-compiler.jar");
+        depend("jasper-compiler-jdt.jar",
+               "tgz:http://www.reverse.net/pub/apache/jakarta/tomcat-5/v5.5.8/bin/jakarta-tomcat-5.5.8.tar.gz!"+
+               "jakarta-tomcat-5.5.8/common/lib/jasper-compiler-jdt.jar");
+
+        depend("xpp3_min-1.1.3.4.I.jar",
+               "http://www.extreme.indiana.edu/dist/java-repository/xpp3/jars/xpp3_min-1.1.3.4.I.jar");
+        depend("xstream.jar",
+               "http://dist.codehaus.org/xstream/jars/xstream-1.1.1.jar");
+        depend("skaringa-r3p5.jar",
+               "tgz:http://unc.dl.sourceforge.net/sourceforge/skaringa/skaringa-r3p5.tar.gz!"+
+               "skaringa/lib/skaringa-r3p5.jar");
     }
 
     private static String autoDetectRoot() throws Exception {
@@ -49,21 +124,25 @@ public class Main {
                         " was not in any of the ClassLoader URLs");
     }
 
-    static {
-        try {
-            configureRoot();
-            configureLogging();
-        } catch (Throwable e) {
-            throw new Error(e);
-        }
+    public static void reboot() {
+        Log.flush();
+        System.exit(0);
+    }
+
+    private static TreeClassLoader rootClassLoader;
+    public static TreeClassLoader getRootClassLoader() {
+        if (rootClassLoader==null)
+            rootClassLoader = new TreeClassLoader(new File(ROOT + File.separatorChar + "lib"),
+                                                  Main.class.getClassLoader());
+        return rootClassLoader;
     }
 
     public static void main(String[] s) throws Exception {
-        Log.color = true;
-        Root root = new Root(Root.root);
+        init();
+        Root root = new Root(ROOT);
         while(true) try {
-            Thread.sleep(1000);
-            if (root != null) root.scan();
+            if (root != null) { root.scan(); return; }
+            Thread.sleep(100);
         } catch (Exception e) { Log.error(Main.class, e); }
     }