vmassol     02/05/03 12:44:38

  Modified:    anttasks/src/java/org/apache/cactus/ant
                        StartServerHelper.java
               documentation/docs/xdocs changes.xml
  Log:
  Improved debugging of <code>runservertests</code> task. Simply run Ant in debug mode 
(<code>ant -debug xxx</code>) and the task will print information. Very useful to know 
why the <code>runservertests</code> task seems to hang after starting your server ...
  
  Revision  Changes    Path
  1.5       +59 -44    
jakarta-cactus/anttasks/src/java/org/apache/cactus/ant/StartServerHelper.java
  
  Index: StartServerHelper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/anttasks/src/java/org/apache/cactus/ant/StartServerHelper.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- StartServerHelper.java    14 Apr 2002 16:34:15 -0000      1.4
  +++ StartServerHelper.java    3 May 2002 19:44:38 -0000       1.5
  @@ -58,6 +58,9 @@
   
   import java.io.InputStream;
   import java.io.IOException;
  +import java.io.PrintWriter;
  +import java.io.ByteArrayOutputStream;
  +import java.io.PrintStream;
   import java.net.HttpURLConnection;
   import java.net.MalformedURLException;
   import java.net.URL;
  @@ -79,7 +82,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: StartServerHelper.java,v 1.4 2002/04/14 16:34:15 vmassol Exp $
  + * @version $Id: StartServerHelper.java,v 1.5 2002/05/03 19:44:38 vmassol Exp $
    */
   public class StartServerHelper implements Runnable
   {
  @@ -137,26 +140,14 @@
   
           // Try connecting in case the server is already running. If so, does
           // nothing
  -        try {
  -
  -            HttpURLConnection connection =
  -                (HttpURLConnection) this.testURL.openConnection();
  -            connection.connect();
  -            readFully(connection);
  -            connection.disconnect();
  -
  +        if (isURLCallable()) {
               // Server is already running. Record this information so that we
               // don't stop it afterwards.
               this.isServerAlreadyStarted = true;
  -
  -            this.task.log("Server is already running", Project.MSG_VERBOSE);
  -
  +            this.task.log("Server is already running", Project.MSG_DEBUG);
               return;
  -
  -        } catch (IOException e) {
  -            // An error occurred. It just means the server is not running. Do
  -            // nothing
  -            this.task.log("Server is not running", Project.MSG_VERBOSE);
  +        } else {
  +            this.task.log("Server is not running", Project.MSG_DEBUG);
           }
   
           // Call the target that starts the server, in another thread. The called
  @@ -167,53 +158,75 @@
   
           // Wait a few ms more (just to make sure the servlet engine is
           // ready to accept connections)
  -        try {
  -            Thread.sleep(1000);
  -        } catch (InterruptedException e) {
  -            throw new BuildException("Interruption during sleep", e);
  -        }
  +        sleep(1000);
   
           // Continuously try calling the test URL until it succeeds
           while (true) {
   
  -            this.task.log("Checking if server is up ...", Project.MSG_VERBOSE);
  -
  -            try {
  -                HttpURLConnection connection =
  -                    (HttpURLConnection) this.testURL.openConnection();
  -                connection.connect();
  -                readFully(connection);
  -                connection.disconnect();
  -            } catch (IOException e) {
  -
  -                this.task.log("... got error : " + e.getMessage(),
  -                    Project.MSG_VERBOSE);
  -
  -                try {
  -                    Thread.sleep(500);
  -                } catch (InterruptedException ee) {
  -                    throw new BuildException("Interruption during sleep", ee);
  -                }
  +            this.task.log("Checking if server is up ...", Project.MSG_DEBUG);
   
  +            if (!isURLCallable()) {
  +                sleep(500);
                   continue;
               }
   
  -            this.task.log("Server is up !", Project.MSG_VERBOSE);
  +            this.task.log("Server is up !", Project.MSG_DEBUG);
   
               break;
           }
   
           // Wait a few ms more (just to be sure !)
  +        sleep(500);
  +
  +        this.task.log("Server started", Project.MSG_DEBUG);
  +
  +        // We're done ... Ant will continue processing other tasks
  +    }
  +
  +    /**
  +     * Sleeps n milliseconds.
  +     *
  +     * @param theMs the number of milliseconds to wait
  +     * @throws BuildException if the sleeping thread is interrupted
  +     */
  +    private void sleep(int theMs) throws BuildException
  +    {
           try {
  -            Thread.sleep(500);
  +            Thread.sleep(theMs);
           } catch (InterruptedException e) {
               throw new BuildException("Interruption during sleep", e);
           }
  +    }
   
  -        this.task.log("Server started", Project.MSG_VERBOSE);
  +    /**
  +     * @return true if the test URL could be called without error or false
  +     *         otherwise
  +     */
  +    private boolean isURLCallable()
  +    {
  +        boolean isURLCallable = false;
   
  -        // We're done ... Ant will continue processing other tasks
  +        try {
  +            HttpURLConnection connection =
  +                (HttpURLConnection) this.testURL.openConnection();
  +            connection.connect();
  +            readFully(connection);
  +            connection.disconnect();
  +            isURLCallable = true;
  +        } catch (IOException e) {
  +            // Log an information in debug mode
  +
  +            // Get stacktrace text
  +            ByteArrayOutputStream baos = new ByteArrayOutputStream();
  +            PrintWriter writer = new PrintWriter(baos);
  +            e.printStackTrace(writer);
  +            writer.close();
   
  +            this.task.log("Failed to call test URL. Reason :" +
  +                new String(baos.toByteArray()), Project.MSG_DEBUG);
  +        }
  +
  +        return isURLCallable;
       }
   
       /**
  @@ -275,6 +288,8 @@
           } catch (MalformedURLException e) {
               throw new BuildException("Bad URL [" + theTestURL + "]", e);
           }
  +
  +        this.task.log("Test URL = [" + this.testURL + "]", Project.MSG_DEBUG);
       }
   
       /**
  
  
  
  1.12      +7 -0      jakarta-cactus/documentation/docs/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/changes.xml,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- changes.xml       21 Apr 2002 22:41:14 -0000      1.11
  +++ changes.xml       3 May 2002 19:44:38 -0000       1.12
  @@ -48,6 +48,13 @@
       </devs>
   
       <release version="1.4 in CVS">
  +      <action dev="VMA" type="update">
  +        Improved debugging of <code>runservertests</code> task. Simply run
  +        Ant in debug mode (<code>ant -debug xxx</code>) and the task will
  +        print information. Very useful to know why the
  +        <code>runservertests</code> task seems to hang after starting your
  +        server ...
  +      </action>
       </release>
   
       <release version="1.3" date="April 21 2002">
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to