001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.ejb;
020:
021: import java.io.File;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.Task;
025: import org.apache.tools.ant.taskdefs.ExecTask;
026: import org.apache.tools.ant.taskdefs.Java;
027: import org.apache.tools.ant.types.Path;
028: import org.apache.tools.ant.types.Reference;
029:
030: /**
031: * Generates a Borland Application Server 4.5 client JAR using as
032: * input the EJB JAR file.
033: *
034: * Two mode are available: java mode (default) and fork mode. With the fork mode,
035: * it is impossible to add classpath to the command line.
036: *
037: * @ant.task name="blgenclient" category="ejb"
038: */
039: public class BorlandGenerateClient extends Task {
040: static final String JAVA_MODE = "java";
041: static final String FORK_MODE = "fork";
042:
043: // CheckStyle:VisibilityModifier OFF - bc
044: /** debug the generateclient task */
045: boolean debug = false;
046:
047: /** hold the ejbjar file name */
048: File ejbjarfile = null;
049:
050: /** hold the client jar file name */
051: File clientjarfile = null;
052:
053: /** hold the classpath */
054: Path classpath;
055:
056: /** hold the mode (java|fork) */
057: String mode = FORK_MODE;
058:
059: /** hold the version */
060: int version = BorlandDeploymentTool.BAS;
061:
062: // CheckStyle:VisibilityModifier ON
063:
064: /**
065: * Set the version attribute.
066: * @param version the value to use.
067: */
068: public void setVersion(int version) {
069: this .version = version;
070: }
071:
072: /**
073: * Command launching mode: java or fork.
074: * @param s the mode to use.
075: */
076: public void setMode(String s) {
077: mode = s;
078: }
079:
080: /**
081: * If true, turn on the debug mode for each of the Borland tools launched.
082: * @param debug a <code>boolean</code> value.
083: */
084: public void setDebug(boolean debug) {
085: this .debug = debug;
086: }
087:
088: /**
089: * EJB JAR file.
090: * @param ejbfile the file to use.
091: */
092: public void setEjbjar(File ejbfile) {
093: ejbjarfile = ejbfile;
094: }
095:
096: /**
097: * Client JAR file name.
098: * @param clientjar the file to use.
099: */
100: public void setClientjar(File clientjar) {
101: clientjarfile = clientjar;
102: }
103:
104: /**
105: * Path to use for classpath.
106: * @param classpath the path to use.
107: */
108: public void setClasspath(Path classpath) {
109: if (this .classpath == null) {
110: this .classpath = classpath;
111: } else {
112: this .classpath.append(classpath);
113: }
114: }
115:
116: /**
117: * Adds path to the classpath.
118: * @return a path to be configured as a nested element.
119: */
120: public Path createClasspath() {
121: if (this .classpath == null) {
122: this .classpath = new Path(getProject());
123: }
124: return this .classpath.createPath();
125: }
126:
127: /**
128: * Reference to existing path, to use as a classpath.
129: * @param r the reference to use.
130: */
131: public void setClasspathRef(Reference r) {
132: createClasspath().setRefid(r);
133: }
134:
135: /**
136: * Do the work.
137: *
138: * The work is actually done by creating a separate JVM to run a java task.
139: *
140: * @exception BuildException if something goes wrong with the build
141: */
142: public void execute() throws BuildException {
143: if (ejbjarfile == null || ejbjarfile.isDirectory()) {
144: throw new BuildException("invalid ejb jar file.");
145: }
146:
147: if (clientjarfile == null || clientjarfile.isDirectory()) {
148: log("invalid or missing client jar file.",
149: Project.MSG_VERBOSE);
150: String ejbjarname = ejbjarfile.getAbsolutePath();
151: //clientname = ejbjarfile+client.jar
152: String clientname = ejbjarname.substring(0, ejbjarname
153: .lastIndexOf("."));
154: clientname = clientname + "client.jar";
155: clientjarfile = new File(clientname);
156: }
157:
158: if (mode == null) {
159: log("mode is null default mode is java");
160: setMode(JAVA_MODE);
161: }
162:
163: if (!(version == BorlandDeploymentTool.BES || version == BorlandDeploymentTool.BAS)) {
164: throw new BuildException("version " + version
165: + " is not supported");
166: }
167:
168: log("client jar file is " + clientjarfile);
169:
170: if (mode.equalsIgnoreCase(FORK_MODE)) {
171: executeFork();
172: } else {
173: executeJava();
174: } // end of else
175: }
176:
177: /**
178: * launch the generate client using java api.
179: * @throws BuildException if there is an error.
180: */
181: protected void executeJava() throws BuildException {
182: try {
183: if (version == BorlandDeploymentTool.BES) {
184: throw new BuildException(
185: "java mode is supported only for "
186: + "previous version <="
187: + BorlandDeploymentTool.BAS);
188: }
189:
190: log("mode : java");
191:
192: Java execTask = null;
193: execTask = new Java(this );
194:
195: execTask.setDir(new File("."));
196: execTask
197: .setClassname("com.inprise.server.commandline.EJBUtilities");
198: //classpath
199: //add at the end of the classpath
200: //the system classpath in order to find the tools.jar file
201: execTask.setClasspath(classpath.concatSystemClasspath());
202:
203: execTask.setFork(true);
204: execTask.createArg().setValue("generateclient");
205: if (debug) {
206: execTask.createArg().setValue("-trace");
207: }
208:
209: execTask.createArg().setValue("-short");
210: execTask.createArg().setValue("-jarfile");
211: // ejb jar file
212: execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
213: //client jar file
214: execTask.createArg().setValue("-single");
215: execTask.createArg().setValue("-clientjarfile");
216: execTask.createArg().setValue(
217: clientjarfile.getAbsolutePath());
218:
219: log("Calling EJBUtilities", Project.MSG_VERBOSE);
220: execTask.execute();
221:
222: } catch (Exception e) {
223: // Have to catch this because of the semantics of calling main()
224: String msg = "Exception while calling generateclient Details: "
225: + e.toString();
226: throw new BuildException(msg, e);
227: }
228: }
229:
230: /**
231: * launch the generate client using system api.
232: * @throws BuildException if there is an error.
233: */
234: protected void executeFork() throws BuildException {
235: if (version == BorlandDeploymentTool.BAS) {
236: executeForkV4();
237: }
238: if (version == BorlandDeploymentTool.BES) {
239: executeForkV5();
240: }
241: }
242:
243: /**
244: * launch the generate client using system api.
245: * @throws BuildException if there is an error.
246: */
247: protected void executeForkV4() throws BuildException {
248: try {
249:
250: log("mode : fork " + BorlandDeploymentTool.BAS,
251: Project.MSG_DEBUG);
252:
253: ExecTask execTask = new ExecTask(this );
254:
255: execTask.setDir(new File("."));
256: execTask.setExecutable("iastool");
257: execTask.createArg().setValue("generateclient");
258: if (debug) {
259: execTask.createArg().setValue("-trace");
260: }
261:
262: execTask.createArg().setValue("-short");
263: execTask.createArg().setValue("-jarfile");
264: // ejb jar file
265: execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
266: //client jar file
267: execTask.createArg().setValue("-single");
268: execTask.createArg().setValue("-clientjarfile");
269: execTask.createArg().setValue(
270: clientjarfile.getAbsolutePath());
271:
272: log("Calling iastool", Project.MSG_VERBOSE);
273: execTask.execute();
274: } catch (Exception e) {
275: // Have to catch this because of the semantics of calling main()
276: String msg = "Exception while calling generateclient Details: "
277: + e.toString();
278: throw new BuildException(msg, e);
279: }
280:
281: }
282:
283: /**
284: * launch the generate client using system api.
285: * @throws BuildException if there is an error.
286: */
287: protected void executeForkV5() throws BuildException {
288: try {
289: log("mode : fork " + BorlandDeploymentTool.BES,
290: Project.MSG_DEBUG);
291: ExecTask execTask = new ExecTask(this );
292:
293: execTask.setDir(new File("."));
294:
295: execTask.setExecutable("iastool");
296: if (debug) {
297: execTask.createArg().setValue("-debug");
298: }
299: execTask.createArg().setValue("-genclient");
300: execTask.createArg().setValue("-jars");
301: // ejb jar file
302: execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
303: //client jar file
304: execTask.createArg().setValue("-target");
305: execTask.createArg().setValue(
306: clientjarfile.getAbsolutePath());
307: //classpath
308: execTask.createArg().setValue("-cp");
309: execTask.createArg().setValue(classpath.toString());
310: log("Calling iastool", Project.MSG_VERBOSE);
311: execTask.execute();
312: } catch (Exception e) {
313: // Have to catch this because of the semantics of calling main()
314: String msg = "Exception while calling generateclient Details: "
315: + e.toString();
316: throw new BuildException(msg, e);
317: }
318:
319: }
320:
321: }
|