KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ejb > EjbJar


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.ejb;
20
21 // Standard java imports
22
import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import javax.xml.parsers.SAXParser JavaDoc;
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.DirectoryScanner;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.taskdefs.MatchingTask;
33 import org.apache.tools.ant.types.EnumeratedAttribute;
34 import org.apache.tools.ant.types.FileSet;
35 import org.apache.tools.ant.types.Path;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * Provides automated EJB JAR file creation.
40  * <p>
41  * Extends the
42  * MatchingTask class provided in the default ant distribution to provide a
43  * directory scanning EJB jarfile generator.
44  * </p>
45  *
46  * <p>
47  * The task works by taking the deployment descriptors one at a time and
48  * parsing them to locate the names of the classes which should be placed in
49  * the jar. The classnames are translated to java.io.Files by replacing
50  * periods with File.separatorChar and resolving the generated filename as a
51  * relative path under the srcDir attribute. All necessary files are then
52  * assembled into a jarfile. One jarfile is constructed for each deployment
53  * descriptor found.
54  * </p>
55  *
56  * */

57 public class EjbJar extends MatchingTask {
58
59     /**
60      * Inner class used to record information about the location of a local DTD
61      */

62     public static class DTDLocation
63         extends org.apache.tools.ant.types.DTDLocation {
64     }
65
66     /**
67      * A class which contains the configuration state of the ejbjar task.
68      * This state is passed to the deployment tools for configuration
69      */

70     static class Config {
71         // CheckStyle:VisibilityModifier OFF - bc
72
/**
73          * Stores a handle to the directory under which to search for class
74          * files
75          */

76         public File JavaDoc srcDir;
77
78         /**
79          * Stores a handle to the directory under which to search for
80          * deployment descriptors
81          */

82         public File JavaDoc descriptorDir;
83
84         /** Instance variable that marks the end of the 'basename' */
85         public String JavaDoc baseNameTerminator = "-";
86
87         /** Stores a handle to the destination EJB Jar file */
88         public String JavaDoc baseJarName;
89
90         /**
91          * Instance variable that determines whether to use a package structure
92          * of a flat directory as the destination for the jar files.
93          */

94         public boolean flatDestDir = false;
95
96         /**
97          * The classpath to use when loading classes
98          */

99         public Path classpath;
100
101         /**
102          * A Fileset of support classes
103          */

104         public List JavaDoc supportFileSets = new ArrayList JavaDoc();
105
106         /**
107          * The list of configured DTD locations
108          */

109         public ArrayList JavaDoc dtdLocations = new ArrayList JavaDoc();
110
111         /**
112          * The naming scheme used to determine the generated jar name
113          * from the descriptor information
114          */

115         public NamingScheme namingScheme;
116
117         /**
118          * The Manifest file
119          */

120         public File JavaDoc manifest;
121
122         /**
123          * The dependency analyzer to use to add additional classes to the jar
124          */

125         public String JavaDoc analyzer;
126         // CheckStyle:VisibilityModifier ON
127
}
128
129     /**
130      * An EnumeratedAttribute class for handling different EJB jar naming
131      * schemes
132      */

133     public static class NamingScheme extends EnumeratedAttribute {
134         /**
135          * Naming scheme where generated jar is determined from the ejb-name in
136          * the deployment descripor
137          */

138         public static final String JavaDoc EJB_NAME = "ejb-name";
139
140         /**
141          * Naming scheme where the generated jar name is based on the
142          * name of the directory containing the deployment descriptor
143          */

144         public static final String JavaDoc DIRECTORY = "directory";
145
146         /**
147          * Naming scheme where the generated jar name is based on the name of
148          * the deployment descriptor file
149          */

150         public static final String JavaDoc DESCRIPTOR = "descriptor";
151
152         /**
153          * Naming scheme where the generated jar is named by the basejarname
154          * attribute
155          */

156         public static final String JavaDoc BASEJARNAME = "basejarname";
157
158         /**
159          * Gets the values of the NamingScheme
160          *
161          * @return an array of the values of this attribute class.
162          */

163         public String JavaDoc[] getValues() {
164             return new String JavaDoc[] {EJB_NAME, DIRECTORY, DESCRIPTOR, BASEJARNAME};
165         }
166     }
167
168     /**
169      * CMP versions supported
170      * valid CMP versions are 1.0 and 2.0
171      * @since ant 1.6
172      */

173     public static class CMPVersion extends EnumeratedAttribute {
174         /** 1.0 value */
175         public static final String JavaDoc CMP1_0 = "1.0";
176         /** 2.0 value */
177         public static final String JavaDoc CMP2_0 = "2.0";
178         /** {@inheritDoc}. */
179         public String JavaDoc[] getValues() {
180             return new String JavaDoc[]{
181                 CMP1_0,
182                 CMP2_0,
183             };
184         }
185     }
186     /**
187      * The config which is built by this task and used by the various deployment
188      * tools to access the configuration of the ejbjar task
189      */

190     private Config config = new Config();
191
192
193     /**
194      * Stores a handle to the directory to put the Jar files in. This is
195      * only used by the generic deployment descriptor tool which is created
196      * if no other deployment descriptor tools are provided. Normally each
197      * deployment tool will specify the desitination dir itself.
198      */

199     private File JavaDoc destDir;
200
201     /** Instance variable that stores the suffix for the generated jarfile. */
202     private String JavaDoc genericJarSuffix = "-generic.jar";
203
204     /** Instance variable that stores the CMP version for the jboss jarfile. */
205     private String JavaDoc cmpVersion = CMPVersion.CMP1_0;
206
207     /** The list of deployment tools we are going to run. */
208     private ArrayList JavaDoc deploymentTools = new ArrayList JavaDoc();
209
210     /**
211      * Add a deployment tool to the list of deployment tools that will be
212      * processed
213      *
214      * @param deploymentTool a deployment tool instance to which descriptors
215      * will be passed for processing.
216      */

217     protected void addDeploymentTool(EJBDeploymentTool deploymentTool) {
218         deploymentTool.setTask(this);
219         deploymentTools.add(deploymentTool);
220     }
221
222     /**
223      * Adds a deployment tool for Weblogic server.
224      *
225      * @return the deployment tool instance to be configured.
226      */

227     public WeblogicDeploymentTool createWeblogic() {
228         WeblogicDeploymentTool tool = new WeblogicDeploymentTool();
229         addDeploymentTool(tool);
230         return tool;
231     }
232
233     /**
234      * Adds a deployment tool for Websphere 4.0 server.
235      *
236      * @return the deployment tool instance to be configured.
237      */

238     public WebsphereDeploymentTool createWebsphere() {
239         WebsphereDeploymentTool tool = new WebsphereDeploymentTool();
240         addDeploymentTool(tool);
241         return tool;
242     }
243
244     /**
245      * Adds a deployment tool for Borland server.
246      *
247      * @return the deployment tool instance to be configured.
248      */

249     public BorlandDeploymentTool createBorland() {
250         log("Borland deployment tools", Project.MSG_VERBOSE);
251
252         BorlandDeploymentTool tool = new BorlandDeploymentTool();
253         tool.setTask(this);
254         deploymentTools.add(tool);
255         return tool;
256     }
257
258     /**
259      * Adds a deployment tool for iPlanet Application Server.
260      *
261      * @return the deployment tool instance to be configured.
262      */

263     public IPlanetDeploymentTool createIplanet() {
264         log("iPlanet Application Server deployment tools", Project.MSG_VERBOSE);
265
266         IPlanetDeploymentTool tool = new IPlanetDeploymentTool();
267         addDeploymentTool(tool);
268         return tool;
269     }
270
271     /**
272      * Adds a deployment tool for JBoss server.
273      *
274      * @return the deployment tool instance to be configured.
275      */

276     public JbossDeploymentTool createJboss() {
277         JbossDeploymentTool tool = new JbossDeploymentTool();
278         addDeploymentTool(tool);
279         return tool;
280     }
281
282     /**
283      * Adds a deployment tool for JOnAS server.
284      *
285      * @return the deployment tool instance to be configured.
286      */

287     public JonasDeploymentTool createJonas() {
288         log("JOnAS deployment tools", Project.MSG_VERBOSE);
289
290         JonasDeploymentTool tool = new JonasDeploymentTool();
291         addDeploymentTool(tool);
292         return tool;
293     }
294
295     /**
296      * Adds a deployment tool for Weblogic when using the Toplink
297      * Object-Relational mapping.
298      *
299      * @return the deployment tool instance to be configured.
300      */

301     public WeblogicTOPLinkDeploymentTool createWeblogictoplink() {
302         log("The <weblogictoplink> element is no longer required. Please use "
303             + "the <weblogic> element and set newCMP=\"true\"",
304             Project.MSG_INFO);
305         WeblogicTOPLinkDeploymentTool tool
306             = new WeblogicTOPLinkDeploymentTool();
307         addDeploymentTool(tool);
308         return tool;
309     }
310
311     /**
312      * Adds to the classpath used to locate the super classes and
313      * interfaces of the classes that will make up the EJB JAR.
314      *
315      * @return the path to be configured.
316      */

317     public Path createClasspath() {
318         if (config.classpath == null) {
319             config.classpath = new Path(getProject());
320         }
321         return config.classpath.createPath();
322     }
323
324     /**
325      * Create a DTD location record. This stores the location of a DTD. The
326      * DTD is identified by its public Id. The location may either be a file
327      * location or a resource location.
328      *
329      * @return the DTD location object to be configured by Ant
330      */

331     public DTDLocation createDTD() {
332         DTDLocation dtdLocation = new DTDLocation();
333         config.dtdLocations.add(dtdLocation);
334
335         return dtdLocation;
336     }
337
338     /**
339      * Adds a fileset for support elements.
340      *
341      * @return a fileset which can be populated with support files.
342      */

343     public FileSet createSupport() {
344         FileSet supportFileSet = new FileSet();
345         config.supportFileSets.add(supportFileSet);
346         return supportFileSet;
347     }
348
349
350     /**
351      * Set the Manifest file to use when jarring. As of EJB 1.1, manifest
352      * files are no longer used to configure the EJB. However, they still
353      * have a vital importance if the EJB is intended to be packaged in an
354      * EAR file. By adding "Class-Path" settings to a Manifest file, the EJB
355      * can look for classes inside the EAR file itself, allowing for easier
356      * deployment. This is outlined in the J2EE specification, and all J2EE
357      * components are meant to support it.
358      *
359      * @param manifest the manifest to be used in the EJB jar
360      */

361      public void setManifest(File JavaDoc manifest) {
362          config.manifest = manifest;
363      }
364
365     /**
366      * Sets the source directory, which is the directory that
367      * contains the classes that will be added to the EJB jar. Typically
368      * this will include the home and remote interfaces and the bean class.
369      *
370      * @param inDir the source directory.
371      */

372     public void setSrcdir(File JavaDoc inDir) {
373         config.srcDir = inDir;
374     }
375
376     /**
377      * Set the descriptor directory. The descriptor directory contains the
378      * EJB deployment descriptors. These are XML files that declare the
379      * properties of a bean in a particular deployment scenario. Such
380      * properties include, for example, the transactional nature of the bean
381      * and the security access control to the bean's methods.
382      *
383      * @param inDir the directory containing the deployment descriptors.
384      */

385     public void setDescriptordir(File JavaDoc inDir) {
386         config.descriptorDir = inDir;
387     }
388
389     /**
390      * Set the analyzer to use when adding in dependencies to the JAR.
391      *
392      * @param analyzer the name of the dependency analyzer or a class.
393      */

394     public void setDependency(String JavaDoc analyzer) {
395         config.analyzer = analyzer;
396     }
397
398     /**
399      * Set the base name of the EJB JAR that is to be created if it is not
400      * to be determined from the name of the deployment descriptor files.
401      *
402      * @param inValue the basename that will be used when writing the jar
403      * file containing the EJB
404      */

405     public void setBasejarname(String JavaDoc inValue) {
406         config.baseJarName = inValue;
407         if (config.namingScheme == null) {
408             config.namingScheme = new NamingScheme();
409             config.namingScheme.setValue(NamingScheme.BASEJARNAME);
410         } else if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)) {
411             throw new BuildException("The basejarname attribute is not "
412                 + "compatible with the "
413                 + config.namingScheme.getValue() + " naming scheme");
414         }
415     }
416
417     /**
418      * Set the naming scheme used to determine the name of the generated jars
419      * from the deployment descriptor
420      *
421      * @param namingScheme the naming scheme to be used
422      */

