KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import javax.xml.parsers.SAXParser JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.xml.sax.SAXException JavaDoc;
28
29 /**
30  * This class is used to generate iPlanet Application Server (iAS) 6.0 stubs and
31  * skeletons and build an EJB Jar file. It is designed to be used with the Ant
32  * <code>ejbjar</code> task. If only stubs and skeletons need to be generated
33  * (in other words, if no JAR file needs to be created), refer to the
34  * <code>iplanet-ejbc</code> task and the <code>IPlanetEjbcTask</code> class.
35  * <p>
36  * The following attributes may be specified by the user:
37  * <ul>
38  * <li><i>destdir</i> -- The base directory into which the generated JAR
39  * files will be written. Each JAR file is written
40  * in directories which correspond to their location
41  * within the "descriptordir" namespace. This is a
42  * required attribute.
43  * <li><i>classpath</i> -- The classpath used when generating EJB stubs and
44  * skeletons. This is an optional attribute (if
45  * omitted, the classpath specified in the "ejbjar"
46  * parent task will be used). If specified, the
47  * classpath elements will be prepended to the
48  * classpath specified in the parent "ejbjar" task.
49  * Note that nested "classpath" elements may also be
50  * used.
51  * <li><i>keepgenerated</i> -- Indicates whether or not the Java source
52  * files which are generated by ejbc will be
53  * saved or automatically deleted. If "yes",
54  * the source files will be retained. This is
55  * an optional attribute (if omitted, it
56  * defaults to "no").
57  * <li><i>debug</i> -- Indicates whether or not the ejbc utility should
58  * log additional debugging statements to the standard
59  * output. If "yes", the additional debugging statements
60  * will be generated (if omitted, it defaults to "no").
61  * <li><i>iashome</i> -- May be used to specify the "home" directory for
62  * this iPlanet Application server installation. This
63  * is used to find the ejbc utility if it isn't
64  * included in the user's system path. This is an
65  * optional attribute (if specified, it should refer
66  * to the <code>[install-location]/iplanet/ias6/ias
67  * </code> directory). If omitted, the ejbc utility
68  * must be on the user's system path.
69  * <li><i>suffix</i> -- String value appended to the JAR filename when
70  * creating each JAR. This attribute is not required
71  * (if omitted, it defaults to ".jar").
72  * </ul>
73  * <p>
74  * For each EJB descriptor found in the "ejbjar" parent task, this deployment
75  * tool will locate the three classes that comprise the EJB. If these class
76  * files cannot be located in the specified <code>srcdir</code> directory, the
77  * task will fail. The task will also attempt to locate the EJB stubs and
78  * skeletons in this directory. If found, the timestamps on the stubs and
79  * skeletons will be checked to ensure they are up to date. Only if these files
80  * cannot be found or if they are out of date will ejbc be called.
81  *
82  * @see IPlanetEjbc
83  */

