001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.ejb;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.Hashtable;
024: import javax.xml.parsers.SAXParser;
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.Project;
027: import org.xml.sax.SAXException;
028:
029: /**
030: * This class is used to generate iPlanet Application Server (iAS) 6.0 stubs and
031: * skeletons and build an EJB Jar file. It is designed to be used with the Ant
032: * <code>ejbjar</code> task. If only stubs and skeletons need to be generated
033: * (in other words, if no JAR file needs to be created), refer to the
034: * <code>iplanet-ejbc</code> task and the <code>IPlanetEjbcTask</code> class.
035: * <p>
036: * The following attributes may be specified by the user:
037: * <ul>
038: * <li><i>destdir</i> -- The base directory into which the generated JAR
039: * files will be written. Each JAR file is written
040: * in directories which correspond to their location
041: * within the "descriptordir" namespace. This is a
042: * required attribute.
043: * <li><i>classpath</i> -- The classpath used when generating EJB stubs and
044: * skeletons. This is an optional attribute (if
045: * omitted, the classpath specified in the "ejbjar"
046: * parent task will be used). If specified, the
047: * classpath elements will be prepended to the
048: * classpath specified in the parent "ejbjar" task.
049: * Note that nested "classpath" elements may also be
050: * used.
051: * <li><i>keepgenerated</i> -- Indicates whether or not the Java source
052: * files which are generated by ejbc will be
053: * saved or automatically deleted. If "yes",
054: * the source files will be retained. This is
055: * an optional attribute (if omitted, it
056: * defaults to "no").
057: * <li><i>debug</i> -- Indicates whether or not the ejbc utility should
058: * log additional debugging statements to the standard
059: * output. If "yes", the additional debugging statements
060: * will be generated (if omitted, it defaults to "no").
061: * <li><i>iashome</i> -- May be used to specify the "home" directory for
062: * this iPlanet Application server installation. This
063: * is used to find the ejbc utility if it isn't
064: * included in the user's system path. This is an
065: * optional attribute (if specified, it should refer
066: * to the <code>[install-location]/iplanet/ias6/ias
067: * </code> directory). If omitted, the ejbc utility
068: * must be on the user's system path.
069: * <li><i>suffix</i> -- String value appended to the JAR filename when
070: * creating each JAR. This attribute is not required
071: * (if omitted, it defaults to ".jar").
072: * </ul>
073: * <p>
074: * For each EJB descriptor found in the "ejbjar" parent task, this deployment
075: * tool will locate the three classes that comprise the EJB. If these class
076: * files cannot be located in the specified <code>srcdir</code> directory, the
077: * task will fail. The task will also attempt to locate the EJB stubs and
078: * skeletons in this directory. If found, the timestamps on the stubs and
079: * skeletons will be checked to ensure they are up to date. Only if these files
080: * cannot be found or if they are out of date will ejbc be called.
081: *
082: * @see IPlanetEjbc
083: */
084: public class IPlanetDeploymentTool extends GenericDeploymentTool {
085:
086: /* Attributes set by the Ant build file */
087: private File iashome;
088: private String jarSuffix = ".jar";
089: private boolean keepgenerated = false;
090: private boolean debug = false;
091:
092: /*
093: * Filenames of the standard EJB descriptor (which is passed to this class
094: * from the parent "ejbjar" task) and the iAS-specific EJB descriptor
095: * (whose name is determined by this class). Both filenames are relative
096: * to the directory specified by the "srcdir" attribute in the ejbjar task.
097: */
098: private String descriptorName;
099: private String 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 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 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 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 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 inString) {
167: log(
168: "Since a generic JAR file is not created during processing, the "
169: + "iPlanet Deployment Tool does not support the "
170: + "\"genericjarsuffix\" attribute. It will be ignored.",
171: Project.MSG_WARN);
172: }
173:
174: /** {@inheritDoc}. */
175: public void processDescriptor(String descriptorName,
176: SAXParser saxParser) {
177: this .descriptorName = descriptorName;
178: this .iasDescriptorName = null;
179:
180: log("iPlanet Deployment Tool processing: " + descriptorName
181: + " (and " + getIasDescriptorName() + ")",
182: Project.MSG_VERBOSE);
183:
184: super .processDescriptor(descriptorName, saxParser);
185: }
186:
187: /**
188: * Verifies that the user selections are valid.
189: *
190: * @param descriptorFileName String representing the file name of an EJB
191: * descriptor to be processed
192: * @param saxParser SAXParser which may be used to parse the XML
193: * descriptor
194: * @throws BuildException If the user selections are invalid.
195: */
196: protected void checkConfiguration(String descriptorFileName,
197: SAXParser saxParser) throws BuildException {
198:
199: int startOfName = descriptorFileName
200: .lastIndexOf(File.separatorChar) + 1;
201: String stdXml = descriptorFileName.substring(startOfName);
202: if (stdXml.equals(EJB_DD) && (getConfig().baseJarName == null)) {
203: String msg = "No name specified for the completed JAR file. The EJB"
204: + " descriptor should be prepended with the JAR "
205: + "name or it should be specified using the "
206: + "attribute \"basejarname\" in the \"ejbjar\" task.";
207: throw new BuildException(msg, getLocation());
208: }
209:
210: File iasDescriptor = new File(getConfig().descriptorDir,
211: getIasDescriptorName());
212: if ((!iasDescriptor.exists()) || (!iasDescriptor.isFile())) {
213: String msg = "The iAS-specific EJB descriptor ("
214: + iasDescriptor + ") was not found.";
215: throw new BuildException(msg, getLocation());
216: }
217:
218: if ((iashome != null) && (!iashome.isDirectory())) {
219: String msg = "If \"iashome\" is specified, it must be a valid "
220: + "directory (it was set to " + iashome + ").";
221: throw new BuildException(msg, getLocation());
222: }
223: }
224:
225: /**
226: * This method returns a list of EJB files found when the specified EJB
227: * descriptor is parsed and processed.
228: *
229: * @param descriptorFileName String representing the file name of an EJB
230: * descriptor to be processed
231: * @param saxParser SAXParser which may be used to parse the XML
232: * descriptor
233: * @return Hashtable of EJB class (and other) files to be
234: * added to the completed JAR file
235: * @throws IOException An IOException from the parser, possibly from
236: * the byte stream or character stream
237: * @throws SAXException Any SAX exception, possibly wrapping another
238: * exception
239: */
240: protected Hashtable parseEjbFiles(String descriptorFileName,
241: SAXParser saxParser) throws IOException, SAXException {
242:
243: Hashtable files;
244:
245: /* Build and populate an instance of the ejbc utility */
246: IPlanetEjbc ejbc = new IPlanetEjbc(new File(
247: getConfig().descriptorDir, descriptorFileName),
248: new File(getConfig().descriptorDir,
249: getIasDescriptorName()), getConfig().srcDir,
250: getCombinedClasspath().toString(), saxParser);
251: ejbc.setRetainSource(keepgenerated);
252: ejbc.setDebugOutput(debug);
253: if (iashome != null) {
254: ejbc.setIasHomeDir(iashome);
255: }
256:
257: /* Execute the ejbc utility -- stubs/skeletons are rebuilt, if needed */
258: try {
259: ejbc.execute();
260: } catch (IPlanetEjbc.EjbcException e) {
261: throw new BuildException(
262: "An error has occurred while trying to "
263: + "execute the iAS ejbc utility", e,
264: getLocation());
265: }
266:
267: displayName = ejbc.getDisplayName();
268: files = ejbc.getEjbFiles();
269:
270: /* Add CMP descriptors to the list of EJB files */
271: String[] cmpDescriptors = ejbc.getCmpDescriptors();
272: if (cmpDescriptors.length > 0) {
273: File baseDir = getConfig().descriptorDir;
274:
275: int endOfPath = descriptorFileName
276: .lastIndexOf(File.separator);
277: String relativePath = descriptorFileName.substring(0,
278: endOfPath + 1);
279:
280: for (int i = 0; i < cmpDescriptors.length; i++) {
281: int endOfCmp = cmpDescriptors[i].lastIndexOf('/');
282: String cmpDescriptor = cmpDescriptors[i]
283: .substring(endOfCmp + 1);
284:
285: File cmpFile = new File(baseDir, relativePath
286: + cmpDescriptor);
287: if (!cmpFile.exists()) {
288: throw new BuildException(
289: "The CMP descriptor file (" + cmpFile
290: + ") could not be found.",
291: getLocation());
292: }
293: files.put(cmpDescriptors[i], cmpFile);
294: }
295: }
296:
297: return files;
298: }
299:
300: /**
301: * Add the iAS-specific EJB descriptor to the list of files which will be
302: * written to the JAR file.
303: *
304: * @param ejbFiles Hashtable of EJB class (and other) files to be added to
305: * the completed JAR file.
306: * @param ddPrefix not used
307: */
308: protected void addVendorFiles(Hashtable ejbFiles, String ddPrefix) {
309: ejbFiles.put(META_DIR + IAS_DD, new File(
310: getConfig().descriptorDir, getIasDescriptorName()));
311: }
312:
313: /**
314: * Get the name of the Jar that will be written. The modification date
315: * of this jar will be checked against the dependent bean classes.
316: *
317: * @param baseName String name of the EJB JAR file to be written (without
318: * a filename extension).
319: *
320: * @return File representing the JAR file which will be written.
321: */
322: File getVendorOutputJarFile(String baseName) {
323: File jarFile = new File(getDestDir(), baseName + jarSuffix);
324: log("JAR file name: " + jarFile.toString(), Project.MSG_VERBOSE);
325: return jarFile;
326: }
327:
328: /**
329: * The iAS ejbc utility doesn't require the Public ID of the descriptor's
330: * DTD for it to process correctly--this method always returns <code>null
331: * </code>.
332: *
333: * @return <code>null</code>.
334: */
335: protected String getPublicId() {
336: return null;
337: }
338:
339: /**
340: * Determines the name of the iAS-specific EJB descriptor using the
341: * specified standard EJB descriptor name. In general, the standard
342: * descriptor will be named "[basename]-ejb-jar.xml", and this method will
343: * return "[basename]-ias-ejb-jar.xml".
344: *
345: * @return The name of the iAS-specific EJB descriptor file.
346: */
347: private String getIasDescriptorName() {
348:
349: /* Only calculate the descriptor name once */
350: if (iasDescriptorName != null) {
351: return iasDescriptorName;
352: }
353:
354: String path = ""; // Directory path of the EJB descriptor
355: String basename; // Filename appearing before name terminator
356: String remainder; // Filename appearing after the name terminator
357:
358: /* Find the end of the standard descriptor's relative path */
359: int startOfFileName = descriptorName
360: .lastIndexOf(File.separatorChar);
361: if (startOfFileName != -1) {
362: path = descriptorName.substring(0, startOfFileName + 1);
363: }
364:
365: /* Check to see if the standard name is used (there's no basename) */
366: if (descriptorName.substring(startOfFileName + 1)
367: .equals(EJB_DD)) {
368: basename = "";
369: remainder = EJB_DD;
370:
371: } else {
372: int endOfBaseName = descriptorName.indexOf(
373: getConfig().baseNameTerminator, startOfFileName);
374: /*
375: * Check for the odd case where the terminator and/or filename
376: * extension aren't found. These will ensure "ias-" appears at the
377: * end of the name and before the '.' (if present).
378: */
379: if (endOfBaseName < 0) {
380: endOfBaseName = descriptorName.lastIndexOf('.') - 1;
381: if (endOfBaseName < 0) {
382: endOfBaseName = descriptorName.length() - 1;
383: }
384: }
385:
386: basename = descriptorName.substring(startOfFileName + 1,
387: endOfBaseName + 1);
388: remainder = descriptorName.substring(endOfBaseName + 1);
389: }
390:
391: iasDescriptorName = path + basename + "ias-" + remainder;
392: return iasDescriptorName;
393: }
394: }
|