423     public void setNaming(NamingScheme namingScheme) {
424         config.namingScheme = namingScheme;
425         if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)
426             && config.baseJarName != null) {
427             throw new BuildException("The basejarname attribute is not "
428                 + "compatible with the "
429                 + config.namingScheme.getValue() + " naming scheme");
430         }
431     }
432
433     /**
434      * Gets the destination directory.
435      *
436      * @return destination directory
437      * @since ant 1.6
438      */

439     public File JavaDoc getDestdir() {
440         return this.destDir;
441     }
442
443     /**
444      * Set the destination directory. The EJB jar files will be written into
445      * this directory. The jar files that exist in this directory are also
446      * used when determining if the contents of the jar file have changed.
447      * Note that this parameter is only used if no deployment tools are
448      * specified. Typically each deployment tool will specify its own
449      * destination directory.
450      *
451      * @param inDir the destination directory in which to generate jars
452      */

453     public void setDestdir(File JavaDoc inDir) {
454         this.destDir = inDir;
455     }
456
457     /**
458      * Gets the CMP version.
459      *
460      * @return CMP version
461      * @since ant 1.6
462      */

463     public String JavaDoc getCmpversion() {
464         return this.cmpVersion;
465     }
466
467     /**
468      * Sets the CMP version.
469      *
470      * @param version CMP version.
471      * Must be either <code>1.0</code> or <code>2.0</code>.<br/>
472      * Default is <code>1.0</code>.<br/>
473      * Initially, only the JBoss implementation does something specific for CMP 2.0.<br/>
474      * @since ant 1.6
475      */

