tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@locus.apache.org
Subject cvs commit: jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup Catalina.java
Date Fri, 21 Apr 2000 23:38:13 GMT
craigmcc    00/04/21 16:38:13

  Modified:    proposals/catalina build.xml
               proposals/catalina/src/bin catalina.bat catalina.sh
               proposals/catalina/src/conf server.xml
               proposals/catalina/src/share/org/apache/tomcat Server.java
               proposals/catalina/src/share/org/apache/tomcat/core
                        StandardServer.java
               proposals/catalina/src/share/org/apache/tomcat/startup
                        Catalina.java
  Log:
  Enable Catalina to actually start and stop itself.
  
  NOTE:  The test HTTP connector currently does not stop itself cleanly.
  I will address that shortly.
  
  Revision  Changes    Path
  1.6       +1 -0      jakarta-tomcat/proposals/catalina/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/build.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- build.xml	2000/04/21 06:46:35	1.5
  +++ build.xml	2000/04/21 23:38:10	1.6
  @@ -15,6 +15,7 @@
       <mkdir dir="${catalina.build}/etc"/>
       <mkdir dir="${catalina.build}/src"/>
       <mkdir dir="${catalina.build}/lib"/>
  +    <mkdir dir="${catalina.build}/logs"/>
     </target>
   
     <!-- Copy static files -->
  
  
  
  1.2       +4 -5      jakarta-tomcat/proposals/catalina/src/bin/catalina.bat
  
  Index: catalina.bat
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/bin/catalina.bat,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- catalina.bat	2000/04/18 15:19:49	1.1
  +++ catalina.bat	2000/04/21 23:38:11	1.2
  @@ -70,20 +70,19 @@
   :startServer
   echo Starting Catalina in new window
   echo Using classpath: %CLASSPATH%
  -start java org.apache.tomcat.startup.Catalina %2 %3 %4 %5 %6 %7 %8 %9
  +start java org.apache.tomcat.startup.Catalina %2 %3 %4 %5 %6 %7 %8 %9 start
   goto cleanup
   
   :runServer
   rem Start the Catalina Server
  -echo cp=%CLASSPATH%
  -java org.apache.tomcat.startup.Catalina %2 %3 %4 %5 %6 %7 %8 %9
  +echo Using classpath: %CLASSPATH%
  +java org.apache.tomcat.startup.Catalina %2 %3 %4 %5 %6 %7 %8 %9 start
   goto cleanup
   
   :stopServer
   rem Stop the Catalina Server
   echo Using classpath: %CLASSPATH%
  -java org.apache.tomcat.startup.Catalina -stop %2 %3 %4 %5 %6 %7 %8 %9
  -goto cleanup
  +java org.apache.tomcat.startup.Catalina -stop %2 %3 %4 %5 %6 %7 %8 %9 stop
   goto cleanup
   
   :setupEnv
  
  
  
  1.2       +4 -8      jakarta-tomcat/proposals/catalina/src/bin/catalina.sh
  
  Index: catalina.sh
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/bin/catalina.sh,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- catalina.sh	2000/02/13 07:09:30	1.1
  +++ catalina.sh	2000/04/21 23:38:11	1.2
  @@ -1,6 +1,6 @@
   #!/bin/sh
   #
  -# $Id: catalina.sh,v 1.1 2000/02/13 07:09:30 craigmcc Exp $
  +# $Id: catalina.sh,v 1.2 2000/04/21 23:38:11 craigmcc Exp $
   
   # Shell script to start and stop the server
   
  @@ -103,21 +103,17 @@
   if [ "$1" = "start" ] ; then 
     shift 
     echo Using classpath: ${CLASSPATH}
  -  $JAVACMD org.apache.tomcat.startup.Catalina "$@" &
  -#   $JAVACMD org.apache.tomcat.shell.Startup "$@" &
  +  $JAVACMD org.apache.tomcat.startup.Catalina "$@" start &
   
   elif [ "$1" = "stop" ] ; then 
     shift 
     echo Using classpath: ${CLASSPATH}
  -  $JAVACMD org.apache.tomcat.startup.Catalina -stop "$@"
  -#   $JAVACMD org.apache.tomcat.shell.Shutdown "$@"
  +  $JAVACMD org.apache.tomcat.startup.Catalina "$@" stop
   
   elif [ "$1" = "run" ] ; then 
     shift 
     echo Using classpath: ${CLASSPATH}
  -  $JAVACMD org.apache.tomcat.startup.Catalina "$@" 
  -#  $JAVACMD org.apache.tomcat.shell.Startup "$@" 
  -  # no &
  +  $JAVACMD org.apache.tomcat.startup.Catalina "$@" start
   
   ## Call it with source catalina.sh to set the env for catalina
   elif [ "$1" = "env" ] ; then 
  
  
  
  1.5       +9 -3      jakarta-tomcat/proposals/catalina/src/conf/server.xml
  
  Index: server.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/conf/server.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- server.xml	2000/04/21 19:50:30	1.4
  +++ server.xml	2000/04/21 23:38:11	1.5
  @@ -2,7 +2,7 @@
   <!-- Note that component elements are nested corresponding to their
        parent-child relationships with each other -->
   
  -<Server adminPort="-1" workDir="work">
  +<Server port="8005" shutdown="SHUTDOWN">
   
     <!-- Define all the connectors to associate with the following container -->
   
  @@ -16,6 +16,12 @@
       <!-- Lifecycle listeners are notified of component start and stop -->
       <!-- <Lifecycle className="....LifecycleListener...." param="value"/> -->
   
  +    <!-- Logger that receives all log messages for subordinate components
  +         unless overridden at a lower level -->
  +    <Logger className="org.apache.tomcat.logger.FileLogger"
  +            directory="logs" prefix="tomcat_log." suffix=".txt"
  +            timestamp="true"/>
  +
       <!-- Because this Realm is here, an instance will be shared globally -->
       <!--
       <Realm className="org.apache.tomcat.realm.JndiRealm"
  @@ -30,8 +36,8 @@
   
         <!-- Shared by all Contexts related to this virtual host -->
         <Logger className="org.apache.tomcat.logger.FileLogger"
  -              directory="localhost_logs" prefix="tomcat_log." suffix=""
  -              timestamp="false"/>
  +              directory="logs" prefix="localhost_log." suffix=".txt"
  +              timestamp="true"/>
   
         <Context path="/" docBase="webpages"/>
   
  
  
  
  1.2       +46 -4     jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/Server.java
  
  Index: Server.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/Server.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Server.java	2000/04/17 02:34:34	1.1
  +++ Server.java	2000/04/21 23:38:11	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/Server.java,v
