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