476     public void setCmpversion(CMPVersion version) {
477         this.cmpVersion = version.getValue();
478     }
479
480     /**
481      * Set the classpath to use when resolving classes for inclusion in the jar.
482      *
483      * @param classpath the classpath to use.
484      */

485     public void setClasspath(Path classpath) {
486         config.classpath = classpath;
487     }
488
489     /**
490      * Controls whether the
491      * destination JARs are written out in the destination directory with
492      * the same hierarchical structure from which the deployment descriptors
493      * have been read. If this is set to true the generated EJB jars are
494      * written into the root of the destination directory, otherwise they
495      * are written out in the same relative position as the deployment
496      * descriptors in the descriptor directory.
497      *
498      * @param inValue the new value of the flatdestdir flag.
499      */

500     public void setFlatdestdir(boolean inValue) {
501         config.flatDestDir = inValue;
502     }
503
504     /**
505      * Set the suffix for the generated jar file. When generic jars are
506      * generated, they have a suffix which is appended to the the bean name
507      * to create the name of the jar file. Note that this suffix includes
508      * the extension fo te jar file and should therefore end with an
509      * appropriate extension such as .jar or .ear
510      *
511      * @param inString the string to use as the suffix.
512      */

513     public void setGenericjarsuffix(String JavaDoc inString) {
514         this.genericJarSuffix = inString;
515     }
516
517     /**
518      * The string which terminates the bean name.
519      * The convention used by this task is
520      * that bean descriptors are named as the BeanName with some suffix. The
521      * baseNameTerminator string separates the bean name and the suffix and
522      * is used to determine the bean name.
523      *
524      * @param inValue a string which marks the end of the basename.
525      */

