jruaux      2002/10/24 05:36:07

  Added:       Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers
                        Tomcat40AntContainerProvider.java
                        IContainerProvider.java StartServerHelper.java
  Log:
  Prototype for Ant script integration. Feel free to comment on this !
  
  Revision  Changes    Path
  1.1                  
jakarta-cactus/Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/Tomcat40AntContainerProvider.java
  
  Index: Tomcat40AntContainerProvider.java
  ===================================================================
  package org.apache.cactus.eclipse.containers;
  
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.Vector;
  import org.eclipse.ant.core.AntRunner;
  import org.eclipse.core.runtime.CoreException;
  
  /**
   * @author Julien Ruaux
   *
   * To change this generated comment edit the template variable "typecomment":
   * Window>Preferences>Java>Templates.
   * To enable and disable the creation of type comments go to
   * Window>Preferences>Java>Code Generation.
   */
  public class Tomcat40AntContainerProvider implements IContainerProvider
  {
      /**
       * Properties passed to the ant script. 
       */
      private String[] antArguments;
  
      /**
       * Location of the build.xml file. 
       */
      private String buildFileLocation;
  
      /**
       * Constructor for Tomcat40AntContainerProvider. 
       */
      public Tomcat40AntContainerProvider()
      {
  //        URL buildFileURL =
  //            CactusPlugin.getDefault().find(new Path("ant/build-share.xml"));
  //        runner.setBuildFileLocation(buildFileURL.getPath());
          buildFileLocation =
              "D:/dev/eclipse/workspace/Eclipse-Plugin/ant/build/build.xml";
          // Properties passed as arguments to Ant :
          // - target.dir (dir where the build will be done
          //         (e.g. the war file) (sort of temp dir))
          // - base.dir (dir where the build (ant scripts) and conf dirs are)
          // - src.dir (java cactus test files)         
          Vector arguments = new Vector();
          arguments.add("-Dtarget.dir=D:/temp/antTemp");
          arguments.add("-Dtest.port=8081");
          arguments.add("-Dbase.dir=D:/dev/eclipse/workspace/Eclipse-Plugin/ant");
          arguments.add("-Dsrc.dir=D:/dev/eclipse/runtime-workspace/cactus-test");
          arguments.add("-Dtomcat.home.40=D:/dev/jakarta-tomcat-4.1.12");
          arguments.add(
              
"-Dcactus.framework.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/cactus-1.4b1.jar");
          arguments.add(
              
"-Dcactus.ant.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/cactus-ant-1.4b1.jar");
          arguments.add(
              
"-Dservlet.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/servletapi-2.3.jar");
          arguments.add(
              
"-Daspectjrt.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/aspectjrt-1.0.5.jar");
          arguments.add(
              
"-Dcommons.logging.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/commons-logging-1.0.jar");
          arguments.add(
              "-Dlog4j.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/log4j-1.2.5.jar");
          arguments.add(
              
"-Dcommons.httpclient.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/commons-httpclient-2.0alpha1-20020720.jar");
          arguments.add(
              "-Djunit.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/junit-3.7.jar");
          arguments.add(
              
"-Dhttpunit.jar=D:/dev/cactus/jakarta-cactus-13-1.4b1/lib/httpunit-1.4.1.jar");
          antArguments = (String[]) arguments.toArray(new String[0]);
      }
  
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#deploy()
       */
      public void deploy() throws CoreException
      {
          String[] targets = { "prepare.test.tomcat.40" };
          createAntRunner(targets).run();
      }
  
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#start()
       */
      public void start() throws CoreException
      {
          String[] targets = { "start.tomcat.40" };
          AntRunner runner = createAntRunner(targets);
          StartServerHelper startHelper = new StartServerHelper(runner);
          URL testURL = null;
          try
          {
              testURL =
                  new 
URL("http://localhost:8081/test/ServletRedirector?Cactus_Service=RUN_TEST";);
          }
          catch (MalformedURLException e)
          {
          }
          startHelper.setTestURL(testURL);
          startHelper.execute();
      }
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#undeploy()
       */
      public void undeploy() throws CoreException
      {
          String[] targets = { "clean" };
          createAntRunner(targets).run();
      }
  
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#stop()
       */
      public void stop() throws CoreException
      {
          String[] targets = { "stop.tomcat.40" };
          createAntRunner(targets).run();
      }
  
      /**
       * returns an AntRunner for this provider.
       * @param targets the ant target to be called
       * @return AntRunner the AntRunner for the script
       */
      private AntRunner createAntRunner(String[] targets)
      {
          AntRunner runner = new AntRunner();
          runner.setBuildFileLocation(buildFileLocation);
          runner.setArguments(antArguments);
          runner.setExecutionTargets(targets);
          return runner;
      }
  
  }
  
  
  
  1.1                  
