EjbJar.java

00001 /*
00002  * The Apache Software License, Version 1.1
00003  *
00004  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
00005  * reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in
00016  *    the documentation and/or other materials provided with the
00017  *    distribution.
00018  *
00019  * 3. The end-user documentation included with the redistribution, if
00020  *    any, must include the following acknowlegement:
00021  *       "This product includes software developed by the
00022  *        Apache Software Foundation (http://www.apache.org/)."
00023  *    Alternately, this acknowlegement may appear in the software itself,
00024  *    if and wherever such third-party acknowlegements normally appear.
00025  *
00026  * 4. The names "Ant" and "Apache Software
00027  *    Foundation" must not be used to endorse or promote products derived
00028  *    from this software without prior written permission. For written
00029  *    permission, please contact apache@apache.org.
00030  *
00031  * 5. Products derived from this software may not be called "Apache"
00032  *    nor may "Apache" appear in their names without prior written
00033  *    permission of the Apache Group.
00034  *
00035  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00036  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00037  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00038  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00039  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00040  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00041  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00042  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00043  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00044  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00045  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00046  * SUCH DAMAGE.
00047  * ====================================================================
00048  *
00049  * This software consists of voluntary contributions made by many
00050  * individuals on behalf of the Apache Software Foundation.  For more
00051  * information on the Apache Software Foundation, please see
00052  * <http://www.apache.org/>.
00053  */
00054 
00055 package org.objectweb.jonas.ant;
00056 
00057 // Standard java imports
00058 import java.io.File;
00059 import java.util.ArrayList;
00060 import java.util.Iterator;
00061 import java.util.List;
00062 import javax.xml.parsers.ParserConfigurationException;
00063 import javax.xml.parsers.SAXParser;
00064 import javax.xml.parsers.SAXParserFactory;
00065 import org.apache.tools.ant.BuildException;
00066 import org.apache.tools.ant.DirectoryScanner;
00067 import org.apache.tools.ant.Project;
00068 import org.apache.tools.ant.taskdefs.MatchingTask;
00069 import org.apache.tools.ant.types.EnumeratedAttribute;
00070 import org.apache.tools.ant.types.FileSet;
00071 import org.apache.tools.ant.types.Path;
00072 import org.xml.sax.SAXException;
00073 
00096 public class EjbJar extends MatchingTask {
00097 
00101     public static class DTDLocation
00102         extends org.apache.tools.ant.types.DTDLocation {
00103     }
00104 
00109     static class Config {
00114         public File srcDir;
00115 
00120         public File descriptorDir;
00121 
00123         public String baseNameTerminator = "-";
00124 
00126         public String baseJarName;
00127 
00132         public boolean flatDestDir = false;
00133 
00137         public Path classpath;
00138 
00142         public List supportFileSets = new ArrayList();
00143 
00147         public ArrayList dtdLocations = new ArrayList();
00148 
00153         public NamingScheme namingScheme;
00154 
00158         public File manifest;
00159 
00163         public String analyzer;
00164     }
00165 
00170     public static class NamingScheme extends EnumeratedAttribute {
00175         public static final String EJB_NAME = "ejb-name";
00176 
00181         public static final String DIRECTORY = "directory";
00182 
00187         public static final String DESCRIPTOR = "descriptor";
00188 
00193         public static final String BASEJARNAME = "basejarname";
00194 
00200         public String[] getValues() {
00201             return new String[] {EJB_NAME, DIRECTORY, DESCRIPTOR, BASEJARNAME};
00202         }
00203     }
00204 
00210     public static class CMPVersion extends EnumeratedAttribute {
00211         public static final String CMP1_0 = "1.0";
00212         public static final String CMP2_0 = "2.0";
00213         public String[] getValues() {
00214             return new String[]{
00215                 CMP1_0,
00216                 CMP2_0,
00217             };
00218         }
00219     }
00224     private Config config = new Config();
00225 
00226 
00233     private File destDir;
00234 
00236     private String genericJarSuffix = "-generic.jar";
00237 
00239     private String cmpVersion = CMPVersion.CMP1_0;
00240 
00242     private ArrayList deploymentTools = new ArrayList();
00243 
00251     protected void addDeploymentTool(EJBDeploymentTool deploymentTool) {
00252         deploymentTool.setTask(this);
00253         deploymentTools.add(deploymentTool);
00254     }
00255 
00261     public JonasDeploymentTool createJonas() {
00262         log("JOnAS deployment tools",  Project.MSG_VERBOSE);
00263 
00264         JonasDeploymentTool tool = new JonasDeploymentTool();
00265         addDeploymentTool(tool);
00266         return tool;
00267     }
00268 
00275     public Path createClasspath() {
00276         if (config.classpath == null) {
00277             config.classpath = new Path(getProject());
00278         }
00279         return config.classpath.createPath();
00280     }
00281 
00289     public DTDLocation createDTD() {
00290         DTDLocation dtdLocation = new DTDLocation();
00291         config.dtdLocations.add(dtdLocation);
00292 
00293         return dtdLocation;
00294     }
00295 
00301     public FileSet createSupport() {
00302         FileSet supportFileSet = new FileSet();
00303         config.supportFileSets.add(supportFileSet);
00304         return supportFileSet;
00305     }
00306 
00307 
00319      public void setManifest(File manifest) {
00320          config.manifest = manifest;
00321      }
00322 
00330     public void setSrcdir(File inDir) {
00331         config.srcDir = inDir;
00332     }
00333 
00343     public void setDescriptordir(File inDir) {
00344         config.descriptorDir = inDir;
00345     }
00346 
00352     public void setDependency(String analyzer) {
00353         config.analyzer = analyzer;
00354     }
00355 
00363     public void setBasejarname(String inValue) {
00364         config.baseJarName = inValue;
00365         if (config.namingScheme == null) {
00366             config.namingScheme = new NamingScheme();
00367             config.namingScheme.setValue(NamingScheme.BASEJARNAME);
00368         } else if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)) {
00369             throw new BuildException("The basejarname attribute is not "
00370                 + "compatible with the "
00371                 + config.namingScheme.getValue() + " naming scheme");
00372         }
00373     }
00374 
00381     public void setNaming(NamingScheme namingScheme) {
00382         config.namingScheme = namingScheme;
00383         if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)
00384             && config.baseJarName != null) {
00385             throw new BuildException("The basejarname attribute is not "
00386                 + "compatible with the "
00387                 + config.namingScheme.getValue() + " naming scheme");
00388         }
00389     }
00390 
00397     public File getDestdir() {
00398         return this.destDir;
00399     }
00400 
00411     public void setDestdir(File inDir) {
00412         this.destDir = inDir;
00413     }
00414 
00421     public String getCmpversion() {
00422         return this.cmpVersion;
00423     }
00424 
00434     public void setCmpversion(CMPVersion version) {
00435         this.cmpVersion = version.getValue();
00436     }
00437 
00443     public void setClasspath(Path classpath) {
00444         config.classpath = classpath;
00445     }
00446 
00458     public void setFlatdestdir(boolean inValue) {
00459         config.flatDestDir = inValue;
00460     }
00461 
00471     public void setGenericjarsuffix(String inString) {
00472         this.genericJarSuffix = inString;
00473     }
00474 
00484     public void setBasenameterminator(String inValue) {
00485         config.baseNameTerminator = inValue;
00486     }
00487 
00493     private void validateConfig() throws BuildException {
00494         if (config.srcDir == null) {
00495             throw new BuildException("The srcDir attribute must be specified");
00496         }
00497 
00498         if (config.descriptorDir == null) {
00499             config.descriptorDir = config.srcDir;
00500         }
00501 
00502         if (config.namingScheme == null) {
00503             config.namingScheme = new NamingScheme();
00504             config.namingScheme.setValue(NamingScheme.DESCRIPTOR);
00505         } else if (config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)
00506                     && config.baseJarName == null) {
00507             throw new BuildException("The basejarname attribute must "
00508                 + "be specified with the basejarname naming scheme");
00509         }
00510     }
00511 
00527     public void execute() throws BuildException {
00528         validateConfig();
00529 
00530         if (deploymentTools.size() == 0) {
00531             GenericDeploymentTool genericTool = new GenericDeploymentTool();
00532             genericTool.setTask(this);
00533             genericTool.setDestdir(destDir);
00534             genericTool.setGenericJarSuffix(genericJarSuffix);
00535             deploymentTools.add(genericTool);
00536         }
00537 
00538         for (Iterator i = deploymentTools.iterator(); i.hasNext();) {
00539             EJBDeploymentTool tool = (EJBDeploymentTool) i.next();
00540             tool.configure(config);
00541             tool.validateConfigured();
00542         }
00543 
00544         try {
00545             // Create the parser using whatever parser the system dictates
00546             SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
00547             saxParserFactory.setValidating(true);
00548             saxParserFactory.setNamespaceAware(true);
00549             SAXParser saxParser = saxParserFactory.newSAXParser();
00550 
00551 
00552             DirectoryScanner ds = getDirectoryScanner(config.descriptorDir);
00553             ds.scan();
00554             String[] files = ds.getIncludedFiles();
00555 
00556             log(files.length + " deployment descriptors located.",
00557                 Project.MSG_VERBOSE);
00558 
00559             // Loop through the files. Each file represents one deployment
00560             // descriptor, and hence one bean in our model.
00561             for (int index = 0; index < files.length; ++index) {
00562                 // process the deployment descriptor in each tool
00563                 for (Iterator i = deploymentTools.iterator(); i.hasNext();) {
00564                     EJBDeploymentTool tool = (EJBDeploymentTool) i.next();
00565                     tool.processDescriptor(files[index], saxParser);
00566                 }
00567             }
00568         } catch (SAXException se) {
00569             String msg = "SAXException while creating parser."
00570                 + "  Details: "
00571                 + se.getMessage();
00572             throw new BuildException(msg, se);
00573         } catch (ParserConfigurationException pce) {
00574             String msg = "ParserConfigurationException while creating parser. "
00575                        + "Details: " + pce.getMessage();
00576             throw new BuildException(msg, pce);
00577         }
00578     } // end of execute()
00579 
00580 }
00581 
00582 
00583 
00584 
00585 
00586 
00587 

Generated on Tue Feb 15 15:05:13 2005 for JOnAS by  doxygen 1.3.9.1