526     public void setBasenameterminator(String JavaDoc inValue) {
527         config.baseNameTerminator = inValue;
528     }
529
530     /**
531      * Validate the config that has been configured from the build file
532      *
533      * @throws BuildException if the config is not valid
534      */

535     private void validateConfig() throws BuildException {
536         if (config.srcDir == null) {
537             throw new BuildException("The srcDir attribute must be specified");
538         }
539
540         if (config.descriptorDir == null) {
541             config.descriptorDir = config.srcDir;
542         }
543
544         if (config.namingScheme == null) {
545             config.namingScheme = new NamingScheme();
546             config.namingScheme.setValue(NamingScheme.DESCRIPTOR);
547         } else if (config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)
548                     && config.baseJarName == null) {
549             throw new BuildException("The basejarname attribute must "
550                 + "be specified with the basejarname naming scheme");
551         }
552     }
553
554     /**
555      * Invoked by Ant after the task is prepared, when it is ready to execute
556      * this task.
557      *
558      * This will configure all of the nested deployment tools to allow them to
559      * process the jar. If no deployment tools have been configured a generic
560      * tool is created to handle the jar.
561      *
562      * A parser is configured and then each descriptor found is passed to all
563      * the deployment tool elements for processing.
564      *
565      * @exception BuildException thrown whenever a problem is
566      * encountered that cannot be recovered from, to signal to ant
567      * that a major problem occurred within this task.
568      */

