a93972085bae8f4c0a72c2e6a7e4f322a59e890f
[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     static {
17         System.setProperty("java.awt.headless", "true");
18         System.setProperty("ibex.log.stackTraces", "true");
19         System.setProperty("ibex.log.notes.on", "false");
20         System.setProperty("org.mortbay.xml.XmlParser.NotValidating", "true");
21         System.setProperty("STOP.PORT", "0");
22     }
23
24     public static String ROOT;
25     public static String LOGFILE;
26     public static PrintStream LOGSTREAM;
27     public static String defaultDomain;
28
29     public static void init() {
30         try {
31             System.err.println("jinetd starting...");
32             ROOT = System.getProperty("jinetd.root", null);
33             if (ROOT == null) System.setProperty("jinetd.root", ROOT = autoDetectRoot());
34             System.err.println("    jinetd.root    = " + ROOT);
35             defaultDomain = System.getProperty("jinetd.hostname", null);
36             if (defaultDomain==null) try {
37                 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
38                 defaultDomain = localMachine.getHostName();
39             } catch(java.net.UnknownHostException uhe) { defaultDomain = "localhost"; }
40             System.err.println("    jinetd.hostname = " + defaultDomain);
41             LOGFILE = System.getProperty("jinetd.logfile", ROOT + File.separatorChar+"log.txt");
42             System.err.println("    jinetd.logfile = " + LOGFILE);
43             System.err.println("    redirecting stdout/stderr to logfile." + LOGFILE);
44             LOGSTREAM = new PrintStream(new FileOutputStream(LOGFILE, true));
45             System.setErr(LOGSTREAM);
46             System.setOut(LOGSTREAM);
47
48             gatherDependencies();
49         } catch (Throwable e) {
50             throw new Error(e);
51         }
52     }
53
54     private static void depend(String name, String url) throws Exception {
55         File f = new File(ROOT + File.separatorChar + "lib" + File.separatorChar + name);
56         if (f.exists()) return;
57         Log.warn(Main.class, "attempting to fetch " + name);
58         File fminus = new File(ROOT + File.separatorChar + "lib" + File.separatorChar + name + "-");
59         GetDep.fetch(fminus.getAbsolutePath(), url);
60         fminus.renameTo(f);
61     }
62
63     // FIXME: really need some hashes in here for security
64     private static void gatherDependencies() throws Exception {
65         depend("bcel-5.1.jar",
66                "tgz:http://apache.cs.utah.edu/jakarta/bcel/binaries/bcel-5.1.tar.gz!bcel-5.1/bcel-5.1.jar");
67
68         depend("commons-el.jar",
69                "tgz:http://www.signal42.com/mirrors/apache/jakarta/commons/el/binaries/commons-el-1.0.tar.gz!"+
70                "commons-el-1.0/commons-el.jar");
71
72         depend("commons-logging.jar",
73                "tgz:http://apache.towardex.com/jakarta/commons/logging/binaries/commons-logging-1.0.4.tar.gz!"+
74                "commons-logging-1.0.4/commons-logging.jar");
75
76         depend("org.mortbay.jetty.jar",
77                "zip:http://voxel.dl.sourceforge.net/sourceforge/jetty/jetty-5.1.2.zip!jetty-5.1.2/lib/org.mortbay.jetty.jar");
78         depend("javax.servlet.jar",
79                "zip:http://voxel.dl.sourceforge.net/sourceforge/jetty/jetty-5.1.2.zip!jetty-5.1.2/lib/javax.servlet.jar");
80
81         depend("prevayler-2.02.005.jar",
82                "tgz:http://unc.dl.sourceforge.net/sourceforge/prevayler/prevayler-2.02.005.tar.gz!"+
83                "prevayler-2.02.005/prevayler-2.02.005.jar");
84
85         depend("jasper-runtime.jar",
86                "tgz:http://www.reverse.net/pub/apache/jakarta/tomcat-5/v5.5.8/bin/jakarta-tomcat-5.5.8.tar.gz!"+
87                "jakarta-tomcat-5.5.8/common/lib/jasper-runtime.jar");
88         depend("jasper-compiler.jar",
89                "tgz:http://www.reverse.net/pub/apache/jakarta/tomcat-5/v5.5.8/bin/jakarta-tomcat-5.5.8.tar.gz!"+
90                "jakarta-tomcat-5.5.8/common/lib/jasper-compiler.jar");
91         depend("jasper-compiler-jdt.jar",
92                "tgz:http://www.reverse.net/pub/apache/jakarta/tomcat-5/v5.5.8/bin/jakarta-tomcat-5.5.8.tar.gz!"+
93                "jakarta-tomcat-5.5.8/common/lib/jasper-compiler-jdt.jar");
94
95         depend("xpp3_min-1.1.3.4.I.jar",
96                "http://www.extreme.indiana.edu/dist/java-repository/xpp3/jars/xpp3_min-1.1.3.4.I.jar");
97         depend("xstream.jar",
98                "http://dist.codehaus.org/xstream/jars/xstream-1.1.1.jar");
99         depend("skaringa-r3p5.jar",
100                "tgz:http://unc.dl.sourceforge.net/sourceforge/skaringa/skaringa-r3p5.tar.gz!"+
101                "skaringa/lib/skaringa-r3p5.jar");
102     }
103
104     private static String autoDetectRoot() throws Exception {
105         if (!(Main.class.getClassLoader() instanceof URLClassLoader))
106             throw new Error("unable to detect jinetd.root because my ClassLoader is not an instanceof URLClassLoader");
107         URL[] urls = ((URLClassLoader)Main.class.getClassLoader()).getURLs();
108         for(int i=0; i<urls.length; i++) {
109             if (!urls[i].getProtocol().equals("file")) continue;
110             File file = new File(urls[i].getPath());
111             if (file.isDirectory()) {
112                 if (new File(file.getAbsolutePath() +
113                              File.separatorChar + 
114                              Main.class.getName().replace('.', File.separatorChar)+".class").exists())
115                     return file.getAbsolutePath();
116             } else if (file.getAbsolutePath().endsWith(".jar")) {
117                 ZipFile zf = new ZipFile(file);
118                 if (zf.getEntry(Main.class.getName().replace('.', File.separatorChar)+".class") != null)
119                     return new File(file.getParent()).getAbsolutePath();
120             }
121         }
122         throw new Error("unable to detect jinetd.root because " +
123                         Main.class.getName() +
124                         " was not in any of the ClassLoader URLs");
125     }
126
127     public static void reboot() {
128         Log.flush();
129         System.exit(0);
130     }
131
132     private static TreeClassLoader rootClassLoader;
133     public static TreeClassLoader getRootClassLoader() {
134         if (rootClassLoader==null)
135             rootClassLoader = new TreeClassLoader(new File(ROOT + File.separatorChar + "lib"),
136                                                   Main.class.getClassLoader());
137         return rootClassLoader;
138     }
139
140     public static void main(String[] s) throws Exception {
141         init();
142         Root root = new Root(ROOT);
143         while(true) try {
144             if (root != null) { root.scan(); return; }
145             Thread.sleep(100);
146         } catch (Exception e) { Log.error(Main.class, e); }
147     }
148
149 }