1. /*
  2. * Copyright 2000-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 a serialized deployment descriptor given a text file description of the
  27. * descriptor in the format supported by WebLogic.
  28. *
  29. * This ant task is a front end for the weblogic DDCreator tool.
  30. *
  31. */
  32. public class DDCreator extends MatchingTask {
  33. /**
  34. * The root directory of the tree containing the textual deployment descriptors. The actual
  35. * deployment descriptor files are selected using include and exclude constructs
  36. * on the EJBC task, as supported by the MatchingTask superclass.
  37. */
  38. private File descriptorDirectory;
  39. /**
  40. * The directory where generated serialised deployment descriptors are placed.
  41. */
  42. private File generatedFilesDirectory;
  43. /**
  44. * The classpath to be used in the weblogic ejbc calls. It must contain the weblogic
  45. * classes necessary fro DDCreator <b>and</b> the implementation classes of the
  46. * home and remote interfaces.
  47. */
  48. private String classpath;
  49. /**
  50. * Do the work.
  51. *
  52. * The work is actually done by creating a helper task. This approach allows
  53. * the classpath of the helper task to be set. Since the weblogic tools require
  54. * the class files of the project's home and remote interfaces to be available in
  55. * the classpath, this also avoids having to start ant with the class path of the
  56. * project it is building.
  57. *
  58. * @exception BuildException if something goes wrong with the build
  59. */
  60. public void execute() throws BuildException {
  61. if (descriptorDirectory == null
  62. || !descriptorDirectory.isDirectory()) {
  63. throw new BuildException("descriptors directory "
  64. + descriptorDirectory.getPath() + " is not valid");
  65. }
  66. if (generatedFilesDirectory == null
  67. || !generatedFilesDirectory.isDirectory()) {
  68. throw new BuildException("dest directory "
  69. + generatedFilesDirectory.getPath() + " is not valid");
  70. }
  71. String args = descriptorDirectory + " " + generatedFilesDirectory;
  72. // get all the files in the descriptor directory
  73. DirectoryScanner ds = super.getDirectoryScanner(descriptorDirectory);
  74. String[] files = ds.getIncludedFiles();
  75. for (int i = 0; i < files.length; ++i) {
  76. args += " " + files[i];
  77. }
  78. String systemClassPath = System.getProperty("java.class.path");
  79. String execClassPath = getProject().translatePath(systemClassPath + ":" + classpath);
  80. Java ddCreatorTask = (Java) getProject().createTask("java");
  81. ddCreatorTask.setTaskName(getTaskName());
  82. ddCreatorTask.setFork(true);
  83. ddCreatorTask.setClassname("org.apache.tools.ant.taskdefs.optional.ejb.DDCreatorHelper");
  84. Commandline.Argument arguments = ddCreatorTask.createArg();
  85. arguments.setLine(args);
  86. ddCreatorTask.setClasspath(new Path(getProject(), execClassPath));
  87. if (ddCreatorTask.executeJava() != 0) {
  88. throw new BuildException("Execution of ddcreator helper failed");
  89. }
  90. }
  91. /**
  92. * Set the directory from where the text descriptions of the deployment descriptors are
  93. * to be read.
  94. *
  95. * @param dirName the name of the directory containing the text deployment descriptor files.
  96. */
  97. public void setDescriptors(String dirName) {
  98. descriptorDirectory = new File(dirName);
  99. }
  100. /**
  101. * Set the directory into which the serialized deployment descriptors are to
  102. * be written.
  103. *
  104. * @param dirName the name of the directory into which the serialised deployment
  105. * descriptors are written.
  106. */
  107. public void setDest(String dirName) {
  108. generatedFilesDirectory = new File(dirName);
  109. }
  110. /**
  111. * Set the classpath to be used for this compilation.
  112. *
  113. * @param s the classpath to use for the ddcreator tool.
  114. */
  115. public void setClasspath(String s) {
  116. this.classpath = getProject().translatePath(s);
  117. }
  118. }