569     public void execute() throws BuildException {
570         validateConfig();
571
572         if (deploymentTools.size() == 0) {
573             GenericDeploymentTool genericTool = new GenericDeploymentTool();
574             genericTool.setTask(this);
575             genericTool.setDestdir(destDir);
576             genericTool.setGenericJarSuffix(genericJarSuffix);
577             deploymentTools.add(genericTool);
578         }
579
580         for (Iterator JavaDoc i = deploymentTools.iterator(); i.hasNext();) {
581             EJBDeploymentTool tool = (EJBDeploymentTool) i.next();
582             tool.configure(config);
583             tool.validateConfigured();
584         }
585
586         try {
587             // Create the parser using whatever parser the system dictates
588
SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
589             saxParserFactory.setValidating(true);
590             SAXParser JavaDoc saxParser = saxParserFactory.newSAXParser();
591
592
593             DirectoryScanner ds = getDirectoryScanner(config.descriptorDir);
594             ds.scan();
595             String JavaDoc[] files = ds.getIncludedFiles();
596
597             log(files.length + " deployment descriptors located.",
598                 Project.MSG_VERBOSE);
599
600             // Loop through the files. Each file represents one deployment
601
// descriptor, and hence one bean in our model.
602
for (int index = 0; index < files.length; ++index) {
603                 // process the deployment descriptor in each tool
604
for (Iterator JavaDoc i = deploymentTools.iterator(); i.hasNext();) {
605                     EJBDeploymentTool tool = (EJBDeploymentTool) i.next();
606                     tool.processDescriptor(files[index], saxParser);
607                 }
608             }
609         } catch (SAXException JavaDoc se) {
610             String JavaDoc msg = "SAXException while creating parser."
611                 + " Details: "
612                 + se.getMessage();
613             throw new BuildException(msg, se);
614         } catch (ParserConfigurationException JavaDoc pce) {
615             String JavaDoc msg = "ParserConfigurationException while creating parser. "
616                        + "Details: " + pce.getMessage();
617             throw new BuildException(msg, pce);
618         }
619     } // end of execute()
620

621 }
622
623
624
625
626
627
628
629
Popular Tags