source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/ejb/EjbJar.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 21.2 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.ejb;
19
20// Standard java imports
21import java.io.File;
22import java.util.ArrayList;
23import java.util.Iterator;
24import java.util.List;
25import javax.xml.parsers.ParserConfigurationException;
26import javax.xml.parsers.SAXParser;
27import javax.xml.parsers.SAXParserFactory;
28import org.apache.tools.ant.BuildException;
29import org.apache.tools.ant.DirectoryScanner;
30import org.apache.tools.ant.Project;
31import org.apache.tools.ant.taskdefs.MatchingTask;
32import org.apache.tools.ant.types.EnumeratedAttribute;
33import org.apache.tools.ant.types.FileSet;
34import org.apache.tools.ant.types.Path;
35import org.xml.sax.SAXException;
36
37/**
38 * Provides automated EJB JAR file creation.
39 * <p>
40 * Extends the
41 * MatchingTask class provided in the default ant distribution to provide a
42 * directory scanning EJB jarfile generator.
43 * </p>
44 *
45 * <p>
46 * The task works by taking the deployment descriptors one at a time and
47 * parsing them to locate the names of the classes which should be placed in
48 * the jar. The classnames are translated to java.io.Files by replacing
49 * periods with File.separatorChar and resolving the generated filename as a
50 * relative path under the srcDir attribute. All necessary files are then
51 * assembled into a jarfile. One jarfile is constructed for each deployment
52 * descriptor found.
53 * </p>
54 *
55 * */
56public class EjbJar extends MatchingTask {
57
58 /**
59 * Inner class used to record information about the location of a local DTD
60 */
61 public static class DTDLocation
62 extends org.apache.tools.ant.types.DTDLocation {
63 }
64
65 /**
66 * A class which contains the configuration state of the ejbjar task.
67 * This state is passed to the deployment tools for configuration
68 */
69 static class Config {
70 /**
71 * Stores a handle to the directory under which to search for class
72 * files
73 */
74 public File srcDir;
75
76 /**
77 * Stores a handle to the directory under which to search for
78 * deployment descriptors
79 */
80 public File descriptorDir;
81
82 /** Instance variable that marks the end of the 'basename' */
83 public String baseNameTerminator = "-";
84
85 /** Stores a handle to the destination EJB Jar file */
86 public String baseJarName;
87
88 /**
89 * Instance variable that determines whether to use a package structure
90 * of a flat directory as the destination for the jar files.
91 */
92 public boolean flatDestDir = false;
93
94 /**
95 * The classpath to use when loading classes
96 */
97 public Path classpath;
98
99 /**
100 * A Fileset of support classes
101 */
102 public List supportFileSets = new ArrayList();
103
104 /**
105 * The list of configured DTD locations
106 */
107 public ArrayList dtdLocations = new ArrayList();
108
109 /**
110 * The naming scheme used to determine the generated jar name
111 * from the descriptor information
112 */
113 public NamingScheme namingScheme;
114
115 /**
116 * The Manifest file
117 */
118 public File manifest;
119
120 /**
121 * The dependency analyzer to use to add additional classes to the jar
122 */
123 public String analyzer;
124 }
125
126 /**
127 * An EnumeratedAttribute class for handling different EJB jar naming
128 * schemes
129 */
130 public static class NamingScheme extends EnumeratedAttribute {
131 /**
132 * Naming scheme where generated jar is determined from the ejb-name in
133 * the deployment descripor
134 */
135 public static final String EJB_NAME = "ejb-name";
136
137 /**
138 * Naming scheme where the generated jar name is based on the
139 * name of the directory containing the deployment descriptor
140 */
141 public static final String DIRECTORY = "directory";
142
143 /**
144 * Naming scheme where the generated jar name is based on the name of
145 * the deployment descriptor file
146 */
147 public static final String DESCRIPTOR = "descriptor";
148
149 /**
150 * Naming scheme where the generated jar is named by the basejarname
151 * attribute
152 */
153 public static final String BASEJARNAME = "basejarname";
154
155 /**
156 * Gets the values of the NamingScheme
157 *
158 * @return an array of the values of this attribute class.
159 */
160 public String[] getValues() {
161 return new String[] {EJB_NAME, DIRECTORY, DESCRIPTOR, BASEJARNAME};
162 }
163 }
164
165 /**
166 * CMP versions supported
167 * valid CMP versions are 1.0 and 2.0
168 * @since ant 1.6
169 */
170 public static class CMPVersion extends EnumeratedAttribute {
171 public static final String CMP1_0 = "1.0";
172 public static final String CMP2_0 = "2.0";
173 public String[] getValues() {
174 return new String[]{
175 CMP1_0,
176 CMP2_0,
177 };
178 }
179 }
180 /**
181 * The config which is built by this task and used by the various deployment
182 * tools to access the configuration of the ejbjar task
183 */
184 private Config config = new Config();
185
186
187 /**
188 * Stores a handle to the directory to put the Jar files in. This is
189 * only used by the generic deployment descriptor tool which is created
190 * if no other deployment descriptor tools are provided. Normally each
191 * deployment tool will specify the desitination dir itself.
192 */
193 private File destDir;
194
195 /** Instance variable that stores the suffix for the generated jarfile. */
196 private String genericJarSuffix = "-generic.jar";
197
198 /** Instance variable that stores the CMP version for the jboss jarfile. */
199 private String cmpVersion = CMPVersion.CMP1_0;
200
201 /** The list of deployment tools we are going to run. */
202 private ArrayList deploymentTools = new ArrayList();
203
204 /**
205 * Add a deployment tool to the list of deployment tools that will be
206 * processed
207 *
208 * @param deploymentTool a deployment tool instance to which descriptors
209 * will be passed for processing.
210 */
211 protected void addDeploymentTool(EJBDeploymentTool deploymentTool) {
212 deploymentTool.setTask(this);
213 deploymentTools.add(deploymentTool);
214 }
215
216 /**
217 * Adds a deployment tool for Weblogic server.
218 *
219 * @return the deployment tool instance to be configured.
220 */
221 public WeblogicDeploymentTool createWeblogic() {
222 WeblogicDeploymentTool tool = new WeblogicDeploymentTool();
223 addDeploymentTool(tool);
224 return tool;
225 }
226
227 /**
228 * Adds a deployment tool for Websphere 4.0 server.
229 *
230 * @return the deployment tool instance to be configured.
231 */
232 public WebsphereDeploymentTool createWebsphere() {
233 WebsphereDeploymentTool tool = new WebsphereDeploymentTool();
234 addDeploymentTool(tool);
235 return tool;
236 }
237
238 /**
239 * Adds a deployment tool for Borland server.
240 *
241 * @return the deployment tool instance to be configured.
242 */
243 public BorlandDeploymentTool createBorland() {
244 log("Borland deployment tools", Project.MSG_VERBOSE);
245
246 BorlandDeploymentTool tool = new BorlandDeploymentTool();
247 tool.setTask(this);
248 deploymentTools.add(tool);
249 return tool;
250 }
251
252 /**
253 * Adds a deployment tool for iPlanet Application Server.
254 *
255 * @return the deployment tool instance to be configured.
256 */
257 public IPlanetDeploymentTool createIplanet() {
258 log("iPlanet Application Server deployment tools", Project.MSG_VERBOSE);
259
260 IPlanetDeploymentTool tool = new IPlanetDeploymentTool();
261 addDeploymentTool(tool);
262 return tool;
263 }
264
265 /**
266 * Adds a deployment tool for JBoss server.
267 *
268 * @return the deployment tool instance to be configured.
269 */
270 public JbossDeploymentTool createJboss() {
271 JbossDeploymentTool tool = new JbossDeploymentTool();
272 addDeploymentTool(tool);
273 return tool;
274 }
275
276 /**
277 * Adds a deployment tool for JOnAS server.
278 *
279 * @return the deployment tool instance to be configured.
280 */
281 public JonasDeploymentTool createJonas() {
282 log("JOnAS deployment tools", Project.MSG_VERBOSE);
283
284 JonasDeploymentTool tool = new JonasDeploymentTool();
285 addDeploymentTool(tool);
286 return tool;
287 }
288
289 /**
290 * Adds a deployment tool for Weblogic when using the Toplink
291 * Object-Relational mapping.
292 *
293 * @return the deployment tool instance to be configured.
294 */
295 public WeblogicTOPLinkDeploymentTool createWeblogictoplink() {
296 log("The <weblogictoplink> element is no longer required. Please use "
297 + "the <weblogic> element and set newCMP=\"true\"",
298 Project.MSG_INFO);
299 WeblogicTOPLinkDeploymentTool tool
300 = new WeblogicTOPLinkDeploymentTool();
301 addDeploymentTool(tool);
302 return tool;
303 }
304
305 /**
306 * Adds to the classpath used to locate the super classes and
307 * interfaces of the classes that will make up the EJB JAR.
308 *
309 * @return the path to be configured.
310 */
311 public Path createClasspath() {
312 if (config.classpath == null) {
313 config.classpath = new Path(getProject());
314 }
315 return config.classpath.createPath();
316 }
317
318 /**
319 * Create a DTD location record. This stores the location of a DTD. The
320 * DTD is identified by its public Id. The location may either be a file
321 * location or a resource location.
322 *
323 * @return the DTD location object to be configured by Ant
324 */
325 public DTDLocation createDTD() {
326 DTDLocation dtdLocation = new DTDLocation();
327 config.dtdLocations.add(dtdLocation);
328
329 return dtdLocation;
330 }
331
332 /**
333 * Adds a fileset for support elements.
334 *
335 * @return a fileset which can be populated with support files.
336 */
337 public FileSet createSupport() {
338 FileSet supportFileSet = new FileSet();
339 config.supportFileSets.add(supportFileSet);
340 return supportFileSet;
341 }
342
343
344 /**
345 * Set the Manifest file to use when jarring. As of EJB 1.1, manifest
346 * files are no longer used to configure the EJB. However, they still
347 * have a vital importance if the EJB is intended to be packaged in an
348 * EAR file. By adding "Class-Path" settings to a Manifest file, the EJB
349 * can look for classes inside the EAR file itself, allowing for easier
350 * deployment. This is outlined in the J2EE specification, and all J2EE
351 * components are meant to support it.
352 *
353 * @param manifest the manifest to be used in the EJB jar
354 */
355 public void setManifest(File manifest) {
356 config.manifest = manifest;
357 }
358
359 /**
360 * Sets the source directory, which is the directory that
361 * contains the classes that will be added to the EJB jar. Typically
362 * this will include the home and remote interfaces and the bean class.
363 *
364 * @param inDir the source directory.
365 */
366 public void setSrcdir(File inDir) {
367 config.srcDir = inDir;
368 }
369
370 /**
371 * Set the descriptor directory. The descriptor directory contains the
372 * EJB deployment descriptors. These are XML files that declare the
373 * properties of a bean in a particular deployment scenario. Such
374 * properties include, for example, the transactional nature of the bean
375 * and the security access control to the bean's methods.
376 *
377 * @param inDir the directory containing the deployment descriptors.
378 */
379 public void setDescriptordir(File inDir) {
380 config.descriptorDir = inDir;
381 }
382
383 /**
384 * Set the analyzer to use when adding in dependencies to the JAR.
385 *
386 * @param analyzer the name of the dependency analyzer or a class.
387 */
388 public void setDependency(String analyzer) {
389 config.analyzer = analyzer;
390 }
391
392 /**
393 * Set the base name of the EJB JAR that is to be created if it is not
394 * to be determined from the name of the deployment descriptor files.
395 *
396 * @param inValue the basename that will be used when writing the jar
397 * file containing the EJB
398 */
399 public void setBasejarname(String inValue) {
400 config.baseJarName = inValue;
401 if (config.namingScheme == null) {
402 config.namingScheme = new NamingScheme();
403 config.namingScheme.setValue(NamingScheme.BASEJARNAME);
404 } else if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)) {
405 throw new BuildException("The basejarname attribute is not "
406 + "compatible with the "
407 + config.namingScheme.getValue() + " naming scheme");
408 }
409 }
410
411 /**
412 * Set the naming scheme used to determine the name of the generated jars
413 * from the deployment descriptor
414 *
415 * @param namingScheme the naming scheme to be used
416 */
417 public void setNaming(NamingScheme namingScheme) {
418 config.namingScheme = namingScheme;
419 if (!config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)
420 && config.baseJarName != null) {
421 throw new BuildException("The basejarname attribute is not "
422 + "compatible with the "
423 + config.namingScheme.getValue() + " naming scheme");
424 }
425 }
426
427 /**
428 * Gets the destination directory.
429 *
430 * @return destination directory
431 * @since ant 1.6
432 */
433 public File getDestdir() {
434 return this.destDir;
435 }
436
437 /**
438 * Set the destination directory. The EJB jar files will be written into
439 * this directory. The jar files that exist in this directory are also
440 * used when determining if the contents of the jar file have changed.
441 * Note that this parameter is only used if no deployment tools are
442 * specified. Typically each deployment tool will specify its own
443 * destination directory.
444 *
445 * @param inDir the destination directory in which to generate jars
446 */
447 public void setDestdir(File inDir) {
448 this.destDir = inDir;
449 }
450
451 /**
452 * Gets the CMP version.
453 *
454 * @return CMP version
455 * @since ant 1.6
456 */
457 public String getCmpversion() {
458 return this.cmpVersion;
459 }
460
461 /**
462 * Sets the CMP version.
463 *
464 * @param version CMP version.
465 * Must be either <code>1.0</code> or <code>2.0</code>.<br/>
466 * Default is <code>1.0</code>.<br/>
467 * Initially, only the JBoss implementation does something specific for CMP 2.0.<br/>
468 * @since ant 1.6
469 */
470 public void setCmpversion(CMPVersion version) {
471 this.cmpVersion = version.getValue();
472 }
473
474 /**
475 * Set the classpath to use when resolving classes for inclusion in the jar.
476 *
477 * @param classpath the classpath to use.
478 */
479 public void setClasspath(Path classpath) {
480 config.classpath = classpath;
481 }
482
483 /**
484 * Controls whether the
485 * destination JARs are written out in the destination directory with
486 * the same hierarchical structure from which the deployment descriptors
487 * have been read. If this is set to true the generated EJB jars are
488 * written into the root of the destination directory, otherwise they
489 * are written out in the same relative position as the deployment
490 * descriptors in the descriptor directory.
491 *
492 * @param inValue the new value of the flatdestdir flag.
493 */
494 public void setFlatdestdir(boolean inValue) {
495 config.flatDestDir = inValue;
496 }
497
498 /**
499 * Set the suffix for the generated jar file. When generic jars are
500 * generated, they have a suffix which is appended to the the bean name
501 * to create the name of the jar file. Note that this suffix includes
502 * the extension fo te jar file and should therefore end with an
503 * appropriate extension such as .jar or .ear
504 *
505 * @param inString the string to use as the suffix.
506 */
507 public void setGenericjarsuffix(String inString) {
508 this.genericJarSuffix = inString;
509 }
510
511 /**
512 * The string which terminates the bean name.
513 * The convention used by this task is
514 * that bean descriptors are named as the BeanName with some suffix. The
515 * baseNameTerminator string separates the bean name and the suffix and
516 * is used to determine the bean name.
517 *
518 * @param inValue a string which marks the end of the basename.
519 */
520 public void setBasenameterminator(String inValue) {
521 config.baseNameTerminator = inValue;
522 }
523
524 /**
525 * Validate the config that has been configured from the build file
526 *
527 * @throws BuildException if the config is not valid
528 */
529 private void validateConfig() throws BuildException {
530 if (config.srcDir == null) {
531 throw new BuildException("The srcDir attribute must be specified");
532 }
533
534 if (config.descriptorDir == null) {
535 config.descriptorDir = config.srcDir;
536 }
537
538 if (config.namingScheme == null) {
539 config.namingScheme = new NamingScheme();
540 config.namingScheme.setValue(NamingScheme.DESCRIPTOR);
541 } else if (config.namingScheme.getValue().equals(NamingScheme.BASEJARNAME)
542 && config.baseJarName == null) {
543 throw new BuildException("The basejarname attribute must "
544 + "be specified with the basejarname naming scheme");
545 }
546 }
547
548 /**
549 * Invoked by Ant after the task is prepared, when it is ready to execute
550 * this task.
551 *
552 * This will configure all of the nested deployment tools to allow them to
553 * process the jar. If no deployment tools have been configured a generic
554 * tool is created to handle the jar.
555 *
556 * A parser is configured and then each descriptor found is passed to all
557 * the deployment tool elements for processing.
558 *
559 * @exception BuildException thrown whenever a problem is
560 * encountered that cannot be recovered from, to signal to ant
561 * that a major problem occurred within this task.
562 */
563 public void execute() throws BuildException {
564 validateConfig();
565
566 if (deploymentTools.size() == 0) {
567 GenericDeploymentTool genericTool = new GenericDeploymentTool();
568 genericTool.setTask(this);
569 genericTool.setDestdir(destDir);
570 genericTool.setGenericJarSuffix(genericJarSuffix);
571 deploymentTools.add(genericTool);
572 }
573
574 for (Iterator i = deploymentTools.iterator(); i.hasNext();) {
575 EJBDeploymentTool tool = (EJBDeploymentTool) i.next();
576 tool.configure(config);
577 tool.validateConfigured();
578 }
579
580 try {
581 // Create the parser using whatever parser the system dictates
582 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
583 saxParserFactory.setValidating(true);
584 SAXParser saxParser = saxParserFactory.newSAXParser();
585
586
587 DirectoryScanner ds = getDirectoryScanner(config.descriptorDir);
588 ds.scan();
589 String[] files = ds.getIncludedFiles();
590
591 log(files.length + " deployment descriptors located.",
592 Project.MSG_VERBOSE);
593
594 // Loop through the files. Each file represents one deployment
595 // descriptor, and hence one bean in our model.
596 for (int index = 0; index < files.length; ++index) {
597 // process the deployment descriptor in each tool
598 for (Iterator i = deploymentTools.iterator(); i.hasNext();) {
599 EJBDeploymentTool tool = (EJBDeploymentTool) i.next();
600 tool.processDescriptor(files[index], saxParser);
601 }
602 }
603 } catch (SAXException se) {
604 String msg = "SAXException while creating parser."
605 + " Details: "
606 + se.getMessage();
607 throw new BuildException(msg, se);
608 } catch (ParserConfigurationException pce) {
609 String msg = "ParserConfigurationException while creating parser. "
610 + "Details: " + pce.getMessage();
611 throw new BuildException(msg, pce);
612 }
613 } // end of execute()
614
615}
616
617
618
619
620
621
622
Note: See TracBrowser for help on using the repository browser.