Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 355   Methods: 13
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IPlanetEjbcTask.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.ejb;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.IOException;
 59   
 import javax.xml.parsers.ParserConfigurationException;
 60   
 import javax.xml.parsers.SAXParser;
 61   
 import javax.xml.parsers.SAXParserFactory;
 62   
 import org.apache.tools.ant.BuildException;
 63   
 import org.apache.tools.ant.Task;
 64   
 import org.apache.tools.ant.types.Path;
 65   
 import org.xml.sax.SAXException;
 66   
 
 67   
 /**
 68   
  * Compiles EJB stubs and skeletons for the iPlanet Application Server.
 69   
  * The EJBs to be processed are specified by the EJB 1.1 standard XML
 70   
  * descriptor, and additional attributes are obtained from the iPlanet Application
 71   
  * Server-specific XML descriptor.  Since the XML descriptors can include
 72   
  * multiple EJBs, this is a convenient way of specifying many EJBs in a single
 73   
  * Ant task.  The following attributes are allowed:
 74   
  *   <ul>
 75   
  *     <li><i>ejbdescriptor</i> -- Standard EJB 1.1 XML descriptor (typically
 76   
  *                                 titled "ejb-jar.xml").  This attribute is
 77   
  *                                 required.
 78   
  *     <li><i>iasdescriptor</i> -- EJB XML descriptor for iPlanet Application
 79   
  *                                 Server (typically titled "ias-ejb-jar.xml).
 80   
  *                                 This attribute is required.
 81   
  *     <li><i>dest</i> -- The is the base directory where the RMI stubs and
 82   
  *                        skeletons are written.  In addition, the class files
 83   
  *                        for each bean (home interface, remote interface, and
 84   
  *                        EJB implementation) must be found in this directory.
 85   
  *                        This attribute is required.
 86   
  *     <li><i>classpath</i> -- The classpath used when generating EJB stubs and
 87   
  *                             skeletons.  This is an optional attribute (if
 88   
  *                             omitted, the classpath specified when Ant was
 89   
  *                             started will be used).  Nested "classpath"
 90   
  *                             elements may also be used.
 91   
  *     <li><i>keepgenerated</i> -- Indicates whether or not the Java source
 92   
  *                                 files which are generated by ejbc will be
 93   
  *                                 saved or automatically deleted.  If "yes",
 94   
  *                                 the source files will be retained.  This is
 95   
  *                                 an optional attribute (if omitted, it
 96   
  *                                 defaults to "no").
 97   
  *     <li><i>debug</i> -- Indicates whether or not the ejbc utility should
 98   
  *                         log additional debugging statements to the standard
 99   
  *                         output.  If "yes", the additional debugging statements
 100   
  *                         will be generated (if omitted, it defaults to "no").
 101   
  *     <li><i>iashome</i> -- May be used to specify the "home" directory for
 102   
  *                           this iPlanet Application Server installation.  This
 103   
  *                           is used to find the ejbc utility if it isn't
 104   
  *                           included in the user's system path.  This is an
 105   
  *                           optional attribute (if specified, it should refer
 106   
  *                           to the <code>[install-location]/iplanet/ias6/ias
 107   
  *                           </code> directory).  If omitted, the ejbc utility
 108   
  *                           must be on the user's system path.
 109   
  *   </ul>
 110   
  * <p>
 111   
  * For each EJB specified, this task will locate the three classes that comprise
 112   
  * the EJB.  If these class files cannot be located in the <code>dest</code>
 113   
  * directory, the task will fail.  The task will also attempt to locate the EJB
 114   
  * stubs and skeletons in this directory.  If found, the timestamps on the
 115   
  * stubs and skeletons will be checked to ensure they are up to date.  Only if
 116   
  * these files cannot be found or if they are out of date will ejbc be called
 117   
  * to generate new stubs and skeletons.
 118   
  *
 119   
  * @see    IPlanetEjbc
 120   
  * @author Greg Nelson <a href="mailto:greg@netscape.com">greg@netscape.com</a>
 121   
  *
 122   
  * @ant.task name="iplanet-ejbc" category="ejb"
 123   
  */
 124   
 public class IPlanetEjbcTask extends Task {
 125   
 
 126   
     /* Attributes set by the Ant build file */
 127   
     private File    ejbdescriptor;
 128   
     private File    iasdescriptor;
 129   
     private File    dest;
 130   
     private Path    classpath;
 131   
     private boolean keepgenerated = false;
 132   
     private boolean debug         = false;
 133   
     private File    iashome;
 134   
 
 135   
     /**
 136   
      * Sets the location of the standard XML EJB descriptor.  Typically, this
 137   
      * file is named "ejb-jar.xml".
 138   
      *
 139   
      * @param ejbdescriptor The name and location of the EJB descriptor.
 140   
      */
 141  0
     public void setEjbdescriptor(File ejbdescriptor) {
 142  0
         this.ejbdescriptor = ejbdescriptor;
 143   
     }
 144   
 
 145   
     /**
 146   
      * Sets the location of the iAS-specific XML EJB descriptor.  Typically,
 147   
      * this file is named "ias-ejb-jar.xml".
 148   
      *
 149   
      * @param iasdescriptor The name and location of the iAS-specific EJB
 150   
      *                      descriptor.
 151   
      */
 152  0
     public void setIasdescriptor (File iasdescriptor) {
 153  0
         this.iasdescriptor = iasdescriptor;
 154   
     }
 155   
 
 156   
     /**
 157   
      * Sets the destination directory where the EJB source classes must exist
 158   
      * and where the stubs and skeletons will be written.  The destination
 159   
      * directory must exist before this task is executed.
 160   
      *
 161   
      * @param dest The directory where the compiled classes will be written.
 162   
      */
 163  0
     public void setDest(File dest) {
 164  0
         this.dest = dest;
 165   
     }
 166   
 
 167   
     /**
 168   
      * Sets the classpath to be used when compiling the EJB stubs and skeletons.
 169   
      *
 170   
      * @param classpath The classpath to be used.
 171   
      */
 172  0
     public void setClasspath(Path classpath) {
 173  0
         if (this.classpath == null) {
 174  0
             this.classpath = classpath;
 175   
         } else {
 176  0
             this.classpath.append(classpath);
 177   
         }
 178   
     }
 179   
 
 180   
     /**
 181   
      * Adds to the classpath used when compiling the EJB stubs and skeletons.
 182   
      */
 183  0
     public Path createClasspath() {
 184  0
         if (classpath == null) {
 185  0
             classpath = new Path(getProject());
 186   
         }
 187  0
         return classpath.createPath();
 188   
     }
 189   
 
 190   
     /**
 191   
      * If true, the Java source files which are generated by ejbc will be saved .
 192   
      *
 193   
      * @param keepgenerated A boolean indicating if the Java source files for
 194   
      *                      the stubs and skeletons should be retained.
 195   
      */
 196  0
     public void setKeepgenerated(boolean keepgenerated) {
 197  0
         this.keepgenerated = keepgenerated;
 198   
     }
 199   
 
 200   
     /**
 201   
      * If true, debugging output will be generated when ejbc is
 202   
      * executed.
 203   
      *
 204   
      * @param debug A boolean indicating if debugging output should be generated
 205   
      */
 206  0
     public void setDebug(boolean debug) {
 207  0
         this.debug = debug;
 208   
     }
 209   
 
 210   
     /**
 211   
      * May be used to specify the "home" directory for this iAS installation.
 212   
      * The directory specified should typically be
 213   
      * <code>[install-location]/iplanet/ias6/ias</code>.
 214   
      *
 215   
      * @param iashome The home directory for the user's iAS installation.
 216   
      */
 217  0
     public void setIashome(File iashome) {
 218  0
         this.iashome = iashome;
 219   
     }
 220   
 
 221   
     /**
 222   
      * Does the work.
 223   
      */
 224  0
     public void execute() throws BuildException {
 225  0
         checkConfiguration();
 226   
 
 227  0
         executeEjbc(getParser());
 228   
     }
 229   
 
 230   
     /**
 231   
      * Verifies that the user selections are valid.
 232   
      *
 233   
      * @throws BuildException If the user selections are invalid.
 234   
      */
 235  0
     private void checkConfiguration() throws BuildException {
 236   
 
 237  0
         if (ejbdescriptor == null) {
 238  0
             String msg = "The standard EJB descriptor must be specified using "
 239   
                             + "the \"ejbdescriptor\" attribute.";
 240  0
             throw new BuildException(msg, getLocation());
 241   
         }
 242  0
         if ((!ejbdescriptor.exists()) || (!ejbdescriptor.isFile())) {
 243  0
             String msg = "The standard EJB descriptor (" + ejbdescriptor
 244   
                             + ") was not found or isn't a file.";
 245  0
             throw new BuildException(msg, getLocation());
 246   
         }
 247   
 
 248  0
         if (iasdescriptor == null) {
 249  0
             String msg = "The iAS-speific XML descriptor must be specified using"
 250   
                             + " the \"iasdescriptor\" attribute.";
 251  0
             throw new BuildException(msg, getLocation());
 252   
         }
 253  0
         if ((!iasdescriptor.exists()) || (!iasdescriptor.isFile())) {
 254  0
             String msg = "The iAS-specific XML descriptor (" + iasdescriptor
 255   
                             + ") was not found or isn't a file.";
 256  0
             throw new BuildException(msg, getLocation());
 257   
         }
 258   
 
 259  0
         if (dest == null) {
 260  0
             String msg = "The destination directory must be specified using "
 261   
                             + "the \"dest\" attribute.";
 262  0
             throw new BuildException(msg, getLocation());
 263   
         }
 264  0
         if ((!dest.exists()) || (!dest.isDirectory())) {
 265  0
             String msg = "The destination directory (" + dest + ") was not "
 266   
                             + "found or isn't a directory.";
 267  0
             throw new BuildException(msg, getLocation());
 268   
         }
 269   
 
 270  0
         if ((iashome != null) && (!iashome.isDirectory())) {
 271  0
             String msg = "If \"iashome\" is specified, it must be a valid "
 272   
                             + "directory (it was set to " + iashome + ").";
 273  0
             throw new BuildException(msg, getLocation());
 274   
         }
 275   
     }
 276   
 
 277   
     /**
 278   
      * Returns a SAXParser that may be used to process the XML descriptors.
 279   
      *
 280   
      * @return Parser which may be used to process the EJB descriptors.
 281   
      * @throws BuildException If the parser cannot be created or configured.
 282   
      */
 283  0
     private SAXParser getParser() throws BuildException {
 284   
 
 285  0
         SAXParser saxParser = null;
 286  0
         try {
 287  0
             SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
 288  0
             saxParserFactory.setValidating(true);
 289  0
             saxParser = saxParserFactory.newSAXParser();
 290   
         } catch (SAXException e) {
 291  0
             String msg = "Unable to create a SAXParser: " + e.getMessage();
 292  0
             throw new BuildException(msg, e, getLocation());
 293   
         } catch (ParserConfigurationException e) {
 294  0
             String msg = "Unable to create a SAXParser: " + e.getMessage();
 295  0
             throw new BuildException(msg, e, getLocation());
 296   
         }
 297   
 
 298  0
         return saxParser;
 299   
     }
 300   
 
 301   
     /**
 302   
      * Executes the EJBc utility using the SAXParser provided.
 303   
      *
 304   
      * @param saxParser SAXParser that may be used to process the EJB
 305   
      *                  descriptors
 306   
      * @throws BuildException If there is an error reading or parsing the XML
 307   
      *                        descriptors
 308   
      */
 309  0
     private void executeEjbc(SAXParser saxParser) throws BuildException {
 310  0
         IPlanetEjbc ejbc = new IPlanetEjbc(ejbdescriptor,
 311   
                                             iasdescriptor,
 312   
                                             dest,
 313   
                                             getClasspath().toString(),
 314   
                                             saxParser);
 315  0
         ejbc.setRetainSource(keepgenerated);
 316  0
         ejbc.setDebugOutput(debug);
 317  0
         if (iashome != null) {
 318  0
             ejbc.setIasHomeDir(iashome);
 319   
         }
 320   
 
 321  0
         try {
 322  0
             ejbc.execute();
 323   
         } catch (IOException e) {
 324  0
             String msg = "An IOException occurred while trying to read the XML "
 325   
                             + "descriptor file: " + e.getMessage();
 326  0
             throw new BuildException(msg, e, getLocation());
 327   
         } catch (SAXException e) {
 328  0
             String msg = "A SAXException occurred while trying to read the XML "
 329   
                             + "descriptor file: " + e.getMessage();
 330  0
             throw new BuildException(msg, e, getLocation());
 331   
         } catch (IPlanetEjbc.EjbcException e) {
 332  0
             String msg = "An exception occurred while trying to run the ejbc "
 333   
                             + "utility: " + e.getMessage();
 334  0
             throw new BuildException(msg, e, getLocation());
 335   
         }
 336   
     }
 337   
 
 338   
     /**
 339   
      * Returns the CLASSPATH to be used when calling EJBc.  If no user CLASSPATH
 340   
      * is specified, the System classpath is returned instead.
 341   
      *
 342   
      * @return Path The classpath to be used for EJBc.
 343   
      */
 344  0
     private Path getClasspath() {
 345  0
         Path cp = null;
 346  0
         if (classpath == null) {
 347  0
             cp = (new Path(getProject())).concatSystemClasspath("last");
 348   
         } else {
 349  0
             cp = classpath.concatSystemClasspath("ignore");
 350   
         }
 351   
 
 352  0
         return cp;
 353   
     }
 354   
 }
 355