+ // Bootup //////////////////////////////////////////////////////////////////////////////
+
+ public static String ROOT;
+ public static String LOGFILE;
+ public static PrintStream LOGSTREAM;
+ public static String defaultDomain;
+
+ public static void init() {
+ try {
+ System.err.println("jinetd starting...");
+ ROOT = System.getProperty("jinetd.root", null);
+ if (ROOT == null) System.setProperty("jinetd.root", ROOT = autoDetectRoot());
+ System.err.println(" jinetd.root = " + ROOT);
+ defaultDomain = System.getProperty("jinetd.hostname", null);
+ if (defaultDomain==null) try {
+ java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
+ defaultDomain = localMachine.getHostName();
+ } catch(java.net.UnknownHostException uhe) { defaultDomain = "localhost"; }
+ System.err.println(" jinetd.hostname = " + defaultDomain);
+ LOGFILE = System.getProperty("jinetd.logfile", ROOT + File.separatorChar+"log.txt");
+ System.err.println(" jinetd.logfile = " + LOGFILE);
+ System.err.println(" redirecting stdout/stderr to logfile." + LOGFILE);
+ LOGSTREAM = new PrintStream(new FileOutputStream(LOGFILE, true));
+ System.setErr(LOGSTREAM);
+ System.setOut(LOGSTREAM);
+ } catch (Throwable e) {
+ throw new Error(e);
+ }
+ }
+
+ private static String autoDetectRoot() throws Exception {
+ if (!(Main.class.getClassLoader() instanceof URLClassLoader))
+ throw new Error("unable to detect jinetd.root because my ClassLoader is not an instanceof URLClassLoader");
+ URL[] urls = ((URLClassLoader)Main.class.getClassLoader()).getURLs();
+ for(int i=0; i<urls.length; i++) {
+ if (!urls[i].getProtocol().equals("file")) continue;
+ File file = new File(urls[i].getPath());
+ if (file.isDirectory()) {
+ if (new File(file.getAbsolutePath() +
+ File.separatorChar +
+ Main.class.getName().replace('.', File.separatorChar)+".class").exists())
+ return file.getAbsolutePath();
+ } else if (file.getAbsolutePath().endsWith(".jar")) {
+ ZipFile zf = new ZipFile(file);
+ if (zf.getEntry(Main.class.getName().replace('.', File.separatorChar)+".class") != null)
+ return new File(file.getParent()).getAbsolutePath();
+ }
+ }
+ throw new Error("unable to detect jinetd.root because " +
+ Main.class.getName() +
+ " was not in any of the ClassLoader URLs");
+ }
+
+ public static void reboot() {
+ Log.flush();
+ System.exit(0);
+ }
+