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;
22 import java.io.IOException;
23 import javax.xml.parsers.ParserConfigurationException;
24 import javax.xml.parsers.SAXParser;
25 import javax.xml.parsers.SAXParserFactory;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Task;
28 import org.apache.tools.ant.types.Path;
29 import org.xml.sax.SAXException;
30
31 /**
32 * Compiles EJB stubs and skeletons for the iPlanet Application Server.
33 * The EJBs to be processed are specified by the EJB 1.1 standard XML
34 * descriptor, and additional attributes are obtained from the iPlanet Application
35 * Server-specific XML descriptor. Since the XML descriptors can include
36 * multiple EJBs, this is a convenient way of specifying many EJBs in a single
37 * Ant task. The following attributes are allowed:
38 * <ul>
39 * <li><i>ejbdescriptor</i> -- Standard EJB 1.1 XML descriptor (typically
40 * titled "ejb-jar.xml"). This attribute is
41 * required.
42 * <li><i>iasdescriptor</i> -- EJB XML descriptor for iPlanet Application
43 * Server (typically titled "ias-ejb-jar.xml).
44 * This attribute is required.
45 * <li><i>dest</i> -- The is the base directory where the RMI stubs and
46 * skeletons are written. In addition, the class files
47 * for each bean (home interface, remote interface, and
48 * EJB implementation) must be found in this directory.
49 * This attribute is required.
50 * <li><i>classpath</i> -- The classpath used when generating EJB stubs and
51 * skeletons. This is an optional attribute (if
52 * omitted, the classpath specified when Ant was
53 * started will be used). Nested "classpath"
54 * elements may also be used.
55 * <li><i>keepgenerated</i> -- Indicates whether or not the Java source
56 * files which are generated by ejbc will be
57 * saved or automatically deleted. If "yes",
58 * the source files will be retained. This is
59 * an optional attribute (if omitted, it
60 * defaults to "no").
61 * <li><i>debug</i> -- Indicates whether or not the ejbc utility should
62 * log additional debugging statements to the standard
63 * output. If "yes", the additional debugging statements
64 * will be generated (if omitted, it defaults to "no").
65 * <li><i>iashome</i> -- May be used to specify the "home" directory for
66 * this iPlanet Application Server installation. This
67 * is used to find the ejbc utility if it isn't
68 * included in the user's system path. This is an
69 * optional attribute (if specified, it should refer
70 * to the <code>[install-location]/iplanet/ias6/ias
71 * </code> directory). If omitted, the ejbc utility
72 * must be on the user's system path.
73 * </ul>
74 * <p>
75 * For each EJB specified, this task will locate the three classes that comprise
76 * the EJB. If these class files cannot be located in the <code>dest</code>
77 * directory, the task will fail. The task will also attempt to locate the EJB
78 * stubs and skeletons in this directory. If found, the timestamps on the
79 * stubs and skeletons will be checked to ensure they are up to date. Only if
80 * these files cannot be found or if they are out of date will ejbc be called
81 * to generate new stubs and skeletons.
82 *
83 * @see IPlanetEjbc
84 *
85 * @ant.task name="iplanet-ejbc" category="ejb"
86 */
87 public class IPlanetEjbcTask extends Task {
88
89 /* Attributes set by the Ant build file */
90 private File ejbdescriptor;
91 private File iasdescriptor;
92 private File dest;
93 private Path classpath;
94 private boolean keepgenerated = false;
95 private boolean debug = false;
96 private File iashome;
97
98 /**
99 * Sets the location of the standard XML EJB descriptor. Typically, this
100 * file is named "ejb-jar.xml".
101 *
102 * @param ejbdescriptor The name and location of the EJB descriptor.
103 */
104 public void setEjbdescriptor(File ejbdescriptor) {
105 this.ejbdescriptor = ejbdescriptor;
106 }
107
108 /**
109 * Sets the location of the iAS-specific XML EJB descriptor. Typically,
110 * this file is named "ias-ejb-jar.xml".
111 *
112 * @param iasdescriptor The name and location of the iAS-specific EJB
113 * descriptor.
114 */
115 public void setIasdescriptor (File iasdescriptor) {
116 this.iasdescriptor = iasdescriptor;
117 }
118
119 /**
120 * Sets the destination directory where the EJB source classes must exist
121 * and where the stubs and skeletons will be written. The destination
122 * directory must exist before this task is executed.
123 *
124 * @param dest The directory where the compiled classes will be written.
125 */
126 public void setDest(File dest) {
127 this.dest = dest;
128 }
129
130 /**
131 * Sets the classpath to be used when compiling the EJB stubs and skeletons.
132 *
133 * @param classpath The classpath to be used.
134 */
135 public void setClasspath(Path classpath) {
136 if (this.classpath == null) {
137 this.classpath = classpath;
138 } else {
139 this.classpath.append(classpath);
140 }
141 }
142
143 /**
144 * Adds to the classpath used when compiling the EJB stubs and skeletons.
145 * @return the class path.
146 */
147 public Path createClasspath() {
148 if (classpath == null) {
149 classpath = new Path(getProject());
150 }
151 return classpath.createPath();
152 }
153
154 /**
155 * If true, the Java source files which are generated by ejbc will be saved .
156 *
157 * @param keepgenerated A boolean indicating if the Java source files for
158 * the stubs and skeletons should be retained.
159 */
160 public void setKeepgenerated(boolean keepgenerated) {
161 this.keepgenerated = keepgenerated;
162 }
163
164 /**
165 * If true, debugging output will be generated when ejbc is
166 * executed.
167 *
168 * @param debug A boolean indicating if debugging output should be generated
169 */
170 public void setDebug(boolean debug) {
171 this.debug = debug;
172 }
173
174 /**
175 * May be used to specify the "home" directory for this iAS installation.
176 * The directory specified should typically be
177 * <code>[install-location]/iplanet/ias6/ias</code>.
178 *
179 * @param iashome The home directory for the user's iAS installation.
180 */
181 public void setIashome(File iashome) {
182 this.iashome = iashome;
183 }
184
185 /**
186 * Does the work.
187 * @throws BuildException if there is a problem.
188 */
189 public void execute() throws BuildException {
190 checkConfiguration();
191
192 executeEjbc(getParser());
193 }
194
195 /**
196 * Verifies that the user selections are valid.
197 *
198 * @throws BuildException If the user selections are invalid.
199 */
200 private void checkConfiguration() throws BuildException {
201
202 if (ejbdescriptor == null) {
203 String msg = "The standard EJB descriptor must be specified using "
204 + "the \"ejbdescriptor\" attribute.";
205 throw new BuildException(msg, getLocation());
206 }
207 if ((!ejbdescriptor.exists()) || (!ejbdescriptor.isFile())) {
208 String msg = "The standard EJB descriptor (" + ejbdescriptor
209 + ") was not found or isn't a file.";
210 throw new BuildException(msg, getLocation());
211 }
212
213 if (iasdescriptor == null) {
214 String msg = "The iAS-speific XML descriptor must be specified using"
215 + " the \"iasdescriptor\" attribute.";
216 throw new BuildException(msg, getLocation());
217 }
218 if ((!iasdescriptor.exists()) || (!iasdescriptor.isFile())) {
219 String msg = "The iAS-specific XML descriptor (" + iasdescriptor
220 + ") was not found or isn't a file.";
221 throw new BuildException(msg, getLocation());
222 }
223
224 if (dest == null) {
225 String msg = "The destination directory must be specified using "
226 + "the \"dest\" attribute.";
227 throw new BuildException(msg, getLocation());
228 }
229 if ((!dest.exists()) || (!dest.isDirectory())) {
230 String msg = "The destination directory (" + dest + ") was not "
231 + "found or isn't a directory.";
232 throw new BuildException(msg, getLocation());
233 }
234
235 if ((iashome != null) && (!iashome.isDirectory())) {
236 String msg = "If \"iashome\" is specified, it must be a valid "
237 + "directory (it was set to " + iashome + ").";
238 throw new BuildException(msg, getLocation());
239 }
240 }
241
242 /**
243 * Returns a SAXParser that may be used to process the XML descriptors.
244 *
245 * @return Parser which may be used to process the EJB descriptors.
246 * @throws BuildException If the parser cannot be created or configured.
247 */
248 private SAXParser getParser() throws BuildException {
249
250 SAXParser saxParser = null;
251 try {
252 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
253 saxParserFactory.setValidating(true);
254 saxParser = saxParserFactory.newSAXParser();
255 } catch (SAXException e) {
256 String msg = "Unable to create a SAXParser: " + e.getMessage();
257 throw new BuildException(msg, e, getLocation());
258 } catch (ParserConfigurationException e) {
259 String msg = "Unable to create a SAXParser: " + e.getMessage();
260 throw new BuildException(msg, e, getLocation());
261 }
262
263 return saxParser;
264 }
265
266 /**
267 * Executes the EJBc utility using the SAXParser provided.
268 *
269 * @param saxParser SAXParser that may be used to process the EJB
270 * descriptors
271 * @throws BuildException If there is an error reading or parsing the XML
272 * descriptors
273 */
274 private void executeEjbc(SAXParser saxParser) throws BuildException {
275 IPlanetEjbc ejbc = new IPlanetEjbc(ejbdescriptor,
276 iasdescriptor,
277 dest,
278 getClasspath().toString(),
279 saxParser);
280 ejbc.setRetainSource(keepgenerated);
281 ejbc.setDebugOutput(debug);
282 if (iashome != null) {
283 ejbc.setIasHomeDir(iashome);
284 }
285
286 try {
287 ejbc.execute();
288 } catch (IOException e) {
289 String msg = "An IOException occurred while trying to read the XML "
290 + "descriptor file: " + e.getMessage();
291 throw new BuildException(msg, e, getLocation());
292 } catch (SAXException e) {
293 String msg = "A SAXException occurred while trying to read the XML "
294 + "descriptor file: " + e.getMessage();
295 throw new BuildException(msg, e, getLocation());
296 } catch (IPlanetEjbc.EjbcException e) {
297 String msg = "An exception occurred while trying to run the ejbc "
298 + "utility: " + e.getMessage();
299 throw new BuildException(msg, e, getLocation());
300 }
301 }
302
303 /**
304 * Returns the CLASSPATH to be used when calling EJBc. If no user CLASSPATH
305 * is specified, the System classpath is returned instead.
306 *
307 * @return Path The classpath to be used for EJBc.
308 */
309 private Path getClasspath() {
310 Path cp = null;
311 if (classpath == null) {
312 cp = (new Path(getProject())).concatSystemClasspath("last");
313 } else {
314 cp = classpath.concatSystemClasspath("ignore");
315 }
316
317 return cp;
318 }
319 }