jakarta-cactus/Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/IContainerProvider.java
  
  Index: IContainerProvider.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
   *    Foundation" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.cactus.eclipse.containers;
  
  import org.eclipse.core.runtime.CoreException;
  
  /**
   * Interface for container configuration and startup.
   * 
   * @version $Id: $
   * @author <a href="mailto:jruaux@;octo.com">Julien Ruaux</a>
   */
  public interface IContainerProvider
  {
      /**
       * Starts the container.
       * @throws CoreException when starting fails
       */
      void start() throws CoreException;
  
      /**
       * Deploy a webapp to the container.
       * @throws CoreException when deployment fails 
       */
      void deploy() throws CoreException;
      /**
       * UnDeploy a webapp to the container.
       * @throws CoreException when undeployment fails
       */    
      void undeploy() throws CoreException;
      /**
       * Stops the container.
       * @throws CoreException when stopping fails
       */    
      void stop() throws CoreException;
  }
  
  
  
  1.1                  
jakarta-cactus/Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/StartServerHelper.java
  
  Index: StartServerHelper.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
   *    Foundation" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.cactus.eclipse.containers;
  
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  import org.apache.tools.ant.BuildException;
  import org.eclipse.ant.core.AntRunner;
  import org.eclipse.core.runtime.CoreException;
  
  /**
   * A helper class for an Ant Task that does the following :
   * <ul>
   *   <li>create a java thread,</li>
   *   <li>start another Ant target in that thread. This target must be a
   *       blocking target that starts a web server/servlet engine,</li>
   *   <li>wait for that server to be started. This is done by continuously
   *       trying to call a URL.</li>
   * </ul>.
   *
   * @author <a href="mailto:vmassol@;apache.org">Vincent Massol</a>
   *
   * @version $Id: StartServerHelper.java,v 1.8 2002/09/07 16:51:43 vmassol Exp $
   */
  public class StartServerHelper implements Runnable
  {
      /**
       * The URL that is continuously pinged to verify if the server is running.
       */
      private URL testURL;    
  
      /**
       * The tasks that wraps around this helper class
       */
      private AntRunner runner;
  
      /**
       * True if the server was already started when this task is executed.
       */
      private boolean isServerAlreadyStarted = false;
  
      /**
       * @param theRunner the Ant runner that this helper is calling
       */
      public StartServerHelper(AntRunner theRunner)
      {
          this.runner = theRunner;
      }
  
      /**
       * @return true if the server has already been started.
       */
      public boolean isServerAlreadyStarted()
      {
          return this.isServerAlreadyStarted;
      }
  
      /**
       * @see Task#execute()
       */
      public void execute() throws BuildException
      {
          // Verify that a test URL has been specified
          if (this.testURL == null)
          {
              throw new BuildException("A testURL attribute must be specified");
          }
  
          // Try connecting in case the server is already running. If so, does
          // nothing
          if (isURLCallable())
          {
              // Server is already running. Record this information so that we
              // don't stop it afterwards.
              this.isServerAlreadyStarted = true;
  
              return;
          }
          else
          {
          }
  
          // Call the target that starts the server, in another thread. The called
          // target must be blocking.
          Thread thread = new Thread(this);
  
          thread.start();
  
          // Wait a few ms more (just to make sure the servlet engine is
          // ready to accept connections)
          sleep(1000);
  
          // Continuously try calling the test URL until it succeeds
          while (true)
          {
  
              if (!isURLCallable())
              {
                  sleep(500);
  
                  continue;
              }
  
              break;
          }
  
          // Wait a few ms more (just to be sure !)
          sleep(500);
  
          // 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(theMs);
          }
          catch (InterruptedException e)
          {
              throw new BuildException("Interruption during sleep", e);
          }
      }
  
      /**
       * @return true if the test URL could be called without error or false
       *         otherwise
       */
      private boolean isURLCallable()
      {
          boolean isURLCallable = false;
  
          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();
          }
  
          return isURLCallable;
      }
  
      /**
       * Fully reads the input stream from the passed HTTP URL connection to
       * prevent (harmless) server-side exception.
       *
       * @param theConnection the HTTP URL connection to read from
       * @exception IOException if an error happens during the read
       */
      static void readFully(HttpURLConnection theConnection) throws IOException
      {
          // Only read if there is data to read ... The problem is that not
          // all servers return a content-length header. If there is no header
          // getContentLength() returns -1. It seems to work and it seems
          // that all servers that return no content-length header also do
          // not block on read() operations !
          if (theConnection.getContentLength() != 0)
          {
              byte[] buf = new byte[256];
  
              InputStream is = theConnection.getInputStream();
  
              while (-1 != is.read(buf))
              {
                  // Make sure we read all the data in the stream
              }
          }
      }
  
      /**
       * The thread that calls the Ant target that starts the web server/servlet
       * engine. Must be a blocking target.
       */
      public void run()
      {
          // Call the AntRunner .
          try
          {
              runner.run();
          }
          catch (CoreException e)
          {
              throw new BuildException("Error while running the Ant task");
          }
  
          // Should never reach this point as the target is blocking, unless the
          // server is stopped.
      }
  
      /**
       * @param theTestURL the test URL to ping
       */
      public void setTestURL(URL theTestURL)
      {
          this.testURL = theTestURL;
      }
  }
  
  

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

Reply via email to