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 // Bootup //////////////////////////////////////////////////////////////////////////////
16 public static String ROOT;
17 public static String LOGFILE;
18 public static PrintStream LOGSTREAM;
19 public static String defaultDomain;
21 public static void init() {
23 System.err.println("jinetd starting...");
24 ROOT = System.getProperty("jinetd.root", null);
25 if (ROOT == null) System.setProperty("jinetd.root", ROOT = autoDetectRoot());
26 System.err.println(" jinetd.root = " + ROOT);
27 defaultDomain = System.getProperty("jinetd.hostname", null);
28 if (defaultDomain==null) try {
29 java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
30 defaultDomain = localMachine.getHostName();
31 } catch(java.net.UnknownHostException uhe) { defaultDomain = "localhost"; }
32 System.err.println(" jinetd.hostname = " + defaultDomain);
33 LOGFILE = System.getProperty("jinetd.logfile", ROOT + File.separatorChar+"log.txt");
34 System.err.println(" jinetd.logfile = " + LOGFILE);
35 System.err.println(" redirecting stdout/stderr to logfile." + LOGFILE);
36 LOGSTREAM = new PrintStream(new FileOutputStream(LOGFILE, true));
37 System.setErr(LOGSTREAM);
38 System.setOut(LOGSTREAM);
39 } catch (Throwable e) {
44 private static String autoDetectRoot() throws Exception {
45 if (!(Main.class.getClassLoader() instanceof URLClassLoader))
46 throw new Error("unable to detect jinetd.root because my ClassLoader is not an instanceof URLClassLoader");
47 URL[] urls = ((URLClassLoader)Main.class.getClassLoader()).getURLs();
48 for(int i=0; i<urls.length; i++) {
49 if (!urls[i].getProtocol().equals("file")) continue;
50 File file = new File(urls[i].getPath());
51 if (file.isDirectory()) {
52 if (new File(file.getAbsolutePath() +
54 Main.class.getName().replace('.', File.separatorChar)+".class").exists())
55 return file.getAbsolutePath();
56 } else if (file.getAbsolutePath().endsWith(".jar")) {
57 ZipFile zf = new ZipFile(file);
58 if (zf.getEntry(Main.class.getName().replace('.', File.separatorChar)+".class") != null)
59 return new File(file.getParent()).getAbsolutePath();
62 throw new Error("unable to detect jinetd.root because " +
63 Main.class.getName() +
64 " was not in any of the ClassLoader URLs");
67 public static void reboot() {
72 public static void main(String[] s) throws Exception {
74 Root root = new Root(ROOT);
76 if (root != null) { root.scan(); return; }
78 } catch (Exception e) { Log.error(Main.class, e); }