84 public class IPlanetDeploymentTool extends GenericDeploymentTool {
85
86     /* Attributes set by the Ant build file */
87     private File JavaDoc iashome;
88     private String JavaDoc jarSuffix = ".jar";
89     private boolean keepgenerated = false;
90     private boolean debug = false;
91
92     /*
93      * Filenames of the standard EJB descriptor (which is passed to this class
94      * from the parent "ejbjar" task) and the iAS-specific EJB descriptor
95      * (whose name is determined by this class). Both filenames are relative
96      * to the directory specified by the "srcdir" attribute in the ejbjar task.
97      */

98     private String JavaDoc descriptorName;
99     private String JavaDoc iasDescriptorName;
100
101     /*
102      * The displayName variable stores the value of the "display-name" element
103      * from the standard EJB descriptor. As a future enhancement to this task,
104      * we may determine the name of the EJB JAR file using this display-name,
105      * but this has not be implemented yet.
106      */

107     private String JavaDoc displayName;
108
109     /*
110      * Regardless of the name of the iAS-specific EJB descriptor file, it will
111      * written in the completed JAR file as "ias-ejb-jar.xml". This is the
112      * naming convention implemented by iAS.
113      */

114     private static final String JavaDoc IAS_DD = "ias-ejb-jar.xml";
115
116     /**
117      * Setter method used to store the "home" directory of the user's iAS
118      * installation. The directory specified should typically be
119      * <code>[install-location]/iplanet/ias6/ias</code>.
120      *
121      * @param iashome The home directory for the user's iAS installation.
122      */

123     public void setIashome(File JavaDoc iashome) {
124         this.iashome = iashome;
125     }
126
127     /**
128      * Setter method used to specify whether the Java source files generated by
129      * the ejbc utility should be saved or automatically deleted.
130      *
131      * @param keepgenerated boolean which, if <code>true</code>, indicates that
132      * Java source files generated by ejbc for the stubs
133      * and skeletons should be kept.
134      */

135     public void setKeepgenerated(boolean keepgenerated) {
136         this.keepgenerated = keepgenerated;
137     }
138
139     /**
140      * Sets whether or not debugging output will be generated when ejbc is
141      * executed.
142      *
143      * @param debug A boolean indicating if debugging output should be generated
144      */

145     public void setDebug(boolean debug) {
146         this.debug = debug;
147     }
148
149     /**
150      * Setter method used to specify the filename suffix (for example, ".jar")
151      * for the JAR files to be created.
152      *
153      * @param jarSuffix The string to use as the JAR filename suffix.
154      */

155     public void setSuffix(String JavaDoc jarSuffix) {
156         this.jarSuffix = jarSuffix;
157     }
158
159     /**
160      * Since iAS doesn't generate a "generic" JAR as part of its processing,
161      * this attribute is ignored and a warning message is displayed to the user.
162      *
163      * @param inString the string to use as the suffix. This parameter is
164      * ignored.
165      */

166     public void setGenericJarSuffix(String JavaDoc inString) {
167         log("Since a generic JAR file is not created during processing, the "
168                 + "iPlanet Deployment Tool does not support the "
169                 + "\"genericjarsuffix\" attribute. It will be ignored.",
170             Project.MSG_WARN);
171     }
172
173     /** {@inheritDoc}. */
174     public void processDescriptor(String JavaDoc descriptorName, SAXParser JavaDoc saxParser) {
175         this.descriptorName = descriptorName;
176         this.iasDescriptorName = null;
177
178         log("iPlanet Deployment Tool processing: " + descriptorName + " (and "
179                 + getIasDescriptorName() + ")", Project.MSG_VERBOSE);
180
181         super.processDescriptor(descriptorName, saxParser);
182     }
183
184     /**
185      * Verifies that the user selections are valid.
186      *
187      * @param descriptorFileName String representing the file name of an EJB
188      * descriptor to be processed
189      * @param saxParser SAXParser which may be used to parse the XML
190      * descriptor
191      * @throws BuildException If the user selections are invalid.
192      */

193     protected void checkConfiguration(String JavaDoc descriptorFileName,
194                                     SAXParser JavaDoc saxParser) throws BuildException {
195
196         int startOfName = descriptorFileName.lastIndexOf(File.separatorChar) + 1;
197         String JavaDoc stdXml = descriptorFileName.substring(startOfName);
198         if (stdXml.equals(EJB_DD) && (getConfig().baseJarName == null)) {
199             String JavaDoc msg = "No name specified for the completed JAR file. The EJB"
200                             + " descriptor should be prepended with the JAR "
201                             + "name or it should be specified using the "
202                             + "attribute \"basejarname\" in the \"ejbjar\" task.";
203             throw new BuildException(msg, getLocation());
204         }
205
206         File JavaDoc iasDescriptor = new File JavaDoc(getConfig().descriptorDir,
207                                         getIasDescriptorName());
208         if ((!iasDescriptor.exists()) || (!iasDescriptor.isFile())) {
209             String JavaDoc msg = "The iAS-specific EJB descriptor ("
210                             + iasDescriptor + ") was not found.";
211             throw new BuildException(msg, getLocation());
212         }
213
214         if ((iashome != null) && (!iashome.isDirectory())) {
215             String JavaDoc msg = "If \"iashome\" is specified, it must be a valid "
216                             + "directory (it was set to " + iashome + ").";
217             throw new BuildException(msg, getLocation());
218         }
219     }
220
221     /**
222      * This method returns a list of EJB files found when the specified EJB
223      * descriptor is parsed and processed.
224      *
225      * @param descriptorFileName String representing the file name of an EJB
226      * descriptor to be processed
227      * @param saxParser SAXParser which may be used to parse the XML
228      * descriptor
229      * @return Hashtable of EJB class (and other) files to be
230      * added to the completed JAR file
231      * @throws IOException An IOException from the parser, possibly from
232      * the byte stream or character stream
233      * @throws SAXException Any SAX exception, possibly wrapping another
234      * exception
235      */

236     protected Hashtable JavaDoc parseEjbFiles(String JavaDoc descriptorFileName,
237                          SAXParser JavaDoc saxParser) throws IOException JavaDoc, SAXException JavaDoc {
238
239         Hashtable JavaDoc files;
240
241         /* Build and populate an instance of the ejbc utility */
242         IPlanetEjbc ejbc = new IPlanetEjbc(
243                                     new File JavaDoc(getConfig().descriptorDir,
244                                                 descriptorFileName),
245                                     new File JavaDoc(getConfig().descriptorDir,
246                                                 getIasDescriptorName()),
247                                     getConfig().srcDir,
248                                     getCombinedClasspath().toString(),
249                                     saxParser);
250         ejbc.setRetainSource(keepgenerated);
251         ejbc.setDebugOutput(debug);
252         if (iashome != null) {
253             ejbc.setIasHomeDir(iashome);
254         }
255
256         /* Execute the ejbc utility -- stubs/skeletons are rebuilt, if needed */
257         try {
258             ejbc.execute();
259         } catch (IPlanetEjbc.EjbcException e) {
260             throw new BuildException("An error has occurred while trying to "
261                         + "execute the iAS ejbc utility", e, getLocation());
262         }
263
264         displayName = ejbc.getDisplayName();
265         files = ejbc.getEjbFiles();
266
267         /* Add CMP descriptors to the list of EJB files */
268         String JavaDoc[] cmpDescriptors = ejbc.getCmpDescriptors();
269         if (cmpDescriptors.length > 0) {
270             File JavaDoc baseDir = getConfig().descriptorDir;
271
272             int endOfPath = descriptorFileName.lastIndexOf(File.separator);
273             String JavaDoc relativePath = descriptorFileName.substring(0, endOfPath + 1);
274
275             for (int i = 0; i < cmpDescriptors.length; i++) {
276                 int endOfCmp = cmpDescriptors[i].lastIndexOf('/');
277                 String JavaDoc cmpDescriptor = cmpDescriptors[i].substring(endOfCmp + 1);
278
279                 File JavaDoc cmpFile = new File JavaDoc(baseDir, relativePath + cmpDescriptor);
280                 if (!cmpFile.exists()) {
281                     throw new BuildException("The CMP descriptor file ("
282                             + cmpFile + ") could not be found.", getLocation());
283                 }
284                 files.put(cmpDescriptors[i], cmpFile);
285             }
286         }
287
288         return files;
289     }
290
291     /**
292      * Add the iAS-specific EJB descriptor to the list of files which will be
293      * written to the JAR file.
294      *
295      * @param ejbFiles Hashtable of EJB class (and other) files to be added to
296      * the completed JAR file.
297      * @param ddPrefix not used
298      */

299     protected void addVendorFiles(Hashtable JavaDoc ejbFiles, String JavaDoc ddPrefix) {
300         ejbFiles.put(META_DIR + IAS_DD, new File JavaDoc(getConfig().descriptorDir,
301                      getIasDescriptorName()));
302     }
303
304     /**
305      * Get the name of the Jar that will be written. The modification date
306      * of this jar will be checked against the dependent bean classes.
307      *
308      * @param baseName String name of the EJB JAR file to be written (without
309      * a filename extension).
310      *
311      * @return File representing the JAR file which will be written.
312      */

313     File JavaDoc getVendorOutputJarFile(String JavaDoc baseName) {
314         File JavaDoc jarFile = new File JavaDoc(getDestDir(), baseName + jarSuffix);
315         log("JAR file name: " + jarFile.toString(), Project.MSG_VERBOSE);
316         return jarFile;
317     }
318
319     /**
320      * The iAS ejbc utility doesn't require the Public ID of the descriptor's
321      * DTD for it to process correctly--this method always returns <code>null
322      * </code>.
323      *
324      * @return <code>null</code>.
325      */

326     protected String JavaDoc getPublicId() {
327         return null;
328     }
329
330     /**
331      * Determines the name of the iAS-specific EJB descriptor using the
332      * specified standard EJB descriptor name. In general, the standard
333      * descriptor will be named "[basename]-ejb-jar.xml", and this method will
334      * return "[basename]-ias-ejb-jar.xml".
335      *
336      * @return The name of the iAS-specific EJB descriptor file.
337      */

338     private String JavaDoc getIasDescriptorName() {
339
340         /* Only calculate the descriptor name once */
341         if (iasDescriptorName != null) {
342             return iasDescriptorName;
343         }
344
345         String JavaDoc path = ""; // Directory path of the EJB descriptor
346
String JavaDoc basename; // Filename appearing before name terminator
347
String JavaDoc remainder; // Filename appearing after the name terminator
348

349         /* Find the end of the standard descriptor's relative path */
350         int startOfFileName = descriptorName.lastIndexOf(File.separatorChar);
351         if (startOfFileName != -1) {
352             path = descriptorName.substring(0, startOfFileName + 1);
353         }
354
355         /* Check to see if the standard name is used (there's no basename) */
356         if (descriptorName.substring(startOfFileName + 1).equals(EJB_DD)) {
357             basename = "";
358             remainder = EJB_DD;
359
360         } else {
361             int endOfBaseName = descriptorName.indexOf(
362                                                 getConfig().baseNameTerminator,
363                                                 startOfFileName);
364             /*
365              * Check for the odd case where the terminator and/or filename
366              * extension aren't found. These will ensure "ias-" appears at the
367              * end of the name and before the '.' (if present).
368              */

369             if (endOfBaseName < 0) {
370                 endOfBaseName = descriptorName.lastIndexOf('.') - 1;
371                 if (endOfBaseName < 0) {
372                     endOfBaseName = descriptorName.length() - 1;
373                 }
374             }
375
376             basename = descriptorName.substring(startOfFileName + 1,
377                                                 endOfBaseName + 1);
378             remainder = descriptorName.substring(endOfBaseName + 1);
379         }
380
381         iasDescriptorName = path + basename + "ias-" + remainder;
382         return iasDescriptorName;
383     }
384 }
385
Popular Tags