vmassol     01/08/19 07:27:55

  Modified:    cactus/src/ant/org/apache/commons/cactus/ant
                        AbstractServerRun.java EnhydraRun.java
                        ResinRun.java
  Log:
  align with coding conventions + more javadocs + refactoring
  
  Revision  Changes    Path
  1.2       +101 -29   
jakarta-commons/cactus/src/ant/org/apache/commons/cactus/ant/AbstractServerRun.java
  
  Index: AbstractServerRun.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/ant/org/apache/commons/cactus/ant/AbstractServerRun.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractServerRun.java    2001/06/18 12:43:14     1.1
  +++ AbstractServerRun.java    2001/08/19 14:27:55     1.2
  @@ -59,70 +59,132 @@
   import java.lang.reflect.*;
   
   /**
  - * Abstract class for starting/stopping an application server
  - * by setting up a listener socket.
  + * Abstract class for starting/stopping an application server. When this
  + * application is first called to start the server, a listener socket is
  + * set up. Then, we it is later called to stop the server, we connect to the
  + * listener socket and tell the server to stop.
  + *
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Robert Leftwich</a>
  + *
  + * @version $Id: AbstractServerRun.java,v 1.2 2001/08/19 14:27:55 vmassol Exp $
    */
   public abstract class AbstractServerRun extends Thread
   {
  -     private int m_Port = 7777;
  +    /**
  +     * Internal socket port that we use to stop the server.
  +     */
  +     private int port = 7777;
  +
  +    /**
  +     * Host name. We assume the server is started and stoppped in the same
  +     * local machine
  +     */
  +    private String host = "127.0.0.1";
  +
  +    /**
  +     * The command line arguments
  +     */
  +    protected String[] args;
  +
  +    /**
  +     * Flag that specifies if the server is already started to prevent
  +     * starting it if it is.
  +     */
  +    private boolean isStarted = false;
  +
  +    /**
  +     * @param theArgs the command line arguments
  +     */
  +    public AbstractServerRun(String[] theArgs)
  +    {
  +        this.args = theArgs;
  +    }
  +
  +    /**
  +     * Starts the server (in a blocking mode) and set up a socket listener.
  +     */
  +     abstract protected void doStartServer() throws Exception;
  +
  +    /**
  +     * Stops the server by connecting to the socket set up when the server
  +     * was started.
  +     */
  +     abstract protected void doStopServer() throws Exception;
   
  -     protected static void doRun(AbstractServerRun run, String[] args)
  +    /**
  +     * Parse and process the command line to start/stop the server.
  +     */
  +     protected void doRun()
        {
                // Look for a -start or -stop flag
                boolean isStart = true;
                Vector newArgs = new Vector();
   
  -             for (int i = 0; i < args.length; i++) {
  -                     if (args[i].equalsIgnoreCase("-start")) {
  +             for (int i = 0; i < this.args.length; i++) {
  +                     if (this.args[i].equalsIgnoreCase("-start")) {
                                isStart = true;
  -                     } else if (args[i].equalsIgnoreCase("-stop")) {
  +                     } else if (this.args[i].equalsIgnoreCase("-stop")) {
                                isStart = false;
  -                     } else if (args[i].equalsIgnoreCase("-port")) {
  -                             run.m_Port = Integer.parseInt(args[i+1]);
  +                     } else if (this.args[i].equalsIgnoreCase("-port")) {
  +                             this.port = Integer.parseInt(this.args[i+1]);
                                i++;
                        } else {
  -                             newArgs.add(args[i]);
  +                             newArgs.add(this.args[i]);
                        }
                }
   
  +        // Remove the command line arguments that should not be part of the
  +        // server command line (i.e. our own arguments).
  +        String[] strArgs = new String[0];
  +        this.args = (String[])newArgs.toArray(strArgs);
  +
                if (isStart) {
  -                     String[] strArgs = new String[0];
  -                     run.startServer((String[])newArgs.toArray(strArgs));
  +                     startServer();
                } else {
  -                     run.stopServer();
  +                     stopServer();
                }
   
        }
  -
  -     // abstract function to actually start the server
  -     abstract protected void doStartServer(String[] args) throws Exception;
  -
  -     // abstract function to actually stop the server
  -     abstract protected void doStopServer() throws Exception;
   
  -     private void startServer(String[] args)
  +    /**
  +     * Starts the server.
  +     */
  +     private void startServer()
        {
  +        // If the server is already started, do nothing
  +        if (this.isStarted) {
  +            return;
  +        }
  +
                try {
  -                     doStartServer( args );
  +                     doStartServer();
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException("Error starting server");
                }
   
  +        // Server is now started
  +        this.isStarted = true;
  +
                // Set up listener socket for listening to request to stop server
                new Thread(this).start();
        }
   
  +    /**
  +     * Stops the running server.
  +     */
        private void stopServer()
        {
                // Open socket connection
                Socket clientSocket = null;
   
                try {
  -                     clientSocket = new Socket("127.0.0.1", m_Port);
  +                     clientSocket = new Socket(this.host, this.port);
                } catch (Exception e) {
                        e.printStackTrace();
  -                     throw new RuntimeException("Error opening socket tp 127.0.0.1 
on port [" + m_Port + "]");
  +                     throw new RuntimeException("Error opening socket to " + 
this.host +
  +                ":" + this.port + "]");
                } finally {
                        try {
                                if (clientSocket != null) {
  @@ -134,6 +196,10 @@
                }
        }
   
  +    /**
  +     * Sets up a listener socket and wait until we receive a request on it to
  +     * stop the running server.
  +     */
        public void run()
        {
                ServerSocket serverSocket = setUpListenerSocket();
  @@ -143,14 +209,15 @@
                try {
                        clientSocket = serverSocket.accept();
                } catch (IOException e) {
  -                     throw new RuntimeException("Error accepting connection for 
server socket [" + serverSocket + "]");
  -
  +                     throw new RuntimeException("Error accepting connection for " +
  +                "server socket [" + serverSocket + "]");
                } finally {
                        // Stop server socket
                        try {
                                serverSocket.close();
                        } catch (IOException e) {
  -                             throw new RuntimeException("Cannot close server socket 
[" + serverSocket + "]");
  +                             throw new RuntimeException("Cannot close server socket 
[" +
  +                    serverSocket + "]");
                        }
                }
   
  @@ -166,20 +233,25 @@
                try {
                        serverSocket.close();
                } catch (IOException e) {
  -                     throw new RuntimeException("Cannot close server socket [" + 
serverSocket + "]");
  +                     throw new RuntimeException("Cannot close server socket [" +
  +                serverSocket + "]");
                }
   
        }
   
  +    /**
  +     * Sets up the listener socket.
  +     */
        private ServerSocket setUpListenerSocket()
        {
                ServerSocket serverSocket = null;
   
                try {
  -                     serverSocket = new ServerSocket(m_Port);
  +                     serverSocket = new ServerSocket(this.port);
                } catch (IOException e) {
                        e.printStackTrace();
  -                     throw new RuntimeException("Error setting up the server 
listener socket");
  +                     throw new RuntimeException("Error setting up the server " +
  +                "listener socket");
                }
   
                return serverSocket;
  
  
  
  1.2       +60 -13    
jakarta-commons/cactus/src/ant/org/apache/commons/cactus/ant/EnhydraRun.java
  
  Index: EnhydraRun.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/ant/org/apache/commons/cactus/ant/EnhydraRun.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EnhydraRun.java   2001/06/18 12:53:37     1.1
  +++ EnhydraRun.java   2001/08/19 14:27:55     1.2
  @@ -60,28 +60,75 @@
   
   /**
    * Starts/stop Enhydra by setting up a listener socket.
  + *
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Robert Leftwich</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
  + *
  + * @version $Id: EnhydraRun.java,v 1.2 2001/08/19 14:27:55 vmassol Exp $
  + * @see AbstractServerRun
    */
   public class EnhydraRun extends AbstractServerRun
   {
  -     private Object m_Server;
  +    /**
  +     * The started Enhydra server class. We use <code>Object</code> instead of
  +     * the Enhydra class so that we don't need the Resin jars in the classpath
  +     * to compile this class.
  +     */
  +    private Object enhydraServer;
   
  -     public static void main(String[] args)
  +    /**
  +     * Entry point to start/stop the Enhydra server.
  +     *
  +     * @param theArgs the command line arguments
  +     */
  +     public static void main(String[] theArgs)
        {
  -             AbstractServerRun.doRun( new EnhydraRun(), args );
  +        EnhydraRun enhydra = new EnhydraRun(theArgs);
  +        enhydra.doRun();
        }
   
  -     // function to actually start an Enhydra server
  -     protected void doStartServer(String[] args) throws Exception {
  -             Class enhydraClass = 
Class.forName("com.lutris.multiServer.MultiServer");
  -             Method initMethod = enhydraClass.getMethod("main", new Class[] { 
args.getClass() });
  -             initMethod.invoke(null, new Object[] { args } );
  +    /**
  +     * @param theArgs the command line arguments
  +     */
  +    public EnhydraRun(String[] args)
  +    {
  +        super(args);
  +    }
  +
  +    /**
  +     * Start the Enhydra server. We use reflection so that the Enhydra jars do
  +     * not need to be in the classpath to compile this class.
  +     */
  +     protected void doStartServer()
  +    {
  +        try {
  +            Class enhydraClass =
  +                Class.forName("com.lutris.multiServer.MultiServer");
  +            Method initMethod = enhydraClass.getMethod("main",
  +                new Class[] {this.args.getClass()});
  +            initMethod.invoke(null, new Object[] {this.args});
  +        } catch (Exception e) {
  +            e.printStackTrace();
  +            throw new RuntimeException("Cannot create instance of MultiServer");
  +        }
        }
   
  -     // function to actually stop an Enhydra server
  -     protected void doStopServer() throws Exception {
  -             Class enhydraClass = 
Class.forName("com.lutris.multiServer.MultiServer");
  -             Method shutDownMethod = enhydraClass.getMethod("shutdown", null);
  -             shutDownMethod.invoke(null, null );
  +    /**
  +     * Stops the Enhydra server. We use reflection so that the Enhydra jars do
  +     * not need to be in the classpath to compile this class.
  +     */
  +     protected void doStopServer()
  +    {
  +        try {
  +            Class enhydraClass =
  +                Class.forName("com.lutris.multiServer.MultiServer");
  +            Method shutDownMethod = enhydraClass.getMethod("shutdown", null);
  +            shutDownMethod.invoke(null, null );
  +        } catch (Exception e) {
  +            e.printStackTrace();
  +            throw new RuntimeException("Cannot stop running instance of " +
  +                "MultiServer");
  +        }
        }
   
   }
  
  
  
  1.3       +63 -21    
jakarta-commons/cactus/src/ant/org/apache/commons/cactus/ant/ResinRun.java
  
  Index: ResinRun.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/ant/org/apache/commons/cactus/ant/ResinRun.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResinRun.java     2001/06/18 12:43:14     1.2
  +++ ResinRun.java     2001/08/19 14:27:55     1.3
  @@ -60,36 +60,78 @@
   
   /**
    * Starts/stop Resin by setting up a listener socket.
  + *
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Robert Leftwich</a>
  + *
  + * @version $Id: ResinRun.java,v 1.3 2001/08/19 14:27:55 vmassol Exp $
  + * @see AbstractServerRun
    */
   public class ResinRun extends AbstractServerRun
   {
  -    private Object m_Server;
  +    /**
  +     * The started Resin server class. We use <code>Object</code> instead of
  +     * the Resin class so that we don't need the Resin jars in the classpath
  +     * to compile this class.
  +     */
  +    private Object resinServer;
   
  -     public static void main(String[] args)
  +    /**
  +     * Entry point to start/stop the Resin server.
  +     *
  +     * @param theArgs the command line arguments
  +     */
  +     public static void main(String[] theArgs)
        {
  -         AbstractServerRun.doRun(new ResinRun(), args);
  +        ResinRun resin = new ResinRun(theArgs);
  +        resin.doRun();
        }
   
  -     // function to actually start a Resin server
  -     protected void doStartServer(String[] args) throws Exception
  +    /**
  +     * @param theArgs the command line arguments
  +     */
  +    public ResinRun(String[] theArgs)
       {
  -         if (m_Server == null) {
  -                     Class resinClass = 
Class.forName("com.caucho.server.http.ResinServer");
  -                     Constructor constructor = resinClass.getConstructor(new 
Class[] { args.getClass(), boolean.class});
  -                     m_Server = constructor.newInstance(new Object[] { args, new 
Boolean(true)});
  -                     Method initMethod = resinClass.getMethod("init", new Class[] { 
boolean.class});
  -                     initMethod.invoke(m_Server, new Object[] { new Boolean(true)} 
);
  -             }
  -     }
  +        super(theArgs);
  +    }
   
  -     // function to actually stop a Resin server
  -     protected void doStopServer() throws Exception
  +    /**
  +     * Start the Resin server. We use reflection so that the Resin jars do not
  +     * need to be in the classpath to compile this class.
  +     */
  +     protected void doStartServer()
       {
  -             // Stop Resin server
  -             if (m_Server != null) {
  -                     Method closeMethod = m_Server.getClass().getMethod("close", 
null);
  -                     closeMethod.invoke(m_Server, null);
  -             }
  -     }
  +        try {
  +            Class resinClass =
  +                Class.forName("com.caucho.server.http.ResinServer");
  +            Constructor constructor = resinClass.getConstructor(
  +                new Class[] {this.args.getClass(), boolean.class});
  +            this.resinServer = constructor.newInstance(
  +                new Object[] {this.args, new Boolean(true)});
  +            Method initMethod = resinClass.getMethod("init",
  +                new Class[] {boolean.class});
  +            initMethod.invoke(this.resinServer, new Object[] {new Boolean(true)});
  +        } catch (Exception e) {
  +            e.printStackTrace();
  +            throw new RuntimeException("Cannot create instance of ResinServer");
  +        }
  +    }
  +
  +    /**
  +     * Stops the Resin server. We use reflection so that the Resin jars do not
  +     * need to be in the classpath to compile this class.
  +     */
  +     protected void doStopServer()
  +    {
  +        try {
  +            Method closeMethod =
  +                this.resinServer.getClass().getMethod("close", null);
  +            closeMethod.invoke(this.resinServer, null);
  +        } catch (Exception e) {
  +            e.printStackTrace();
  +            throw new RuntimeException("Cannot stop running instance of " +
  +                "ResinServer");
  +        }
  +    }
   
   }
  
  
  

Reply via email to