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