2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 332   Methods: 11
NCLOC: 218   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
CactifyEarTask.java 83.3% 89.9% 90.9% 89.1%
coverage coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2005 The Apache Software Foundation.
 5   
  *
 6   
  * Licensed under the Apache License, Version 2.0 (the "License");
 7   
  * you may not use this file except in compliance with the License.
 8   
  * You may obtain a copy of the License at
 9   
  * 
 10   
  *   http://www.apache.org/licenses/LICENSE-2.0
 11   
  * 
 12   
  * Unless required by applicable law or agreed to in writing, software
 13   
  * distributed under the License is distributed on an "AS IS" BASIS,
 14   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   
  * See the License for the specific language governing permissions and
 16   
  * limitations under the License.
 17   
  * 
 18   
  * ========================================================================
 19   
  */
 20   
 package org.apache.cactus.integration.ant;
 21   
 
 22   
 import java.io.File;
 23   
 import java.io.IOException;
 24   
 import java.util.Iterator;
 25   
 
 26   
 import javax.xml.parsers.ParserConfigurationException;
 27   
 
 28   
 import org.apache.tools.ant.BuildException;
 29   
 import org.apache.tools.ant.Project;
 30   
 import org.apache.tools.ant.taskdefs.Ear;
 31   
 import org.apache.tools.ant.types.ZipFileSet;
 32   
 import org.apache.tools.ant.util.FileUtils;
 33   
 import org.codehaus.cargo.module.application.ApplicationXml;
 34   
 import org.codehaus.cargo.module.application.ApplicationXmlIo;
 35   
 import org.codehaus.cargo.module.application.DefaultEarArchive;
 36   
 import org.codehaus.cargo.module.application.EarArchive;
 37   
 import org.codehaus.cargo.module.ejb.EjbArchive;
 38   
 import org.codehaus.cargo.module.ejb.EjbJarXml;
 39   
 import org.codehaus.cargo.module.ejb.Entity;
 40   
 import org.codehaus.cargo.module.ejb.Session;
 41   
 import org.codehaus.cargo.module.ejb.VendorEjbDescriptor;
 42   
 import org.xml.sax.SAXException;
 43   
 
 44   
 /**
 45   
  * An Ant task that injects elements necessary to run Cactus tests into an
 46   
  * existing EAR file.
 47   
  * 
 48   
  * @version $Id: CactifyEarTask.java 239173 2005-05-24 12:02:31Z grimsell $
 49   
  */
 50   
 public class CactifyEarTask extends Ear
 51   
 {
 52   
     /**
 53   
      * Cactus war configuration holder
 54   
      */
 55   
     private CactusWar cactusWar;
 56   
     
 57   
     /**
 58   
      * The archive that contains the web-app that should be cactified.
 59   
      */
 60   
     private File srcFile;
 61   
 
 62   
     /**
 63   
      * Indicates whether or not we should add ejb references to local ejbs
 64   
      * in the deployment descriptor.
 65   
      */
 66   
     private boolean addEjbReferences;
 67   
     
 68   
     /**
 69   
      * 
 70   
      * @param theCactusWar CactusWar to set
 71   
      */
 72  2
     public void addConfiguredCactuswar(CactusWar theCactusWar)
 73   
     {
 74  2
         cactusWar = theCactusWar;
 75   
     }
 76   
     
 77   
     /**
 78   
      * @param theSrcFile The srcFile to set.
 79   
      */
 80  3
     public void setSrcFile(File theSrcFile)
 81   
     {
 82  3
         srcFile = theSrcFile;
 83   
     }
 84   
     
 85   
     /**
 86   
      * @return Returns the addEjbReferences.
 87   
      */
 88  0
     public boolean getAddEjbReferences()
 89   
     {
 90  0
         return addEjbReferences;
 91   
     }
 92   
     /**
 93   
      * Indicates whether or not ejb references should be added.
 94   
      * If set to true all local ejbs will be accessible via
 95   
      * java:comp/env/ejb/<EJB_NAME>
 96   
      * 
 97   
      * @param isAddEjbReferences if ejb references should be added.
 98   
      */
 99  1
     public void setAddEjbReferences(boolean isAddEjbReferences)
 100   
     {
 101  1
         this.addEjbReferences = isAddEjbReferences;
 102   
     }
 103   
     
 104   
     /**
 105   
      * @see org.apache.tools.ant.Task#execute()
 106   
      */
 107  3
     public void execute() throws BuildException
 108   
     {
 109  3
         if (cactusWar == null)
 110   
         {
 111  1
             cactusWar = createCactusWarConfig();
 112   
         }
 113   
         //Add everything that's in the source EAR to the destination EAR
 114  3
         ZipFileSet currentFiles = new ZipFileSet();
 115  3
         currentFiles.setSrc(this.srcFile);
 116  3
         currentFiles.createExclude().setName("META-INF/application.xml");
 117  3
         addZipfileset(currentFiles);
 118   
         
 119   
         // cactify the application.xml
 120  3
         ApplicationXml appXml = getOriginalApplicationXml();
 121  3
         File tmpAppXml = cactifyApplicationXml(appXml);
 122  3
         setAppxml(tmpAppXml);
 123   
         
 124   
         // create the cactus war
 125  3
         File cactusWarFile = createCactusWar();
 126  3
         addFileToEar(cactusWarFile, cactusWar.getFileName());
 127   
         
 128  3
         super.execute();
 129   
     }
 130   
     
 131   
     /**
 132   
      * 
 133   
      * @return the application.xml from the source ear
 134   
      */
 135  3
     private ApplicationXml getOriginalApplicationXml()
 136   
     {
 137  3
         ApplicationXml appXml = null;
 138  3
         try
 139   
         {
 140  3
             EarArchive ear = new DefaultEarArchive(this.srcFile);
 141  3
             appXml = ear.getApplicationXml();
 142  3
             if (appXml == null)
 143   
             {
 144  0
                 throw new BuildException("The EAR source file does not "
 145   
                     + "contain a META-INF/application.xml " 
 146   
                     + "deployment descriptor");
 147   
             }
 148   
         }
 149   
         catch (SAXException e)
 150   
         {
 151  0
             throw new BuildException(
 152   
                 "Parsing of application.xml deployment descriptor failed", e);
 153   
         }
 154   
         catch (IOException e)
 155   
         {
 156  0
             throw new BuildException("Failed to open EAR", e);
 157   
         }
 158   
         catch (ParserConfigurationException e)
 159   
         {
 160  0
             throw new BuildException("XML parser configuration error", e);
 161   
         }
 162   
         
 163  3
         return appXml;
 164   
     }
 165   
     
 166   
     /**
 167   
      * 
 168   
      * @param theAppXml the application.xml to cactify
 169   
      * @return the cactified application.xml
 170   
      */
 171  3
     private File cactifyApplicationXml(ApplicationXml theAppXml)
 172   
     {
 173  3
         theAppXml.addWebModule(cactusWar.getFileName(), cactusWar.getContext());
 174   
         // serialize the cactified app xml
 175  3
         FileUtils fileUtils = FileUtils.newFileUtils();
 176  3
         File tmpAppXml = fileUtils.createTempFile("cactus", "application.xml",
 177   
                                                   getProject().getBaseDir());
 178  3
         tmpAppXml.deleteOnExit();
 179  3
         try
 180   
         {
 181  3
             ApplicationXmlIo.writeApplicationXml(theAppXml, 
 182   
                                                  tmpAppXml, 
 183   
                                                  null, true);
 184   
         }
 185   
         catch (IOException ioe)
 186   
         {
 187  0
             throw new BuildException(
 188   
                 "Could not write temporary deployment descriptor", ioe);
 189   
         }
 190  3
         return tmpAppXml;
 191   
     }
 192   
     
 193   
     /**
 194   
      * 
 195   
      * @return the cactus.war
 196   
      */
 197  3
     private File createCactusWar()
 198   
     {
 199  3
         FileUtils fileUtils = FileUtils.newFileUtils();
 200  3
         File tmpCactusWar = fileUtils.createTempFile("cactus", "cactus.war",
 201   
                                                      getProject().getBaseDir());
 202  3
         tmpCactusWar.deleteOnExit();
 203  3
         cactusWar.setDestFile(tmpCactusWar);
 204   
         
 205  3
         if (addEjbReferences)
 206   
         {
 207  1
             addEjbReferencesToWar(tmpCactusWar);
 208   
         }
 209   
         
 210  3
         cactusWar.execute();
 211   
         
 212  3
         return tmpCactusWar;
 213   
     }
 214   
     
 215   
     /**
 216   
      * 
 217   
      * @param theFile the file to add
 218   
      * @param theFullPath the path within the ear
 219   
      */
 220  3
     private void addFileToEar(File theFile, String theFullPath)
 221   
     {
 222  3
         ZipFileSet fs = new ZipFileSet();
 223  3
         fs.setFile(theFile);
 224  3
         fs.setFullpath(theFullPath);
 225  3
         addZipfileset(fs);
 226   
     }
 227   
     
 228   
     /**
 229   
      * Initialize cactusWar with some default values.
 230   
      *
 231   
      * @return the CactusWar configuration
 232   
      */
 233  1
     private CactusWar createCactusWarConfig()
 234   
     {
 235  1
         CactusWar cactusWarConfig = new CactusWar();
 236  1
         CactusWar.Version version = new CactusWar.Version();
 237  1
         version.setValue("2.3");
 238  1
         cactusWarConfig.setVersion(version);
 239  1
         cactusWarConfig.setContext("/cactus");
 240  1
         cactusWarConfig.setProject(getProject());
 241   
         
 242  1
         return cactusWarConfig;
 243   
     }
 244   
 
 245   
     /**
 246   
      * Add ejb references.
 247   
      * 
 248   
      * @param theWar temporary cactus war
 249   
      */
 250  1
     private void addEjbReferencesToWar(File theWar) 
 251   
     {
 252  1
         try
 253   
         {
 254  1
             EarArchive ear = new DefaultEarArchive(srcFile);
 255  1
             ApplicationXml appXml = ear.getApplicationXml();
 256  1
             Iterator ejbModules = appXml.getEjbModules();
 257  1
             while (ejbModules.hasNext())
 258   
             {
 259  1
                 String module = (String) ejbModules.next();
 260  1
                 EjbArchive ejbArchive = ear.getEjbModule(module);
 261  1
                 EjbJarXml descr = ejbArchive.getEjbJarXml();
 262  1
                 VendorEjbDescriptor vendorDescr = descr.getVendorDescriptor();
 263  1
                 if (vendorDescr == null)
 264   
                 {
 265  0
                     throw new BuildException("Failed to find vendor " 
 266   
                                              + "deployment descriptor " 
 267   
                                              + "for ejb jar " + module);
 268   
                 }
 269  1
                 Iterator ejbs = descr.getSessionEjbs();
 270  1
                 while (ejbs.hasNext())
 271   
                 {
 272  2
                     Session ejb = (Session) ejbs.next();
 273  2
                     String name = ejb.getName();
 274  2
                     String local = ejb.getLocal();
 275  2
                     String localHome = ejb.getLocalHome();
 276  2
                     if (local != null)
 277   
                     {
 278  1
                         log("Adding ejb-ref for local session ejb "
 279   
                             + ejb.getName(),
 280   
                             Project.MSG_VERBOSE);
 281  1
                         CactifyWarTask.EjbRef ref = new CactifyWarTask.EjbRef();
 282  1
                         ref.setType("Session");
 283  1
                         ref.setName("ejb/" + name);
 284  1
                         ref.setLocalInterface(local);
 285  1
                         ref.setLocalHomeInterface(localHome);
 286   
                         
 287  1
                         String jndiName = vendorDescr.getJndiName(ejb);
 288  1
                         ref.setJndiName(jndiName);
 289  1
                         cactusWar.addConfiguredEjbref(ref);
 290   
                     }
 291   
                 }
 292  1
                 ejbs = descr.getEntityEjbs();
 293  1
                 while (ejbs.hasNext())
 294   
                 {
 295  1
                     Entity ejb = (Entity) ejbs.next();
 296  1
                     String name = ejb.getName();
 297  1
                     String local = ejb.getLocal();
 298  1
                     String localHome = ejb.getLocalHome();
 299  1
                     if (local != null)
 300   
                     {
 301  1
                         log("Adding ejb-ref for local entity ejb "
 302   
                             + ejb.getName(),
 303   
                             Project.MSG_VERBOSE);
 304  1
                         CactifyWarTask.EjbRef ref = new CactifyWarTask.EjbRef();
 305  1
                         ref.setType("Entity");
 306  1
                         ref.setName("ejb/" + name);
 307  1
                         ref.setLocalInterface(local);
 308  1
                         ref.setLocalHomeInterface(localHome);
 309   
                         
 310  1
                         String jndiName = vendorDescr.getJndiName(ejb);
 311  1
                         ref.setJndiName(jndiName);
 312  1
                         cactusWar.addConfiguredEjbref(ref);
 313   
                     }
 314   
                 }
 315   
             }
 316   
         }
 317   
         catch (IOException e)
 318   
         {
 319  0
             throw new BuildException("Could not merge deployment " 
 320   
                                      + "descriptors", e);
 321   
         }
 322   
         catch (SAXException e)
 323   
         {
 324  0
             throw new BuildException("Parsing of merge file failed", e);
 325   
         }
 326   
         catch (ParserConfigurationException e)
 327   
         {
 328  0
             throw new BuildException("XML parser configuration error", e);
 329   
         }
 330   
     }
 331   
 }
 332