You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2002/10/30 15:15:02 UTC

cvs commit: jakarta-cactus/Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/ant GenericAntProvider.java ContainerHome.java StartServerHelper.java

vmassol     2002/10/30 06:15:02

  Added:       Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/ant
                        GenericAntProvider.java ContainerHome.java
                        StartServerHelper.java
  Removed:     Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers
                        ContainerHome.java StartServerHelper.java
                        GenericAntProvider.java
  Log:
  Moved the generic ant implementation to its own package
  
  Revision  Changes    Path
  1.1                  jakarta-cactus/Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/ant/GenericAntProvider.java
  
  Index: GenericAntProvider.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 apache@apache.org.
   *
   * 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.ant;
  
  import java.io.File;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.Vector;
  
  import org.apache.cactus.eclipse.containers.ContainerInfo;
  import org.apache.cactus.eclipse.containers.Credential;
  import org.apache.cactus.eclipse.containers.IContainerProvider;
  import org.apache.cactus.eclipse.ui.CactusPlugin;
  import org.eclipse.ant.core.AntRunner;
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.IStatus;
  import org.eclipse.core.runtime.Path;
  import org.eclipse.core.runtime.Status;
  
  /**
   * Implementation of a container provider that uses ant scripts to set up
   * and launch containers.
   * 
   * @author <a href="mailto:jruaux@octo.com">Julien Ruaux</a>
   * 
   * @version $Id: $
   */
  public class GenericAntProvider implements IContainerProvider
  {
      /**
       * the arguments given to the AntRunner
       */
      private Vector antArguments;
      /**
       * location of the Ant build file to be used 
       */
      private File buildFileLocation;
      /**
       * the port that will be used when setting up the container
       */
      private int port;
      /**
       * the context path for the test web app
       */
      private String contextPath = null;
  
      /**
       * Constructor.
       * @param thePort the port that will be used when setting up the container
       * @param theTargetDir the directory to be used for container configuration
       */
      public GenericAntProvider(int thePort, String theTargetDir)
      {
          port = thePort;
          ContainerHome[] containerDirs =
              {
                   new ContainerHome(
                      "tomcat.home.40",
                      "D:/dev/jakarta-tomcat-4.1.12")};
          antArguments = new Vector();
          antArguments.add("-Dtest.port=" + thePort);
          for (int i = 0; i < containerDirs.length; i++)
          {
              ContainerHome currentContainerHome = containerDirs[i];
              antArguments.add(
                  "-D"
                      + currentContainerHome.getTarget()
                      + "="
                      + currentContainerHome.getDirectory());
          }
          CactusPlugin thePlugin = CactusPlugin.getDefault();
          File antFilesLocation =
              new File(thePlugin.find(new Path("./ant")).getPath());
          buildFileLocation =
              new File(
                  thePlugin.find(new Path("./ant/build/build.xml")).getPath());
          antArguments.add("-Dbase.dir=" + antFilesLocation.getAbsolutePath());
          antArguments.add("-Dtarget.dir=" + theTargetDir);
      }
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#start(org.apache.cactus.eclipse.containers.ContainerInfo)
       */
      public void start(ContainerInfo theContainerInfo) throws CoreException
      {
          String[] targets = { "start.all" };
          AntRunner runner = createAntRunner(targets);
          StartServerHelper startHelper = new StartServerHelper(runner);
          URL testURL = null;
          try
          {
              testURL =
                  new URL(
                      "http://localhost:"
                          + port
                          + "/"
                          + contextPath
                          + "/ServletRedirector"
                          + "?Cactus_Service=RUN_TEST");
          }
          catch (MalformedURLException e)
          {
              throw new CoreException(
                  new Status(
                      IStatus.ERROR,
                      CactusPlugin.getPluginId(),
                      IStatus.OK,
                      e.getMessage(),
                      e));
          }
          startHelper.setTestURL(testURL);
          startHelper.execute();
      }
  
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#deploy(java.lang.String, java.net.URL, org.apache.cactus.eclipse.containers.Credential)
       */
      public void deploy(
          String theContextPath,
          URL theDeployableObject,
          Credential theCredentials)
          throws CoreException
      {
          contextPath = theContextPath;
          String warPath = theDeployableObject.getPath();
          antArguments.add("-Dwar.path=" + warPath);
          antArguments.add("-Dcontext.path=" + theContextPath);
          String[] targets = { "prepare.all" };
          createAntRunner(targets).run();
  
      }
  
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#undeploy(java.lang.String, org.apache.cactus.eclipse.containers.Credential)
       */
      public void undeploy(String theContextPath, Credential theCredentials)
          throws CoreException
      {
          String[] targets = { "clean" };
          createAntRunner(targets).run();
      }
  
      /**
       * @see org.apache.cactus.eclipse.containers.IContainerProvider#stop(org.apache.cactus.eclipse.containers.ContainerInfo)
       */
      public void stop(ContainerInfo theContainerInfo) throws CoreException
      {
          String[] targets = { "stop.all" };
          createAntRunner(targets).run();
      }
  
      /**
       * returns an AntRunner for this provider.
       * @param theTargets the ant target to be called (in that order)
       * @return the AntRunner for the script
       */
      private AntRunner createAntRunner(String[] theTargets)
      {
          AntRunner runner = new AntRunner();
          runner.setBuildFileLocation(buildFileLocation.getAbsolutePath());
          runner.setArguments((String[]) antArguments.toArray(new String[0]));
          runner.setExecutionTargets(theTargets);
          return runner;
      }
  }
  
  
  
  1.1                  jakarta-cactus/Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/ant/ContainerHome.java
  
  Index: ContainerHome.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 apache@apache.org.
   *
   * 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.ant;
  
  /**
   * Class representing a target/directory couple.
   * 
   * @author <a href="mailto:jruaux@octo.com">Julien Ruaux</a>
   * 
   * @version $Id: $
   */
  public class ContainerHome
  {
      /**
       * the target in the ant build (e.g. "tomcat.home.40") 
       */
      private String target;
      /**
       * path to the container home (e.g. "c:/jakarta/tomcat40")
       */
      private String directory;
  
      /**
       * Constructor.
       * @param theTarget the target in the ant build (e.g. "tomcat.home.40")
       * @param theDirectory path to the container home
       *                      (e.g. "c:/jakarta/tomcat40")
       */
      public ContainerHome(String theTarget, String theDirectory)
      {
          target = theTarget;
          directory = theDirectory;
      }
  
      /**
       * Returns the target.
       * @return String the target
       */
      public String getTarget()
      {
          return target;
      }
  
      /**
       * Returns the directory.
       * @return String the directory
       */
      public String getDirectory()
      {
          return directory;
      }
  }
  
  
  
  1.1                  jakarta-cactus/Eclipse-Plugin/src/java/org/apache/cactus/eclipse/containers/ant/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 apache@apache.org.
   *
   * 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.ant;
  
  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>
   * @author <a href="mailto:jruaux@octo.com">Julien Ruaux</a>
   *
   * @version $Id: $
   */
  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)
          {
              e.printStackTrace();
              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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>