📄 serverhandlermanager.java
字号:
unixStuff(); // add the shutdown hook, if we can! Class c = java.lang.Runtime.class; Class cp[] = { java.lang.Thread.class }; try { Method m = c.getMethod("addShutdownHook", cp); Runtime r = Runtime.getRuntime(); Object[] param = { new ServerShutdownHook(this) }; m.invoke(r, param); if (debug) System.out.println("*** shutdownHook succesfully registered"); } catch (NoSuchMethodException ex) { // not using a recent jdk... } catch (InvocationTargetException ex) { if (debug) ex.printStackTrace(); } catch (IllegalAccessException ex) { if (debug) ex.printStackTrace(); } // now start the beast Enumeration e = enumerateServerHandlers(); ServerHandler s; while (e.hasMoreElements()) { String id = (String) e.nextElement(); s = lookupServerHandler(id); try { s.start(); } catch (ServerHandlerInitException ex) { error("Unable to start "+id+": "+ex.getMessage()); s.shutdown(); removeServerHandler(s); } catch (Exception ex) { error("Unknown error while starting "+id+": "+ex.getMessage()); s.shutdown(); removeServerHandler(s); } } if ( handlers.size() <= 0 ) fatal("No servers launched !"); } /** * Shutdown this server handler manager. * This method will call the shutdown method of all the running servers * stopping the manager. * <p>This server handler clones are considered shutdown too. */ public synchronized void shutdown() { Enumeration sk = handlers.keys(); ServerHandler server; while (sk.hasMoreElements()) { String servname = (String) sk.nextElement(); server = (ServerHandler) lookupServerHandler(servname); server.shutdown(); } } /** * Do some UNIX specific initialization. * THis method exists straight if it cannot succeed ! */ public void unixStuff() { // Do we have specific UNIX stuff in configuration ? String user = props.getString(SERVER_USER_P, null); String group = props.getString(SERVER_GROUP_P, null); Unix unix = null; // Check that UNIX native methods are available: if ((user != null) || (group != null)) { unix = Unix.getUnix(); if ( ! unix.isResolved() ) { String msg = ("You used either the -user or -group command " + " line option" + " usefull only when running Jigsaw under UNIX." + "To be able to set these, Jigsaw requires " + " that libUnix, distributed with Jigsaw, " + "be accessible from your LD_LIBRARY_PATH."); fatal(msg); } } // Change group if needed: if (group != null) { boolean error = false; int gid = unix.getGID(group); if (gid >= 0) { try { unix.setGID(gid); } catch (UnixException ex) { error = true; } } if ((gid < 0) || error) { String msg = ("UNIX initialization, unable to setgid(2) to " + group + ". Check the -group command line option." + ((gid < 0) ? "(the group doesn't exist)." : "(setgid call failed).")); fatal(msg); } System.out.println("+ running as group \""+group+"\"."); } // Change user if needed: if (user != null) { boolean error = false; int uid = unix.getUID(user); if (uid >= 0) { try { unix.setUID(uid); } catch (UnixException ex) { error = true; } } if ((uid < 0) || error) { String msg = ("UNIX initialization, unable to setuid(2) to " + user + ". Check the -user command line option." + ((uid < 0) ? "(the user doesn't exist)." : "(setuid call failed).")); fatal(msg); } System.out.println("+ running as user \""+user+"\"."); } } public static void usage () { PrintStream o = System.out ; o.println("usage: httpd [OPTIONS]") ; o.println("-root <directory> : root directory of server.") ; o.println("-p <propfile> : property file to read."); o.println("-trace : turns debugging on.") ; o.println("-chroot <root> : chroot Jigsaw at startup."); o.println("-config <dir> : use given directory as config dir."); o.println("-user <user> : identity of the server (UNIX only)."); o.println("-group <group> : group of the server (UNIX only)."); o.println("-maxstores <int> : Max number of stores in memory.") ; System.exit (1) ; } public static void main (String args[]) { Integer cmdport = null ; String cmdhost = null ; String cmdroot = null ; String cmdprop = "server.props"; String chroot = null; Boolean cmdtrace = null ; String cmdconfig = null; String cmduser = null; String cmdgroup = null; String maxstores = null; // Parse command line options: for (int i = 0 ; i < args.length ; i++) { if ( args[i].equals ("-port") ) { try { cmdport = new Integer(args[++i]) ; } catch (NumberFormatException ex) { System.out.println ("invalid port number ["+args[i]+"]"); System.exit (1) ; } } else if ( args[i].equals ("-host") && (i+1 < args.length)) { cmdhost = args[++i] ; } else if ( args[i].equals ("-maxstores") && (i+1 < args.length)) { maxstores = args[++i]; } else if ( args[i].equals ("-root") && (i+1 < args.length)) { cmdroot = args[++i] ; } else if ( args[i].equals ("-p") && (i+1 < args.length)) { cmdprop = args[++i] ; } else if ( args[i].equals ("-trace") ) { cmdtrace = Boolean.TRUE; } else if ( args[i].equals("-chroot") && (i+1 < args.length)) { chroot = args[++i]; } else if ( args[i].equals("-group") && (i+1 < args.length)) { cmdgroup = args[++i]; } else if ( args[i].equals("-user") && (i+1 < args.length)) { cmduser = args[++i]; } else if ( args[i].equals("-config") && (i+1 < args.length)) { cmdconfig = args[++i]; } else if ( args[i].equals ("?") || args[i].equals ("-help") ) { usage() ; } else { System.out.println ("unknown option: ["+args[i]+"]") ; System.exit (1) ; } } // Unix chroot if needed: if ( chroot != null ) { Unix unix = Unix.getUnix(); if ( ! unix.isResolved() ) { System.out.println("You cannot chroot Jigsaw if the libUnix " + " native extension is not available. " + "Check the documentation, or remove " + " this argument from command line."); System.exit(1); } try { unix.chroot(chroot); } catch (Exception ex) { System.out.println("The chroot system call failed, details:"); ex.printStackTrace(); System.exit(1); } } // Create the global daemon properties for all servers: Properties props = System.getProperties(); if (cmdroot == null) cmdroot = props.getProperty("user.dir", null); File configdir = new File(cmdroot , (cmdconfig==null) ? "config" : cmdconfig); // Try to guess it, cause it is really required: File propfile = new File (configdir, cmdprop); System.out.println ("loading properties from: " + propfile.getAbsolutePath()) ; try { props.load(new FileInputStream(propfile)) ; } catch (FileNotFoundException ex) { System.out.println ("Unable to load properties: "+cmdprop); System.out.println ("\t"+ex.getMessage()) ; System.exit (1) ; } catch (IOException ex) { System.out.println ("Unable to load properties: "+cmdprop); System.out.println ("\t"+ex.getMessage()) ; System.exit (1) ; } // Override properties with our command line options: if ( cmdconfig != null ) props.put(httpd.CONFIG_P , new File(cmdroot, cmdconfig).getAbsolutePath()); if ( cmdport != null ) props.put (httpd.PORT_P, cmdport.toString()) ; if ( cmdhost != null ) props.put (httpd.HOST_P, cmdhost) ; if ( cmdroot != null ) props.put (httpd.ROOT_P, cmdroot) ; if ( cmdtrace != null ) { props.put (httpd.TRACE_P, "true") ; props.put (httpd.CLIENT_DEBUG_P, "true") ; } if (maxstores != null) props.put (httpd.MAX_LOADED_STORE_P, maxstores); if ( cmdgroup != null ) props.put(SERVER_GROUP_P, cmdgroup); if ( cmduser != null ) props.put(SERVER_USER_P, cmduser); new ServerHandlerManager(args, configdir, props); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -