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 org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.taskdefs.ExecTask;
  23. import org.apache.tools.ant.taskdefs.Java;
  24. import org.apache.tools.ant.types.Path;
  25. import org.apache.tools.ant.types.Reference;
  26. /**
  27. * Generates a Borland Application Server 4.5 client JAR using as
  28. * input the EJB JAR file.
  29. *
  30. * Two mode are available: java mode (default) and fork mode. With the fork mode,
  31. * it is impossible to add classpath to the command line.
  32. *
  33. *
  34. * @ant.task name="blgenclient" category="ejb"
  35. */
  36. public class BorlandGenerateClient extends Task {
  37. static final String JAVA_MODE = "java";
  38. static final String FORK_MODE = "fork";
  39. /** debug the generateclient task */
  40. boolean debug = false;
  41. /** hold the ejbjar file name */
  42. File ejbjarfile = null;
  43. /** hold the client jar file name */
  44. File clientjarfile = null;
  45. /** hold the classpath */
  46. Path classpath;
  47. /** hold the mode (java|fork) */
  48. String mode = FORK_MODE;
  49. /** hold the version */
  50. int version = BorlandDeploymentTool.BAS;
  51. public void setVersion(int version) {
  52. this.version = version;
  53. }
  54. /**
  55. * Command launching mode: java or fork.
  56. */
  57. public void setMode(String s) {
  58. mode = s;
  59. }
  60. /**
  61. * If true, turn on the debug mode for each of the Borland tools launched.
  62. */
  63. public void setDebug(boolean debug) {
  64. this.debug = debug;
  65. }
  66. /**
  67. * EJB JAR file.
  68. */
  69. public void setEjbjar(File ejbfile) {
  70. ejbjarfile = ejbfile;
  71. }
  72. /**
  73. * Client JAR file name.
  74. */
  75. public void setClientjar(File clientjar) {
  76. clientjarfile = clientjar;
  77. }
  78. /**
  79. * Path to use for classpath.
  80. */
  81. public void setClasspath(Path classpath) {
  82. if (this.classpath == null) {
  83. this.classpath = classpath;
  84. } else {
  85. this.classpath.append(classpath);
  86. }
  87. }
  88. /**
  89. * Adds path to the classpath.
  90. */
  91. public Path createClasspath() {
  92. if (this.classpath == null) {
  93. this.classpath = new Path(getProject());
  94. }
  95. return this.classpath.createPath();
  96. }
  97. /**
  98. * Reference to existing path, to use as a classpath.
  99. */
  100. public void setClasspathRef(Reference r) {
  101. createClasspath().setRefid(r);
  102. }
  103. /**
  104. * Do the work.
  105. *
  106. * The work is actually done by creating a separate JVM to run a java task.
  107. *
  108. * @exception BuildException if something goes wrong with the build
  109. */
  110. public void execute() throws BuildException {
  111. if (ejbjarfile == null || ejbjarfile.isDirectory()) {
  112. throw new BuildException("invalid ejb jar file.");
  113. }
  114. if (clientjarfile == null || clientjarfile.isDirectory()) {
  115. log("invalid or missing client jar file.", Project.MSG_VERBOSE);
  116. String ejbjarname = ejbjarfile.getAbsolutePath();
  117. //clientname = ejbjarfile+client.jar
  118. String clientname = ejbjarname.substring(0, ejbjarname.lastIndexOf("."));
  119. clientname = clientname + "client.jar";
  120. clientjarfile = new File(clientname);
  121. }
  122. if (mode == null) {
  123. log("mode is null default mode is java");
  124. setMode(JAVA_MODE);
  125. }
  126. if (!(version == BorlandDeploymentTool.BES
  127. || version == BorlandDeploymentTool.BAS)) {
  128. throw new BuildException("version " + version
  129. + " is not supported");
  130. }
  131. log("client jar file is " + clientjarfile);
  132. if (mode.equalsIgnoreCase(FORK_MODE)) {
  133. executeFork();
  134. } else {
  135. executeJava();
  136. } // end of else
  137. }
  138. /** launch the generate client using java api */
  139. protected void executeJava() throws BuildException {
  140. try {
  141. if (version == BorlandDeploymentTool.BES) {
  142. throw new BuildException("java mode is supported only for "
  143. + "previous version <=" + BorlandDeploymentTool.BAS);
  144. }
  145. log("mode : java");
  146. org.apache.tools.ant.taskdefs.Java execTask = null;
  147. execTask = (Java) getProject().createTask("java");
  148. execTask.setDir(new File("."));
  149. execTask.setClassname("com.inprise.server.commandline.EJBUtilities");
  150. //classpath
  151. //add at the end of the classpath
  152. //the system classpath in order to find the tools.jar file
  153. execTask.setClasspath(classpath.concatSystemClasspath());
  154. execTask.setFork(true);
  155. execTask.createArg().setValue("generateclient");
  156. if (debug) {
  157. execTask.createArg().setValue("-trace");
  158. }
  159. execTask.createArg().setValue("-short");
  160. execTask.createArg().setValue("-jarfile");
  161. // ejb jar file
  162. execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
  163. //client jar file
  164. execTask.createArg().setValue("-single");
  165. execTask.createArg().setValue("-clientjarfile");
  166. execTask.createArg().setValue(clientjarfile.getAbsolutePath());
  167. log("Calling EJBUtilities", Project.MSG_VERBOSE);
  168. execTask.execute();
  169. } catch (Exception e) {
  170. // Have to catch this because of the semantics of calling main()
  171. String msg = "Exception while calling generateclient Details: " + e.toString();
  172. throw new BuildException(msg, e);
  173. }
  174. }
  175. /** launch the generate client using system api */
  176. protected void executeFork() throws BuildException {
  177. if (version == BorlandDeploymentTool.BAS) {
  178. executeForkV4();
  179. }
  180. if (version == BorlandDeploymentTool.BES) {
  181. executeForkV5();
  182. }
  183. }
  184. /** launch the generate client using system api */
  185. protected void executeForkV4() throws BuildException {
  186. try {
  187. log("mode : fork " + BorlandDeploymentTool.BAS, Project.MSG_DEBUG);
  188. org.apache.tools.ant.taskdefs.ExecTask execTask = null;
  189. execTask = (ExecTask) getProject().createTask("exec");
  190. execTask.setDir(new File("."));
  191. execTask.setExecutable("iastool");
  192. execTask.createArg().setValue("generateclient");
  193. if (debug) {
  194. execTask.createArg().setValue("-trace");
  195. }
  196. execTask.createArg().setValue("-short");
  197. execTask.createArg().setValue("-jarfile");
  198. // ejb jar file
  199. execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
  200. //client jar file
  201. execTask.createArg().setValue("-single");
  202. execTask.createArg().setValue("-clientjarfile");
  203. execTask.createArg().setValue(clientjarfile.getAbsolutePath());
  204. log("Calling iastool", Project.MSG_VERBOSE);
  205. execTask.execute();
  206. } catch (Exception e) {
  207. // Have to catch this because of the semantics of calling main()
  208. String msg = "Exception while calling generateclient Details: "
  209. + e.toString();
  210. throw new BuildException(msg, e);
  211. }
  212. }
  213. /** launch the generate client using system api */
  214. protected void executeForkV5() throws BuildException {
  215. try {
  216. log("mode : fork " + BorlandDeploymentTool.BES, Project.MSG_DEBUG);
  217. org.apache.tools.ant.taskdefs.ExecTask execTask = null;
  218. execTask = (ExecTask) getProject().createTask("exec");
  219. execTask.setDir(new File("."));
  220. execTask.setExecutable("iastool");
  221. if (debug) {
  222. execTask.createArg().setValue("-debug");
  223. }
  224. execTask.createArg().setValue("-genclient");
  225. execTask.createArg().setValue("-jars");
  226. // ejb jar file
  227. execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
  228. //client jar file
  229. execTask.createArg().setValue("-target");
  230. execTask.createArg().setValue(clientjarfile.getAbsolutePath());
  231. //classpath
  232. execTask.createArg().setValue("-cp");
  233. execTask.createArg().setValue(classpath.toString());
  234. log("Calling iastool", Project.MSG_VERBOSE);
  235. execTask.execute();
  236. } catch (Exception e) {
  237. // Have to catch this because of the semantics of calling main()
  238. String msg = "Exception while calling generateclient Details: "
  239. + e.toString();
  240. throw new BuildException(msg, e);
  241. }
  242. }
  243. }