KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ejb > BorlandGenerateClient


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19
20 package org.apache.tools.ant.taskdefs.optional.ejb;
21
22 import java.io.File JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.taskdefs.ExecTask;
27 import org.apache.tools.ant.taskdefs.Java;
28 import org.apache.tools.ant.types.Path;
29 import org.apache.tools.ant.types.Reference;
30
31 /**
32  * Generates a Borland Application Server 4.5 client JAR using as
33  * input the EJB JAR file.
34  *
35  * Two mode are available: java mode (default) and fork mode. With the fork mode,
36  * it is impossible to add classpath to the command line.
37  *
38  * @ant.task name="blgenclient" category="ejb"
39  */

40 public class BorlandGenerateClient extends Task {
41     static final String JavaDoc JAVA_MODE = "java";
42     static final String JavaDoc FORK_MODE = "fork";
43
44     // CheckStyle:VisibilityModifier OFF - bc
45
/** debug the generateclient task */
46     boolean debug = false;
47
48     /** hold the ejbjar file name */
49     File JavaDoc ejbjarfile = null;
50
51     /** hold the client jar file name */
52     File JavaDoc clientjarfile = null;
53
54     /** hold the classpath */
55     Path classpath;
56
57     /** hold the mode (java|fork) */
58     String JavaDoc mode = FORK_MODE;
59
60     /** hold the version */
61     int version = BorlandDeploymentTool.BAS;
62     // CheckStyle:VisibilityModifier ON
63

64     /**
65      * Set the version attribute.
66      * @param version the value to use.
67      */

68     public void setVersion(int version) {
69         this.version = version;
70     }
71
72     /**
73      * Command launching mode: java or fork.
74      * @param s the mode to use.
75      */

76     public void setMode(String JavaDoc s) {
77         mode = s;
78     }
79
80     /**
81      * If true, turn on the debug mode for each of the Borland tools launched.
82      * @param debug a <code>boolean</code> value.
83      */

84     public void setDebug(boolean debug) {
85         this.debug = debug;
86     }
87
88     /**
89      * EJB JAR file.
90      * @param ejbfile the file to use.
91      */

92     public void setEjbjar(File JavaDoc ejbfile) {
93         ejbjarfile = ejbfile;
94     }
95
96     /**
97      * Client JAR file name.
98      * @param clientjar the file to use.
99      */

100     public void setClientjar(File JavaDoc 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     /**
137      * Do the work.
138      *
139      * The work is actually done by creating a separate JVM to run a java task.
140      *
141      * @exception BuildException if something goes wrong with the build
142      */

143     public void execute() throws BuildException {
144         if (ejbjarfile == null || ejbjarfile.isDirectory()) {
145             throw new BuildException("invalid ejb jar file.");
146         }
147
148         if (clientjarfile == null || clientjarfile.isDirectory()) {
149             log("invalid or missing client jar file.", Project.MSG_VERBOSE);
150             String JavaDoc ejbjarname = ejbjarfile.getAbsolutePath();
151             //clientname = ejbjarfile+client.jar
152
String JavaDoc clientname = ejbjarname.substring(0, ejbjarname.lastIndexOf("."));
153             clientname = clientname + "client.jar";
154             clientjarfile = new File JavaDoc(clientname);
155         }
156
157         if (mode == null) {
158             log("mode is null default mode is java");
159             setMode(JAVA_MODE);
160         }
161
162         if (!(version == BorlandDeploymentTool.BES
163             || 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("java mode is supported only for "
185                     + "previous version <=" + BorlandDeploymentTool.BAS);
186             }
187
188             log("mode : java");
189
190             Java execTask = null;
191             execTask = new Java(this);
192
193             execTask.setDir(new File JavaDoc("."));
194             execTask.setClassname("com.inprise.server.commandline.EJBUtilities");
195             //classpath
196
//add at the end of the classpath
197
//the system classpath in order to find the tools.jar file
198
execTask.setClasspath(classpath.concatSystemClasspath());
199
200             execTask.setFork(true);
201             execTask.createArg().setValue("generateclient");
202             if (debug) {
203                 execTask.createArg().setValue("-trace");
204             }
205
206             execTask.createArg().setValue("-short");
207             execTask.createArg().setValue("-jarfile");
208             // ejb jar file
209
execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
210             //client jar file
211
execTask.createArg().setValue("-single");
212             execTask.createArg().setValue("-clientjarfile");
213             execTask.createArg().setValue(clientjarfile.getAbsolutePath());
214
215             log("Calling EJBUtilities", Project.MSG_VERBOSE);
216             execTask.execute();
217
218         } catch (Exception JavaDoc e) {
219             // Have to catch this because of the semantics of calling main()
220
String JavaDoc msg = "Exception while calling generateclient Details: " + e.toString();
221             throw new BuildException(msg, e);
222         }
223     }
224
225     /**
226      * launch the generate client using system api.
227      * @throws BuildException if there is an error.
228      */

229     protected void executeFork() throws BuildException {
230         if (version == BorlandDeploymentTool.BAS) {
231             executeForkV4();
232         }
233         if (version == BorlandDeploymentTool.BES) {
234             executeForkV5();
235         }
236     }
237
238     /**
239      * launch the generate client using system api.
240      * @throws BuildException if there is an error.
241      */

242     protected void executeForkV4() throws BuildException {
243         try {
244
245             log("mode : fork " + BorlandDeploymentTool.BAS, Project.MSG_DEBUG);
246
247             ExecTask execTask = new ExecTask(this);
248
249             execTask.setDir(new File JavaDoc("."));
250             execTask.setExecutable("iastool");
251             execTask.createArg().setValue("generateclient");
252             if (debug) {
253                 execTask.createArg().setValue("-trace");
254             }
255
256             execTask.createArg().setValue("-short");
257             execTask.createArg().setValue("-jarfile");
258             // ejb jar file
259
execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
260             //client jar file
261
execTask.createArg().setValue("-single");
262             execTask.createArg().setValue("-clientjarfile");
263             execTask.createArg().setValue(clientjarfile.getAbsolutePath());
264
265             log("Calling iastool", Project.MSG_VERBOSE);
266             execTask.execute();
267         } catch (Exception JavaDoc e) {
268             // Have to catch this because of the semantics of calling main()
269
String JavaDoc msg = "Exception while calling generateclient Details: "
270                 + e.toString();
271             throw new BuildException(msg, e);
272         }
273
274     }
275
276     /**
277      * launch the generate client using system api.
278      * @throws BuildException if there is an error.
279      */

280     protected void executeForkV5() throws BuildException {
281         try {
282             log("mode : fork " + BorlandDeploymentTool.BES, Project.MSG_DEBUG);
283             ExecTask execTask = new ExecTask(this);
284
285             execTask.setDir(new File JavaDoc("."));
286
287             execTask.setExecutable("iastool");
288             if (debug) {
289                 execTask.createArg().setValue("-debug");
290             }
291             execTask.createArg().setValue("-genclient");
292             execTask.createArg().setValue("-jars");
293             // ejb jar file
294
execTask.createArg().setValue(ejbjarfile.getAbsolutePath());
295             //client jar file
296
execTask.createArg().setValue("-target");
297             execTask.createArg().setValue(clientjarfile.getAbsolutePath());
298             //classpath
299
execTask.createArg().setValue("-cp");
300             execTask.createArg().setValue(classpath.toString());
301             log("Calling iastool", Project.MSG_VERBOSE);
302             execTask.execute();
303         } catch (Exception JavaDoc e) {
304             // Have to catch this because of the semantics of calling main()
305
String JavaDoc msg = "Exception while calling generateclient Details: "
306                 + e.toString();
307             throw new BuildException(msg, e);
308         }
309
310     }
311
312 }
313
Popular Tags