tons of changes
[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     public static String ROOT;
15     public static String LOGFILE;
16     public static PrintStream LOGSTREAM;
17
18     private static void configureRoot() throws Exception {
19         ROOT = System.getProperty("jinetd.root", null);
20         if (ROOT == null) {
21             ROOT = autoDetectRoot();
22             System.setProperty("jinetd.root", ROOT);
23         }
24     }
25
26     private static void configureLogging() throws Exception {
27         LOGFILE  = System.getProperty("jinetd.logfile", ROOT + File.separatorChar+"log.txt");
28         LOGSTREAM = new PrintStream(new FileOutputStream(LOGFILE, true));
29         System.setErr(LOGSTREAM);
30         System.setOut(LOGSTREAM);
31     }
32
33     private static String autoDetectRoot() throws Exception {
34         if (!(Main.class.getClassLoader() instanceof URLClassLoader))
35             throw new Error("unable to detect jinetd.root because my ClassLoader is not an instanceof URLClassLoader");
36         URL[] urls = ((URLClassLoader)Main.class.getClassLoader()).getURLs();
37         for(int i=0; i<urls.length; i++) {
38             if (!urls[i].getProtocol().equals("file")) continue;
39             File file = new File(urls[i].getPath());
40             if (file.isDirectory()) {
41                 if (new File(file.getAbsolutePath() +
42                              File.separatorChar + 
43                              Main.class.getName().replace('.', File.separatorChar)+".class").exists())
44                     return file.getAbsolutePath();
45             } else if (file.getAbsolutePath().endsWith(".jar")) {
46                 ZipFile zf = new ZipFile(file);
47                 if (zf.getEntry(Main.class.getName().replace('.', File.separatorChar)+".class") != null)
48                     return new File(file.getParent()).getAbsolutePath();
49             }
50         }
51         throw new Error("unable to detect jinetd.root because " +
52                         Main.class.getName() +
53                         " was not in any of the ClassLoader URLs");
54     }
55
56     static {
57         try {
58             configureRoot();
59             configureLogging();
60         } catch (Throwable e) {
61             throw new Error(e);
62         }
63     }
64
65     public static void main(String[] s) throws Exception {
66         Log.color = true;
67         Root root = new Root(Root.root);
68         while(true) try {
69             if (root != null) { root.scan(); return; }
70             Thread.sleep(100);
71         } catch (Exception e) { Log.error(Main.class, e); }
72     }
73
74 }