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.
5 package org.ibex.jinetd;
6 import org.ibex.util.*;
10 import java.util.zip.*;
14 public static String ROOT;
15 public static String LOGFILE;
16 public static PrintStream LOGSTREAM;
18 private static void configureRoot() throws Exception {
19 ROOT = System.getProperty("jinetd.root", null);
21 ROOT = autoDetectRoot();
22 System.setProperty("jinetd.root", ROOT);
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);
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() +
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();
51 throw new Error("unable to detect jinetd.root because " +
52 Main.class.getName() +
53 " was not in any of the ClassLoader URLs");
60 } catch (Throwable e) {
65 public static void main(String[] s) throws Exception {
67 Root root = new Root(Root.root);
70 if (root != null) root.scan();
71 } catch (Exception e) { Log.error(Main.class, e); }