tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cvs commit: jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup Bootstrap.java Catalina.java BootstrapService.java CatalinaService.java
Date Thu, 24 Oct 2002 22:06:04 GMT
remm        2002/10/24 15:06:04

  Modified:    catalina/src/share/org/apache/catalina/startup
                        Bootstrap.java Catalina.java
  Removed:     catalina/src/share/org/apache/catalina/startup
                        BootstrapService.java CatalinaService.java
  Log:
  - Remove the *Service classes.
  - The basic functionality appears to be working. The Windows service
    will need to be tested and updated (tomorrow).
  
  Revision  Changes    Path
  1.6       +243 -112  jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/Bootstrap.java
  
  Index: Bootstrap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/Bootstrap.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Bootstrap.java	24 Oct 2002 13:53:22 -0000	1.5
  +++ Bootstrap.java	24 Oct 2002 22:06:04 -0000	1.6
  @@ -91,76 +91,54 @@
   
   public final class Bootstrap {
   
  +
  +    // -------------------------------------------------------------- Constants
  +
  +
  +    protected static final String CATALINA_TOKEN = "${catalina.home}";
  +
  +
  +    // ------------------------------------------------------- Static Variables
  +
  +
  +    /**
  +     * Daemon object used by main.
  +     */
  +    private static Bootstrap daemon = null;
  +
  +
  +    // -------------------------------------------------------------- Variables
  +
  +
       /**
        * Debugging detail level for processing the startup.
        */
       protected int debug = 0;
  -    protected String args[];
   
  -    // Construct the class loaders we will need
  +
  +    /**
  +     * Daemon reference.
  +     */
  +    private Object catalinaDaemon = null;
  +
  +
       protected ClassLoader commonLoader = null;
       protected ClassLoader catalinaLoader = null;
       protected ClassLoader sharedLoader = null;
   
  -    protected static final String CATALINA_TOKEN = "${catalina.home}";
  -    
  -    public void setDebug( int debug ) {
  -        this.debug=debug;
  -    }
  -
  -    public void setArgs(String args[] ) {
  -        this.args=args;
  -    }
   
  +    // -------------------------------------------------------- Private Methods
   
  -    // ----------------------------------------------------------- Main Program
   
  -
  -    public void initClassLoaders() {
  +    private void initClassLoaders() {
           try {
  -
  -            /*
  -            File unpacked[] = new File[1];
  -            File packed[] = new File[1];
  -            File packed2[] = new File[2];
  -            ClassLoaderFactory.setDebug(debug);
  -
  -            unpacked[0] = new File(getCatalinaHome(),
  -                                   "common" + File.separator + "classes");
  -            packed2[0] = new File(getCatalinaHome(),
  -                                  "common" + File.separator + "endorsed");
  -            packed2[1] = new File(getCatalinaHome(),
  -                                  "common" + File.separator + "lib");
  -            commonLoader =
  -                ClassLoaderFactory.createClassLoader(unpacked, packed2, null);
  -
  -            unpacked[0] = new File(getCatalinaHome(),
  -                                   "server" + File.separator + "classes");
  -            packed[0] = new File(getCatalinaHome(),
  -                                 "server" + File.separator + "lib");
  -            catalinaLoader =
  -                ClassLoaderFactory.createClassLoader(unpacked, packed,
  -                                                     commonLoader);
  -
  -            unpacked[0] = new File(getCatalinaBase(),
  -                                   "shared" + File.separator + "classes");
  -            packed[0] = new File(getCatalinaBase(),
  -                                 "shared" + File.separator + "lib");
  -            sharedLoader =
  -                ClassLoaderFactory.createClassLoader(unpacked, packed,
  -                                                     commonLoader);
  -            */
  -
               ClassLoaderFactory.setDebug(debug);
               commonLoader = createClassLoader("common", null);
               catalinaLoader = createClassLoader("server", commonLoader);
               sharedLoader = createClassLoader("shared", commonLoader);
  -
           } catch (Throwable t) {
  -
               log("Class loader creation threw exception", t);
               System.exit(1);
  -
           }
       }
   
  @@ -203,84 +181,237 @@
       }
   
   
  -    // ----------------------------------------------------------- Main Program
  +    /**
  +     * Initialize daemon.
  +     */
  +    private void init()
  +        throws Exception {
   
  -    public void execute() {
  -        // Set the debug flag appropriately
  -        for (int i = 0; i < args.length; i++)  {
  -            if ("-debug".equals(args[i]))
  -                setDebug( 1 );
  -        }
  -        
  -        // Configure catalina.base from catalina.home if not yet set
  -        if (System.getProperty("catalina.base") == null)
  -            System.setProperty("catalina.base", getCatalinaHome());
  +        // Set Catalina path
  +        setCatalinaHome();
  +        setCatalinaBase();
   
  -        this.initClassLoaders();
  +        initClassLoaders();
   
           Thread.currentThread().setContextClassLoader(catalinaLoader);
   
  - 
  +        SecurityClassLoad.securityClassLoad(catalinaLoader);
  +
           // Load our startup class and call its process() method
  -        try {
  -            SecurityClassLoad.securityClassLoad(catalinaLoader);
  +        if (debug >= 1)
  +            log("Loading startup class");
  +        Class startupClass =
  +            catalinaLoader.loadClass
  +            ("org.apache.catalina.startup.Catalina");
  +        Object startupInstance = startupClass.newInstance();
  +
  +        // Set the shared extensions class loader
  +        if (debug >= 1)
  +            log("Setting startup class properties");
  +        String methodName = "setParentClassLoader";
  +        Class paramTypes[] = new Class[1];
  +        paramTypes[0] = Class.forName("java.lang.ClassLoader");
  +        Object paramValues[] = new Object[1];
  +        paramValues[0] = sharedLoader;
  +        Method method =
  +            startupInstance.getClass().getMethod(methodName, paramTypes);
  +        method.invoke(startupInstance, paramValues);
  +
  +        catalinaDaemon = startupInstance;
  +
  +    }
  +
  +
  +    /**
  +     * Load daemon.
  +     */
  +    private void load(String[] arguments)
  +        throws Exception {
   
  -            // Instantiate a startup class instance
  -            if (debug >= 1)
  -                log("Loading startup class");
  -            Class startupClass =
  -                catalinaLoader.loadClass
  -                ("org.apache.catalina.startup.Catalina");
  -            Object startupInstance = startupClass.newInstance();
  -
  -            // Set the shared extensions class loader
  -            if (debug >= 1)
  -                log("Setting startup class properties");
  -            String methodName = "setParentClassLoader";
  -            Class paramTypes[] = new Class[1];
  -            paramTypes[0] = Class.forName("java.lang.ClassLoader");
  -            Object paramValues[] = new Object[1];
  -            paramValues[0] = sharedLoader;
  -            Method method =
  -                startupInstance.getClass().getMethod(methodName, paramTypes);
  -            method.invoke(startupInstance, paramValues);
  -
  -            // Call the process() method
  -            if (debug >= 1)
  -                log("Calling startup class process() method");
  -            methodName = "process";
  +        // Call the load() method
  +        String methodName = "load";
  +        Object param[];
  +        Class paramTypes[];
  +        if (arguments==null || arguments.length==0) {
  +            paramTypes = null;
  +            param = null;
  +        } else {
               paramTypes = new Class[1];
  -            paramTypes[0] = args.getClass();
  -            paramValues = new Object[1];
  -            paramValues[0] = args;
  -            method =
  -                startupInstance.getClass().getMethod(methodName, paramTypes);
  -            method.invoke(startupInstance, paramValues);
  +            paramTypes[0] = arguments.getClass();
  +            param = new Object[1];
  +            param[0] = arguments;
  +        }
  +        Method method = 
  +            catalinaDaemon.getClass().getMethod(methodName, paramTypes);
  +        if (debug >= 1)
  +            log("Calling startup class " + method);
  +        method.invoke(catalinaDaemon, param);
  +
  +    }
  +
   
  -        } catch (Exception e) {
  -            System.out.println("Exception during startup processing");
  -            e.printStackTrace(System.out);
  -            System.exit(2);
  +    // ----------------------------------------------------------- Main Program
  +
  +
  +    /**
  +     * Load the Catalina daemon.
  +     */
  +    public void init(String[] arguments)
  +        throws Exception {
  +
  +        // Read the arguments
  +        if (arguments != null) {
  +            for (int i = 0; i < arguments.length; i++) {
  +                if (arguments[i].equals("-debug")) {
  +                    debug = 1;
  +                }
  +            }
           }
  +
  +        init();
  +        load(arguments);
  +
  +    }
  +
  +
  +    /**
  +     * Start the Catalina daemon.
  +     */
  +    public void start()
  +        throws Exception {
  +
  +        Method method = catalinaDaemon.getClass().getMethod("start", null);
  +        method.invoke(catalinaDaemon, null);
  +
  +    }
  +
  +
  +    /**
  +     * Stop the Catalina Daemon.
  +     */
  +    public void stop()
  +        throws Exception {
  +
  +        Method method = catalinaDaemon.getClass().getMethod("stop", null);
  +        method.invoke(catalinaDaemon, null);
  +
       }
   
  +
       /**
  -     * The main program for the bootstrap.
  +     * Stop the standlone server.
  +     */
  +    public void stopServer()
  +        throws Exception {
  +
  +        Method method = 
  +            catalinaDaemon.getClass().getMethod("stopServer", null);
  +        method.invoke(catalinaDaemon, null);
  +
  +    }
  +
  +
  +    /**
  +     * Set flag.
  +     */
  +    public void setAwait(boolean await)
  +        throws Exception {
  +
  +        Class paramTypes[] = new Class[1];
  +        paramTypes[0] = Boolean.TYPE;
  +        Object paramValues[] = new Object[1];
  +        paramValues[0] = new Boolean(await);
  +        Method method = 
  +            catalinaDaemon.getClass().getMethod("setAwait", paramTypes);
  +        method.invoke(catalinaDaemon, paramValues);
  +
  +    }
  +
  +
  +    /**
  +     * Destroy the Catalina Daemon.
  +     */
  +    public void destroy() {
  +
  +        // FIXME
  +
  +    }
  +
  +
  +    /**
  +     * Main method, used for testing only.
        *
        * @param args Command line arguments to be processed
        */
       public static void main(String args[]) {
  -        Bootstrap bootstrap=new Bootstrap();
   
  -        bootstrap.setArgs( args );
  -        bootstrap.execute();
  +        if (daemon == null) {
  +            daemon = new Bootstrap();
  +            try {
  +                daemon.init();
  +            } catch (Throwable t) {
  +                t.printStackTrace();
  +                return;
  +            }
  +        }
  +
  +        try {
  +            String command = args[0];
  +            if (command.equals("startd")) {
  +                daemon.load(args);
  +                daemon.start();
  +            } else if (command.equals("stopd")) {
  +                daemon.stop();
  +            } else if (command.equals("start")) {
  +                daemon.setAwait(true);
  +                daemon.load(args);
  +                daemon.start();
  +            } else if (command.equals("stop")) {
  +                daemon.stopServer();
  +            }
  +        } catch (Throwable t) {
  +            t.printStackTrace();
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Set the <code>catalina.base</code> System property to the current
  +     * working directory if it has not been set.
  +     */
  +    private void setCatalinaBase() {
  +
  +        if (System.getProperty("catalina.base") != null)
  +            return;
  +        if (System.getProperty("catalina.home") != null)
  +            System.setProperty("catalina.base",
  +                               System.getProperty("catalina.home"));
  +        else
  +            System.setProperty("catalina.base",
  +                               System.getProperty("user.dir"));
  +
  +    }
  +
  +
  +    /**
  +     * Set the <code>catalina.home</code> System property to the current
  +     * working directory if it has not been set.
  +     */
  +    private void setCatalinaHome() {
  +
  +        if (System.getProperty("catalina.home") != null)
  +            return;
  +        System.setProperty("catalina.home",
  +                           System.getProperty("user.dir"));
  +
       }
   
   
       /**
        * Get the value of the catalina.home environment variable.
        */
  -    protected String getCatalinaHome() {
  +    protected static String getCatalinaHome() {
           return System.getProperty("catalina.home",
                                     System.getProperty("user.dir"));
       }
  @@ -289,7 +420,7 @@
       /**
        * Get the value of the catalina.base environment variable.
        */
  -    protected String getCatalinaBase() {
  +    protected static String getCatalinaBase() {
           return System.getProperty("catalina.base", getCatalinaHome());
       }
   
  @@ -299,7 +430,7 @@
        *
        * @param message The message to be logged
        */
  -    protected void log(String message) {
  +    protected static void log(String message) {
   
           System.out.print("Bootstrap: ");
           System.out.println(message);
  @@ -313,7 +444,7 @@
        * @param message The message to be logged
        * @param exception The exception to be logged
        */
  -    protected void log(String message, Throwable exception) {
  +    protected static void log(String message, Throwable exception) {
   
           log(message);
           exception.printStackTrace(System.out);
  
  
  
  1.8       +149 -90   jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/Catalina.java
  
  Index: Catalina.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/Catalina.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Catalina.java	16 Oct 2002 20:10:01 -0000	1.7
  +++ Catalina.java	24 Oct 2002 22:06:04 -0000	1.8
  @@ -150,6 +150,36 @@
       protected boolean useNaming = true;
   
   
  +    /**
  +     * Use await.
  +     */
  +    protected boolean await = false;
  +
  +
  +    /**
  +     * Shutdown hook.
  +     */
  +    protected Thread shutdownHook = new CatalinaShutdownHook();
  +
  +
  +    // ------------------------------------------------------------- Properties
  +
  +
  +    public void setUseNaming(boolean b) {
  +        useNaming = b;
  +    }
  +
  +
  +    public void setConfig(String file) {
  +        configFile = file;
  +    }
  +
  +
  +    public void setAwait(boolean b) {
  +        await = b;
  +    }
  +
  +
       // ----------------------------------------------------------- Main Program
   
   
  @@ -159,7 +189,6 @@
        * @param args Command line arguments
        */
       public static void main(String args[]) {
  -
           (new Catalina()).process(args);
       }
   
  @@ -170,12 +199,17 @@
        * @param args Command line arguments
        */
       public void process(String args[]) {
  -
  +        setAwait(true);
           setCatalinaHome();
           setCatalinaBase();
           try {
  -            if (arguments(args))
  -                execute();
  +            if (arguments(args)) {
  +                if (starting) {
  +                    start();
  +                } else if (stopping) {
  +                    stopServer();
  +                }
  +            }
           } catch (Exception e) {
               e.printStackTrace(System.out);
           }
  @@ -243,7 +277,9 @@
                   return (false);
               } else if (args[i].equals("start")) {
                   starting = true;
  +                stopping = false;
               } else if (args[i].equals("stop")) {
  +                starting = false;
                   stopping = true;
               } else {
                   usage();
  @@ -391,15 +427,40 @@
       }
   
   
  -    /**
  -     * Execute the processing that has been configured from the command line.
  -     */
  -    protected void execute() throws Exception {
  +    public void stopServer() {
   
  -        if (starting)
  -            start();
  -        else if (stopping)
  -            stop();
  +        // Create and execute our Digester
  +        Digester digester = createStopDigester();
  +        File file = configFile();
  +        try {
  +            InputSource is =
  +                new InputSource("file://" + file.getAbsolutePath());
  +            FileInputStream fis = new FileInputStream(file);
  +            is.setByteStream(fis);
  +            digester.push(this);
  +            digester.parse(is);
  +            fis.close();
  +        } catch (Exception e) {
  +            System.out.println("Catalina.stop: " + e);
  +            e.printStackTrace(System.out);
  +            System.exit(1);
  +        }
  +
  +        // Stop the existing server
  +        try {
  +            Socket socket = new Socket("127.0.0.1", server.getPort());
  +            OutputStream stream = socket.getOutputStream();
  +            String shutdown = server.getShutdown();
  +            for (int i = 0; i < shutdown.length(); i++)
  +                stream.write(shutdown.charAt(i));
  +            stream.flush();
  +            stream.close();
  +            socket.close();
  +        } catch (IOException e) {
  +            System.out.println("Catalina.stop: " + e);
  +            e.printStackTrace(System.out);
  +            System.exit(1);
  +        }
   
       }
   
  @@ -435,10 +496,11 @@
       /**
        * Start a new server instance.
        */
  -    protected void start() {
  +    public void load() {
  +
           // Create and execute our Digester
           Digester digester = createStartDigester();
  -        long t1=System.currentTimeMillis();
  +        long t1 = System.currentTimeMillis();
           File file = configFile();
           try {
               InputSource is =
  @@ -453,9 +515,9 @@
               e.printStackTrace(System.out);
               System.exit(1);
           }
  -        long t2=System.currentTimeMillis();
  -        log.debug( "Server.xml processed " + (t2-t1 ));
  -        
  +        long t2 = System.currentTimeMillis();
  +        log.debug( "Server.xml processed " + (t2 - t1));
  +
           // Setting additional variables
           if (!useNaming) {
               System.setProperty("catalina.useNaming", "false");
  @@ -480,104 +542,101 @@
           SecurityConfig securityConfig = SecurityConfig.newInstance();
           securityConfig.setPackageDefinition();
           securityConfig.setPackageAccess();
  -        
  +
           // Replace System.out and System.err with a custom PrintStream
           SystemLogHandler systemlog = new SystemLogHandler(System.out);
           System.setOut(systemlog);
           System.setErr(systemlog);
   
  -        Thread shutdownHook = new CatalinaShutdownHook();
  -
  -        long t3=System.currentTimeMillis();
  -
           // Start the new server
           if (server instanceof Lifecycle) {
               try {
                   server.initialize();
  -                ((Lifecycle) server).start();
  -                long t4=System.currentTimeMillis();
  -                log.debug( "server.start " + server + " " + (t4-t3 ));
  -                try {
  -                    // Register shutdown hook
  -                    Runtime.getRuntime().addShutdownHook(shutdownHook);
  -                } catch (Throwable t) {
  -                    // This will fail on JDK 1.2. Ignoring, as Tomcat can run
  -                    // fine without the shutdown hook.
  -                }
  -                // Wait for the server to be told to shut down
  -                server.await();
               } catch (LifecycleException e) {
  -                System.out.println("Catalina.start: " + e);
  -                e.printStackTrace(System.out);
  -                if (e.getThrowable() != null) {
  -                    System.out.println("----- Root Cause -----");
  -                    e.getThrowable().printStackTrace(System.out);
  -                }
  +                log.error("Catalina.start", e);
               }
           }
   
  -        // Shut down the server
  +    }
  +
  +    /* 
  +     * Load using arguments
  +     */
  +    public void load(String args[]) {
  +
  +        setCatalinaHome();
  +        setCatalinaBase();
  +        try {
  +            if (arguments(args))
  +                load();
  +        } catch (Exception e) {
  +            e.printStackTrace(System.out);
  +        }
  +    }
  +
  +
  +    /**
  +     * Start a new server instance.
  +     */
  +    public void start() {
  +
  +        // Start the new server
           if (server instanceof Lifecycle) {
               try {
  -                try {
  -                    // Remove the ShutdownHook first so that server.stop() 
  -                    // doesn't get invoked twice
  -                    Runtime.getRuntime().removeShutdownHook(shutdownHook);
  -                } catch (Throwable t) {
  -                    // This will fail on JDK 1.2. Ignoring, as Tomcat can run
  -                    // fine without the shutdown hook.
  -                }
  -                ((Lifecycle) server).stop();
  +                ((Lifecycle) server).start();
               } catch (LifecycleException e) {
  -                System.out.println("Catalina.stop: " + e);
  -                e.printStackTrace(System.out);
  -                if (e.getThrowable() != null) {
  -                    System.out.println("----- Root Cause -----");
  -                    e.getThrowable().printStackTrace(System.out);
  -                }
  +                log.error("Catalina.start: ", e);
               }
           }
  +
  +        try {
  +            // Register shutdown hook
  +            Runtime.getRuntime().addShutdownHook(shutdownHook);
  +        } catch (Throwable t) {
  +            // This will fail on JDK 1.2. Ignoring, as Tomcat can run
  +            // fine without the shutdown hook.
  +        }
  +
  +        if (await) {
  +            await();
  +            stop();
  +        }
  +
       }
   
   
       /**
        * Stop an existing server instance.
        */
  -    protected void stop() {
  +    public void stop() {
   
  -        // Create and execute our Digester
  -        Digester digester = createStopDigester();
  -        File file = configFile();
           try {
  -            InputSource is =
  -                new InputSource("file://" + file.getAbsolutePath());
  -            FileInputStream fis = new FileInputStream(file);
  -            is.setByteStream(fis);
  -            digester.push(this);
  -            digester.parse(is);
  -            fis.close();
  -        } catch (Exception e) {
  -            System.out.println("Catalina.stop: " + e);
  -            e.printStackTrace(System.out);
  -            System.exit(1);
  +            // Remove the ShutdownHook first so that server.stop() 
  +            // doesn't get invoked twice
  +            Runtime.getRuntime().removeShutdownHook(shutdownHook);
  +        } catch (Throwable t) {
  +            // This will fail on JDK 1.2. Ignoring, as Tomcat can run
  +            // fine without the shutdown hook.
  +        }
  +
  +        // Shut down the server
  +        if (server instanceof Lifecycle) {
  +            try {
  +                ((Lifecycle) server).stop();
  +            } catch (LifecycleException e) {
  +                log.error("Catalina.stop", e);
  +            }
           }
   
  -      // Stop the existing server
  -      try {
  -          Socket socket = new Socket("127.0.0.1", server.getPort());
  -          OutputStream stream = socket.getOutputStream();
  -          String shutdown = server.getShutdown();
  -          for (int i = 0; i < shutdown.length(); i++)
  -              stream.write(shutdown.charAt(i));
  -          stream.flush();
  -          stream.close();
  -          socket.close();
  -      } catch (IOException e) {
  -          System.out.println("Catalina.stop: " + e);
  -          e.printStackTrace(System.out);
  -          System.exit(1);
  -      }
  +    }
  +
  +
  +    /**
  +     * Await and shutdown.
  +     */
  +    public void await() {
   
  +        server.await();
   
       }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:tomcat-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-dev-help@jakarta.apache.org>


Mime
View raw message