📄 serverhandlermanager.java
字号:
// ServerHandlerManager.java// $Id: ServerHandlerManager.java,v 1.24 2002/01/22 16:26:36 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1996.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.jigsaw.daemon;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.util.StringTokenizer;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.PrintStream;import org.w3c.util.ObservableProperties;import org.w3c.util.Unix;import org.w3c.util.UnixException;import org.w3c.jigsaw.http.httpd;import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;/** * A ServerHandlerManager instance manages a set of ServerHandler. */public class ServerHandlerManager { private static final boolean debug = false; /** * The property containing the servers to be launched at startup time. * This property is a <code>|</code> separated list of server identifiers. * Declaring a server to this list requires that either: * <ul> * <li>An appropriate * <code>org.w3c.jigsaw.daemon.</code><em>identifier</em>.<code>class *</code> * is declared and specify the class of the server to be launched (this * class should implement the ServerHandler interface.). * <li>An appropriate * <code>org.w3c.jigsaw.daemon.</code><em>identifier</em>.<code>clones *</code> * is declared and its value specify an existing server to be cloned in * order to create the new server. * </ul> */ protected static final String HANDLERS_P ="org.w3c.jigsaw.daemon.handlers"; /** * The server handler property <code>class</code> suffix. */ public final static String CLASS_P = "org.w3c.jigsaw.daemon.class"; /** * The server handler property <code>clones</code> prefix. */ public final static String CLONES_P = "org.w3c.jigsaw.daemon.clones"; public final static String SERVER_USER_P = "org.w3c.jigsaw.daemon.user"; public final static String SERVER_GROUP_P = "org.w3c.jigsaw.daemon.group"; /** * The Application-Wide server manager. */ protected static ServerHandlerManager manager = null; /** * The list of running server handlers. */ protected Hashtable handlers = null; /** * The server handler manager property list. */ protected DaemonProperties props = null; /** * Command line options that were provided at launch time. */ protected String commandLine[] = null; /** * Emit a non-fatal error. * @param msg The message to emit. */ protected void error(String msg) { System.err.println(msg); } /** * Emit a fatal error. * This will abort the whole process ! * @param msg The fata error message. */ protected void fatal(String msg) { System.out.println("*** FATAL Error:"); System.out.println(msg); System.exit(1); } /** * Get the command line options that were provided at launch time. * @return A String array instance. */ public String[] getCommandLine() { if ( commandLine == null ) commandLine = new String[0]; return commandLine; } /** * For subclasses only. Used to update properties at runtime. * This method is called by * launchServerHandler(String id, DaemonProperties props). * @param p the ServerHandlerManager properties. * @see launchServerHandler */ protected void fixProperties(Properties p) { //subclassed } /** * Launch a new server handler. * This method tries to launch a new server handler. If launching * succeeds, it returns happily, otherwise, it emits an error message * to the standard error stream. * @param identifier The identifier of the server handler to be launched. * @param props The properties from which the server handler should * initialize itself. */ protected void launchServerHandler(String id, DaemonProperties props) { ServerHandler server = null; String cls = props.getString(id+"."+CLASS_P, null); if ( cls != null ) { // This is a fresh new server handler: try { Class c = Class.forName(cls); server = (ServerHandler) c.newInstance(); ObservableProperties p = props.loadPropertySpace(id); this.fixProperties(p); server.initialize(this, id, p); } catch (FileNotFoundException ex) { error("Unable to launch "+id+", no properties found:" + ex.getMessage()); return; } catch (IOException ex) { error("Unable to launch "+id+", IO error in reading props."); return; } catch (ClassNotFoundException ex) { error("Unable to launch "+id+": class "+cls+" not found."); return; } catch (InstantiationException ex) { error("Unable to launch "+id+": unable to instanciate "+cls); return; } catch (IllegalAccessException ex) { error("Unable to launch "+id+": illegal access to "+cls); return; } catch (ServerHandlerInitException ex) { error("Unable to launch "+id+": "+ex.getMessage()); return; } } else { // It may be a clone of some already existing server handler: String clones = props.getString(id+"."+CLONES_P, null); if ( clones == null ) { error("Unable to launch "+id+": no class or clones defined."); return; } // Lookup the server handler to be cloned, clone it server = lookupServerHandler(clones); if ( server == null ) { error("Unable to launch "+id+": "+clones+" doesn't exit."); return; } try { server = server.clone(this, id, props.loadPropertySpace(id)); } catch (Exception ex) { error("Unable to launch "+id+": clone failed on "+server+"\r\n" + ex.getMessage()); return; } } // Register the created server: handlers.put(id, server); } /** * Lookup the server handler having the given identifier. * @param id The identifier of the server handler to look for. * @return A ServerHandler instance, or <strong>null</strong> if * undefined. */ public ServerHandler lookupServerHandler(String id) { return (ServerHandler) handlers.get(id); } /** * Enumerate all the server handler manager's identifiers. * @return An enumeration of String. */ public Enumeration enumerateServerHandlers() { return handlers.keys(); } /** * Remove a server handler from this manager * @param server, the Server Handler to remove */ public void removeServerHandler(ServerHandler server) { handlers.remove(server.getIdentifier()); } /** * Create and initialize a fresh server handler manager. * Each server handler declared in the properties is launched in turn. * If no server handlers is declared, or if none of them is initializable * the server manager is not created and a RuntimeException is thrown, * otherwise, if at least one server handler was initialized, the server * manager emits appropriate error messages to the error stream for each * of the server handlers whose launch failed. * @param props The properties this manager should be initialized from. * @exception RuntimeException If no server handlers was declared through * the properties. */ public ServerHandlerManager (String args[], File config, Properties p) { // Initialize instance variables: this.commandLine = args; this.handlers = new Hashtable(7); this.props = new DaemonProperties(config, p); // Basically launch all requested servers, emiting errors if needed: String hprop = props.getProperty(HANDLERS_P); if ((hprop == null) || hprop.equals("")) fatal("The property [org.w3c.jigsaw.daemon.handlers] is "+ " undefined."); StringTokenizer st = new StringTokenizer(hprop, "|"); while (st.hasMoreTokens()) { // Initialize and register this new server: String id = st.nextToken(); launchServerHandler(id, props); } // Check that we launched at least one server if ( handlers.size() <= 0 ) fatal("No servers initialized !"); // Terminate UNIX specific initialization:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -