Clover coverage report - Cactus 1.5 for J2EE API 1.2
Coverage timestamp: Wed Feb 18 2004 09:04:33 EST
file stats: LOC: 327   Methods: 10
NCLOC: 147   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JBoss3xContainer.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus.integration.ant.container.jboss;
 58   
 
 59   
 import java.io.File;
 60   
 import java.io.FileNotFoundException;
 61   
 import java.io.IOException;
 62   
 import java.util.jar.Attributes;
 63   
 import java.util.jar.JarFile;
 64   
 import java.util.jar.Manifest;
 65   
 
 66   
 import org.apache.cactus.integration.ant.container.AbstractJavaContainer;
 67   
 import org.apache.tools.ant.BuildException;
 68   
 import org.apache.tools.ant.taskdefs.Java;
 69   
 import org.apache.tools.ant.types.Path;
 70   
 import org.apache.tools.ant.util.FileUtils;
 71   
 
 72   
 /**
 73   
  * Special container support for the JBoss application server.
 74   
  * 
 75   
  * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
 76   
  * 
 77   
  * @version $Id: JBoss3xContainer.java,v 1.6.2.3 2003/10/23 18:20:45 vmassol Exp $
 78   
  */
 79   
 public class JBoss3xContainer extends AbstractJavaContainer
 80   
 {
 81   
     // Instance Variables ------------------------------------------------------
 82   
 
 83   
     /**
 84   
      * The JBoss 3.x installation directory.
 85   
      */
 86   
     private File dir;
 87   
 
 88   
     /**
 89   
      * The name of the server configuration to use for running the tests.
 90   
      */
 91   
     private String config = "default";
 92   
 
 93   
     /**
 94   
      * The port to which the container should be bound.
 95   
      */
 96   
     private transient int port = 8080;
 97   
 
 98   
     /**
 99   
      * The JBoss version detected by reading the Manifest file in the
 100   
      * installation directory.
 101   
      */
 102   
     private transient String version;
 103   
 
 104   
     // Public Methods ----------------------------------------------------------
 105   
 
 106   
     /**
 107   
      * Sets the JBoss installation directory.
 108   
      * 
 109   
      * @param theDir The directory to set
 110   
      * @throws BuildException If the specified directory doesn't contain a valid
 111   
      *         JBoss 3.x installation
 112   
      */
 113  0
     public final void setDir(File theDir) throws BuildException
 114   
     {
 115  0
         this.dir = theDir;
 116   
     }
 117   
 
 118   
     /**
 119   
      * Sets the name of the server configuration to use for running the tests.
 120   
      * 
 121   
      * @param theConfig The configuration name
 122   
      */
 123  0
     public final void setConfig(String theConfig)
 124   
     {
 125  0
         this.config = theConfig;
 126   
     }
 127   
 
 128   
     /**
 129   
      * Sets the port that will be used to poll the server to verify if
 130   
      * it is started. This is needed for the use case where the user
 131   
      * has defined his own JBoss configuration by using the
 132   
      * {@link #setConfig(String)} call and has defined a port other
 133   
      * than the default one. 
 134   
      * 
 135   
      * Note: This value is not yet used to set the port
 136   
      * to which the container will listen to. The reason is that this is
 137   
      * hard to implement with JBoss and nobody had the courage to implement
 138   
      * it yet...
 139   
      * 
 140   
      * @param thePort The port to set
 141   
      */
 142  0
     public final void setPort(int thePort)
 143   
     {
 144  0
         this.port = thePort;
 145   
     }
 146   
 
 147   
     // Container Implementation ------------------------------------------------
 148   
 
 149   
     /**
 150   
      * @see org.apache.cactus.integration.ant.container.Container#getName
 151   
      */
 152  0
     public final String getName()
 153   
     {
 154  0
         return "JBoss " + this.version;
 155   
     }
 156   
 
 157   
     /**
 158   
      * Returns the port to which the container should listen.
 159   
      * 
 160   
      * @return The port
 161   
      */
 162  0
     public final int getPort()
 163   
     {
 164  0
         return this.port;
 165   
     }
 166   
     
 167   
     /**
 168   
      * @see org.apache.cactus.integration.ant.container.Container#init
 169   
      */
 170  0
     public final void init()
 171   
     {
 172   
         // Verify the installation directory
 173  0
         this.version = getVersion(this.dir);
 174  0
         if (this.version == null)
 175   
         {
 176  0
             throw new BuildException(this.dir
 177   
                 + " not recognized as a JBoss 3.x installation");
 178   
         }
 179  0
         if (!this.version.startsWith("3"))
 180   
         {
 181  0
             throw new BuildException(
 182   
                 "This element doesn't support version " + this.version
 183   
                 + " of JBoss");
 184   
         }
 185   
 
 186   
         // TODO: as long as we don't have a way to set the port on the JBoss 
 187   
         // instance, we'll at least need to extract the port from a config file
 188   
         // in the installation directory
 189   
     }
 190   
 
 191   
     /**
 192   
      * @see org.apache.cactus.integration.ant.container.Container#startUp
 193   
      */
 194  0
     public final void startUp()
 195   
     {
 196  0
         try
 197   
         {
 198  0
             prepare("cactus/jboss3x");
 199   
             
 200  0
             File binDir = new File(this.dir, "bin");
 201  0
             File configDir = new File(this.dir, "server");
 202   
             
 203  0
             Java java = createJavaForStartUp();
 204  0
             java.setDir(binDir);
 205   
             
 206  0
             java.addSysproperty(
 207   
                 createSysProperty("program.name",
 208   
                     new File(binDir, "run.bat")));
 209  0
             java.addSysproperty(
 210   
                 createSysProperty("jboss.server.home.dir",
 211   
                     new File(configDir, this.config)));
 212  0
             java.addSysproperty(
 213   
                 createSysProperty("jboss.server.home.url",
 214   
                     new File(configDir, this.config).toURL().toString()));
 215   
 
 216  0
             Path classPath = java.createClasspath();
 217  0
             classPath.createPathElement().setLocation(
 218   
                 new File(binDir, "run.jar"));
 219  0
             try
 220   
             {
 221  0
                 classPath.createPathElement().setLocation(getToolsJar());
 222   
             }
 223   
             catch (FileNotFoundException fnfe)
 224   
             {
 225  0
                 getLog().warn(
 226   
                     "Couldn't find tools.jar (needed for JSP compilation)");
 227   
             }
 228   
 
 229  0
             java.setClassname("org.jboss.Main");
 230  0
             java.createArg().setValue("-c");
 231  0
             java.createArg().setValue(this.config);
 232  0
             java.execute();
 233   
         }
 234   
         catch (IOException ioe)
 235   
         {
 236  0
             getLog().error("Failed to startup the container", ioe);
 237  0
             throw new BuildException(ioe);
 238   
         }
 239   
     }
 240   
 
 241   
     /**
 242   
      * @see org.apache.cactus.integration.ant.container.Container#shutDown
 243   
      */
 244  0
     public final void shutDown()
 245   
     {
 246  0
         File binDir = new File(this.dir, "bin");
 247   
             
 248  0
         Java java = createJavaForShutDown();
 249  0
         java.setFork(true);
 250   
 
 251  0
         Path classPath = java.createClasspath();
 252  0
         classPath.createPathElement().setLocation(
 253   
             new File(binDir, "shutdown.jar"));
 254   
 
 255  0
         java.setClassname("org.jboss.Shutdown");
 256  0
         if (this.version.startsWith("3.2"))
 257   
         {
 258  0
             java.createArg().setValue("--shutdown");
 259   
         }
 260   
         else
 261   
         {
 262  0
             java.createArg().setValue("localhost");
 263  0
             java.createArg().setValue(String.valueOf(getPort()));
 264   
         }
 265  0
         java.execute();
 266   
     }
 267   
 
 268   
     // Private Methods ---------------------------------------------------------
 269   
 
 270   
     /**
 271   
      * Prepares a temporary installation of the container and deploys the 
 272   
      * web-application.
 273   
      * 
 274   
      * @param theDirName The name of the temporary container installation
 275   
      *        directory
 276   
      * @throws IOException If an I/O error occurs
 277   
      */
 278  0
     private void prepare(String theDirName) throws IOException
 279   
     {
 280  0
         FileUtils fileUtils = FileUtils.newFileUtils();
 281   
 
 282   
         // TODO: Find out how to create a valid default server configuration.
 283   
         // Copying the server directory does not seem to be enough
 284   
             
 285   
         // deploy the web-app by copying the WAR file into the webapps
 286   
         // directory
 287  0
         File configDir = new File(this.dir, "server");
 288  0
         File deployDir = new File(configDir, this.config + "/deploy");
 289  0
         fileUtils.copyFile(getDeployableFile().getFile(),
 290   
             new File(deployDir, getDeployableFile().getFile().getName()), 
 291   
             null, true);
 292   
     }
 293   
 
 294   
     /**
 295   
      * Returns the version of the JBoss installation.
 296   
      * 
 297   
      * @param theDir The JBoss installation directory 
 298   
      * @return The JBoss version, or <code>null</code> if the verion number
 299   
      *         could not be retrieved
 300   
      */
 301  0
     private String getVersion(File theDir)
 302   
     {
 303   
         // Extract version information from the manifest in run.jar
 304  0
         String retVal = null;
 305  0
         try
 306   
         {
 307  0
             JarFile runJar = new JarFile(new File(theDir, "bin/run.jar"));
 308  0
             Manifest mf = runJar.getManifest();
 309  0
             if (mf != null)
 310   
             {
 311  0
                 Attributes attrs = mf.getMainAttributes();
 312  0
                 retVal = attrs.getValue(Attributes.Name.SPECIFICATION_VERSION);
 313   
             }
 314   
             else
 315   
             {
 316  0
                 getLog().warn("Couldn't find MANIFEST.MF in " + runJar);
 317   
             }
 318   
         }
 319   
         catch (IOException ioe)
 320   
         {
 321  0
             getLog().warn("Couldn't retrieve JBoss version information", ioe);
 322   
         }
 323  0
         return retVal;
 324   
     }
 325   
 
 326   
 }
 327