1. /*
  2. * Copyright 2000,2002,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 org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.DirectoryScanner;
  21. import org.apache.tools.ant.taskdefs.Java;
  22. import org.apache.tools.ant.taskdefs.MatchingTask;
  23. import org.apache.tools.ant.types.Commandline;
  24. import org.apache.tools.ant.types.Path;
  25. /**
  26. * Builds EJB support classes using WebLogic's ejbc tool from a directory containing
  27. * a set of deployment descriptors.
  28. *
  29. *
  30. */
  31. public class Ejbc extends MatchingTask {
  32. /**
  33. * The root directory of the tree containing the serialised deployment desciptors. The actual
  34. * deployment descriptor files are selected using include and exclude constructs
  35. * on the ejbc task provided by the MatchingTask superclass.
  36. */
  37. private File descriptorDirectory;
  38. /**
  39. * The directory where generated files are placed.
  40. */
  41. private File generatedFilesDirectory;
  42. /**
  43. * The name of the manifest file generated for the EJB jar.
  44. */
  45. private File generatedManifestFile;
  46. /**
  47. * The classpath to be used in the weblogic ejbc calls. It must contain the weblogic
  48. * classes <b>and</b> the implementation classes of the home and remote interfaces.
  49. */
  50. private String classpath;
  51. /**
  52. * The source directory for the home and remote interfaces. This is used to determine if
  53. * the generated deployment classes are out of date.
  54. */
  55. private File sourceDirectory;
  56. public boolean keepgenerated;
  57. /**
  58. * Do the work.
  59. *
  60. * The work is actually done by creating a separate JVM to run a helper task.
  61. * This approach allows the classpath of the helper task to be set. Since the
  62. * weblogic tools require the class files of the project's home and remote
  63. * interfaces to be available in the classpath, this also avoids having to
  64. * start ant with the class path of the project it is building.
  65. *
  66. * @exception BuildException if someting goes wrong with the build
  67. */
  68. public void execute() throws BuildException {
  69. if (descriptorDirectory == null
  70. || !descriptorDirectory.isDirectory()) {
  71. throw new BuildException("descriptors directory "
  72. + descriptorDirectory.getPath() + " is not valid");
  73. }
  74. if (generatedFilesDirectory == null
  75. || !generatedFilesDirectory.isDirectory()) {
  76. throw new BuildException("dest directory "
  77. + generatedFilesDirectory.getPath() + " is not valid");
  78. }
  79. if (sourceDirectory == null
  80. || !sourceDirectory.isDirectory()) {
  81. throw new BuildException("src directory "
  82. + sourceDirectory.getPath() + " is not valid");
  83. }
  84. String systemClassPath = System.getProperty("java.class.path");
  85. String execClassPath
  86. = getProject().translatePath(systemClassPath + ":" + classpath
  87. + ":" + generatedFilesDirectory);
  88. // get all the files in the descriptor directory
  89. DirectoryScanner ds = super.getDirectoryScanner(descriptorDirectory);
  90. String[] files = ds.getIncludedFiles();
  91. Java helperTask = (Java) getProject().createTask("java");
  92. helperTask.setTaskName(getTaskName());
  93. helperTask.setFork(true);
  94. helperTask.setClassname("org.apache.tools.ant.taskdefs.optional.ejb.EjbcHelper");
  95. String args = "";
  96. args += " " + descriptorDirectory;
  97. args += " " + generatedFilesDirectory;
  98. args += " " + sourceDirectory;
  99. args += " " + generatedManifestFile;
  100. args += " " + keepgenerated;
  101. for (int i = 0; i < files.length; ++i) {
  102. args += " " + files[i];
  103. }
  104. Commandline.Argument arguments = helperTask.createArg();
  105. arguments.setLine(args);
  106. helperTask.setClasspath(new Path(getProject(), execClassPath));
  107. if (helperTask.executeJava() != 0) {
  108. throw new BuildException("Execution of ejbc helper failed");
  109. }
  110. }
  111. public boolean getKeepgenerated() {
  112. return keepgenerated;
  113. }
  114. /**
  115. * Set the directory from where the serialized deployment descriptors are
  116. * to be read.
  117. *
  118. * @param dirName the name of the directory containing the serialised deployment descriptors.
  119. */
  120. public void setDescriptors(String dirName) {
  121. descriptorDirectory = new File(dirName);
  122. }
  123. /**
  124. * Set the directory into which the support classes, RMI stubs, etc are to be written.
  125. *
  126. * @param dirName the name of the directory into which code is generated
  127. */
  128. public void setDest(String dirName) {
  129. generatedFilesDirectory = new File(dirName);
  130. }
  131. /**
  132. * If true, ejbc will keep the
  133. * intermediate Java files used to build the class files.
  134. * This can be useful when debugging.
  135. */
  136. public void setKeepgenerated(String newKeepgenerated) {
  137. keepgenerated = Boolean.valueOf(newKeepgenerated.trim()).booleanValue();
  138. }
  139. /**
  140. * Set the name of the generated manifest file.
  141. *
  142. * For each EJB that is processed an entry is created in this file. This can then be used
  143. * to create a jar file for dploying the beans.
  144. *
  145. * @param manifestFilename the name of the manifest file to be generated.
  146. */
  147. public void setManifest(String manifestFilename) {
  148. generatedManifestFile = new File(manifestFilename);
  149. }
  150. /**
  151. * Set the classpath to be used for this compilation.
  152. */
  153. public void setClasspath(String s) {
  154. this.classpath = getProject().translatePath(s);
  155. }
  156. /**
  157. * Set the directory containing the source code for the home interface, remote interface
  158. * and public key class definitions.
  159. *
  160. * @param dirName the directory containg the source tree for the EJB's interface classes.
  161. */
  162. public void setSrc(String dirName) {
  163. sourceDirectory = new File(dirName);
  164. }
  165. }