1.1 2000/04/17 02:34:34 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/04/17 02:34:34 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/Server.java,v
1.2 2000/04/21 23:38:11 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/04/21 23:38:11 $
    *
    * ====================================================================
    *
  @@ -80,14 +80,50 @@
    * <code>Lifecycle</code>, such that when the <code>start()</code>
and
    * <code>stop()</code> methods are called, all of the defined Containers
    * and Connectors are also started or stopped.
  + * <p>
  + * In between, the implementation must open a server socket on the port number
  + * specified by the <code>port</code> property.  When a connection is accepted,
  + * the first line is read and compared with the specified shutdown command.
  + * If the command matches, shutdown of the server is initiated.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2000/04/17 02:34:34 $
  + * @version $Revision: 1.2 $ $Date: 2000/04/21 23:38:11 $
    */
   
   public interface Server {
   
   
  +    // ------------------------------------------------------------- Properties
  +
  +
  +    /**
  +     * Return the port number we listen to for shutdown commands.
  +     */
  +    public int getPort();
  +
  +
  +    /**
  +     * Set the port number we listen to for shutdown commands.
  +     *
  +     * @param port The new port number
  +     */
  +    public void setPort(int port);
  +
  +
  +    /**
  +     * Return the shutdown command string we are waiting for.
  +     */
  +    public String getShutdown();
  +
  +
  +    /**
  +     * Set the shutdown command we are waiting for.
  +     *
  +     * @param shutdown The new shutdown command
  +     */
  +    public void setShutdown(String shutdown);
  +
  +
       // --------------------------------------------------------- Public Methods
   
   
  @@ -112,6 +148,12 @@
        *  Connectors to associate with (implying calls out of order)
        */
       public void addContainer(Container container);
  +
  +
  +    /**
  +     * Wait until a proper shutdown command is received, then return.
  +     */
  +    public void await();
   
   
       /**
  
  
  
  1.2       +143 -11   jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardServer.java
  
  Index: StandardServer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardServer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StandardServer.java	2000/04/17 02:34:34	1.1
  +++ StandardServer.java	2000/04/21 23:38:12	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardServer.java,v
1.1 2000/04/17 02:34:34 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/04/17 02:34:34 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/core/StandardServer.java,v
1.2 2000/04/21 23:38:12 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/04/21 23:38:12 $
    *
    * ====================================================================
    *
  @@ -65,6 +65,10 @@
   package org.apache.tomcat.core;
   
   
  +import java.io.InputStream;
  +import java.io.IOException;
  +import java.net.ServerSocket;
  +import java.net.Socket;
   import java.util.Enumeration;
   import java.util.Vector;
   import org.apache.tomcat.Connector;
  @@ -84,7 +88,7 @@
    * (but not required) when deploying and starting Tomcat.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2000/04/17 02:34:34 $
  + * @version $Revision: 1.2 $ $Date: 2000/04/21 23:38:12 $
    */
   
   public final class StandardServer
  @@ -120,6 +124,18 @@
   
   
       /**
  +     * The port number on which we wait for shutdown commands.
  +     */
  +    private int port = 8005;
  +
  +
  +    /**
  +     * The shutdown command string we are looking for.
  +     */
  +    private String shutdown = "SHUTDOWN";
  +
  +
  +    /**
        * The string manager for this package.
        */
       private static StringManager sm =
  @@ -132,6 +148,53 @@
       private boolean started = false;
   
   
  +    // ------------------------------------------------------------- Properties
  +
  +
  +    /**
  +     * Return the port number we listen to for shutdown commands.
  +     */
  +    public int getPort() {
  +
  +        return (this.port);
  +
  +    }
  +
  +
  +    /**
  +     * Set the port number we listen to for shutdown commands.
  +     *
  +     * @param port The new port number
  +     */
  +    public void setPort(int port) {
  +
  +        this.port = port;
  +
  +    }
  +
  +
  +    /**
  +     * Return the shutdown command string we are waiting for.
  +     */
  +    public String getShutdown() {
  +
  +        return (this.shutdown);
  +
  +    }
  +
  +
  +    /**
  +     * Set the shutdown command we are waiting for.
  +     *
  +     * @param shutdown The new shutdown command
  +     */
  +    public void setShutdown(String shutdown) {
  +
  +        this.shutdown = shutdown;
  +
  +    }
  +
  +
       // --------------------------------------------------------- Server Methods
   
   
  @@ -163,18 +226,87 @@
       public void addContainer(Container container) {
   
   	containers.addElement(container);
  -	int n = 0;
   	Enumeration conns = connectors.elements();
   	while (conns.hasMoreElements()) {
   	    Connector conn = (Connector) conns.nextElement();
  -	    if (conn.getContainer() == null) {
  +	    if (conn.getContainer() == null)
   		conn.setContainer(container);
  -		n++;
  +	}
  +
  +    }
  +
  +
  +    /**
  +     * Wait until a proper shutdown command is received, then return.
  +     */
  +    public void await() {
  +
  +        // Set up a server socket to wait on
  +        ServerSocket serverSocket = null;
  +	try {
  +	    serverSocket = new ServerSocket(port, 1);
  +	} catch (IOException e) {
  +	    System.err.println("StandardServer.await: create: " + e);
  +	    e.printStackTrace();
  +	    System.exit(1);
  +	}
  +
  +	// Loop waiting for a connection and a valid command
  +	while (true) {
  +
  +	    // Wait for the next connection
  +	    Socket socket = null;
  +	    InputStream stream = null;
  +	    try {
  +	        socket = serverSocket.accept();
  +		socket.setSoTimeout(10 * 1000);  // Ten seconds
  +		stream = socket.getInputStream();
  +	    } catch (IOException e) {
  +	        System.err.println("StandardServer.await: accept: " + e);
  +		e.printStackTrace();
  +		System.exit(1);
   	    }
  +
  +	    // Read a line of characters from the socket
  +	    StringBuffer command = new StringBuffer();
  +	    while (true) {
  +	        int ch = -1;
  +	        try {
  +		    ch = stream.read();
  +		} catch (IOException e) {
  +		    System.err.println("StandardServer.await: read: " + e);
  +		    e.printStackTrace();
  +		    ch = -1;
  +		}
  +		if (ch < 32)  // Control character or EOF terminates loop
  +		    break;
  +		command.append((char) ch);
  +	    }
  +
  +	    // Close the socket now that we are done with it
  +	    try {
  +	        socket.close();
  +	    } catch (IOException e) {
  +	        ;
  +	    }
  +
  +	    // Match against our command string
  +	    boolean match = command.toString().equals(shutdown);
  +	    if (match) {
  +	        System.err.println("StandardServer.await: Shutting down");
  +		break;
  +	    } else
  +	        System.err.println("StandardServer.await: Invalid command '" +
  +				   command.toString() + "' received");
  +
  +	}
  +
  +	// Close the server socket and return
  +	try {
  +	    serverSocket.close();
  +	} catch (IOException e) {
  +	    ;
   	}
  -	if (n == 0)
  -	    throw new IllegalStateException
  -		(sm.getString("standardServer.addContainer.ise"));
   
       }
   
  @@ -242,7 +374,7 @@
   	    if (conn.getContainer() == null)
   		n++;
   	}
  -	if (n == 0)
  +	if (n > 0)
   	    throw new LifecycleException
   		(sm.getString("standardServer.start.connectors"));
   	started = true;
  
  
  
  1.3       +156 -15   jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/Catalina.java
  
  Index: Catalina.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/Catalina.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Catalina.java	2000/04/17 02:34:34	1.2
  +++ Catalina.java	2000/04/21 23:38:12	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/Catalina.java,v
1.2 2000/04/17 02:34:34 craigmcc Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/04/17 02:34:34 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/startup/Catalina.java,v
1.3 2000/04/21 23:38:12 craigmcc Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/04/21 23:38:12 $
    *
    * ====================================================================
    *
  @@ -67,6 +67,11 @@
   
   import java.io.File;
   import java.io.IOException;
  +import java.io.OutputStream;
  +import java.net.Socket;
  +import org.apache.tomcat.Lifecycle;
  +import org.apache.tomcat.LifecycleException;
  +import org.apache.tomcat.Server;
   import org.apache.tomcat.util.xml.XmlAction;
   import org.apache.tomcat.util.xml.XmlMapper;
   
  @@ -82,7 +87,7 @@
    * </u>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.2 $ $Date: 2000/04/17 02:34:34 $
  + * @version $Revision: 1.3 $ $Date: 2000/04/21 23:38:12 $
    */
   
   public final class Catalina {
  @@ -97,6 +102,24 @@
       private String configFile = "conf/server.xml";
   
   
  +    /**
  +     * The server component we are starting or stopping
  +     */
  +    private Server server = null;
  +
  +
  +    /**
  +     * Are we starting a new server?
  +     */
  +    private boolean starting = false;
  +
  +
  +    /**
  +     * Are we stopping an existing server?
  +     */
  +    private boolean stopping = false;
  +
  +
       // ----------------------------------------------------------- Main Program
   
   
  @@ -118,6 +141,21 @@
       }
   
   
  +    // --------------------------------------------------------- Public Methods
  +
  +
  +    /**
  +     * Set the server instance we are configuring.
  +     *
  +     * @param server The new server
  +     */
  +    public void setServer(Server server) {
  +
  +        this.server = server;
  +
  +    }
  +
  +
       // -------------------------------------------------------- Private Methods
   
   
  @@ -136,9 +174,15 @@
   	    if (isConfig) {
   		configFile = args[i];
   		isConfig = false;
  +	    } else if (args[i].equals("-config")) {
  +	        isConfig = true;
   	    } else if (args[i].equals("-help")) {
   		usage();
   		return (false);
  +	    } else if (args[i].equals("start")) {
  +	        starting = true;
  +	    } else if (args[i].equals("stop")) {
  +	        stopping = true;
   	    } else {
   		usage();
   	    }
  @@ -150,9 +194,9 @@
   
   
       /**
  -     * Create and configure the XmlMapper we will be using.
  +     * Create and configure the XmlMapper we will be using for startup.
        */
  -    private XmlMapper createMapper() {
  +    private XmlMapper createStartMapper() {
   
   	// Initialize the mapper
   	XmlMapper mapper = new XmlMapper();
  @@ -163,6 +207,8 @@
   	mapper.addRule("Server", mapper.objectCreate
   		       ("org.apache.tomcat.core.StandardServer", "className"));
   	mapper.addRule("Server", mapper.setProperties());
  +        mapper.addRule("Server", mapper.addChild
  +		       ("setServer", "org.apache.tomcat.Server"));
   
   	mapper.addRule("Server/Connector", mapper.objectCreate
   		       ("org.apache.tomcat.connector.http.HttpConnector",
  @@ -344,10 +390,29 @@
   	mapper.addRule("Server/Engine/Valve", mapper.addChild
   		       ("addValve", "org.apache.tomcat.Valve"));
   
  -
   	return (mapper);
  +
  +    }
  +
  +
  +    /**
  +     * Create and configure the XmlMapper we will be using for shutdown.
  +     */
  +    private XmlMapper createStopMapper() {
  +
  +        // Initialize the mapper
  +        XmlMapper mapper = new XmlMapper();
  +        mapper.setDebug(999);
  +
  +        // Configure the actions we will be using
  +
  +        mapper.addRule("Server", mapper.objectCreate
  +		       ("org.apache.tomcat.core.StandardServer", "className"));
  +        mapper.addRule("Server", mapper.setProperties());
  +        mapper.addRule("Server", mapper.addChild
  +		       ("setServer", "org.apache.tomcat.Server"));
   
  -	// FIXME - When do we call start() on all of these objects?
  +	return (mapper);
   
       }
   
  @@ -356,16 +421,92 @@
        * Execute the processing that has been configured from the command line.
        */
       private void execute() throws Exception {
  +
  +        if (starting)
  +	    start();
  +	else if (stopping)
  +	    stop();
  +
  +    }
  +
   
  -	// Configure the XmlMapper we will be using
  -	XmlMapper mapper = createMapper();
  +    /**
  +     * Start a new server instance.
  +     */
  +    private void start() {
   
  -	// Process the configuration file
  +        // Create and execute our mapper
  +        XmlMapper mapper = createStartMapper();
   	File file = new File(configFile);
  -	mapper.readXml(file, null);
  +	try {
  +	    mapper.readXml(file, this);
  +	} catch (Exception e) {
  +	    System.err.println("Catalina.start: " + e);
  +	    e.printStackTrace();
  +	    System.exit(1);
  +	}
  +
  +	// Start the new server
  +	if (server instanceof Lifecycle) {
  +	    try {
  +	        ((Lifecycle) server).start();
  +	    } catch (LifecycleException e) {
  +	        System.err.println("Catalina.start: " + e);
  +		e.printStackTrace();
  +	    }
  +	}
  +
  +
  +	// Wait for the server to be told to shut down
  +	server.await();
  +
  +	// Shut down the server
  +	if (server instanceof Lifecycle) {
  +	    try {
  +	        ((Lifecycle) server).stop();
  +	    } catch (LifecycleException e) {
  +	        System.err.println("Catalina.stop: " + e);
  +	        e.printStackTrace();
  +	    }
  +	}
  +
  +    }
  +
  +
  +    /**
  +     * Stop an existing server instance.
  +     */
  +    private void stop() {
  +
  +      // Create and execute our mapper
  +      XmlMapper mapper = createStopMapper();
  +      File file = new File(configFile);
  +	try {
  +	    mapper.readXml(file, this);
  +	} catch (Exception e) {
  +	    System.err.println("Catalina.stop: " + e);
  +	    e.printStackTrace();
  +	    System.exit(1);
  +	}
  +
  +      // Stop the existing server
  +      System.out.println("FIXME: Stop server at port " + server.getPort() +
  +			 " with command '" + server.getShutdown() + "'");
  +      try {
  +	  Socket socket = new Socket("localhost", 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.err.println("Catalina.stop: " + e);
  +	  e.printStackTrace();
  +	  System.exit(1);
  +      }
   
  -	// Start or stop
  -	;	// FIXME
   
       }
   
  @@ -376,7 +517,7 @@
       private void usage() {
   
   	System.err.println("usage: java org.apache.tomcat.startup.Catalina" +
  -			   " [ -config {pathname} | -help | -stop ]");
  +			   " [ -config {pathname} ] { start | stop }");
   
       }
   
  
  
  

Mime
View raw message