KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > GenICTask


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: GenICTask.java,v 1.2 2005/03/09 12:55:50 sauthieg Exp $
23  * --------------------------------------------------------------------------
24 */

25 package org.objectweb.jonas.ant;
26
27 import java.io.File JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.DirectoryScanner;
33 import org.apache.tools.ant.Project;
34 import org.apache.tools.ant.taskdefs.Java;
35 import org.apache.tools.ant.types.FileSet;
36 import org.apache.tools.ant.types.Path;
37
38 /**
39  * GenIC Task. That's basically an Ant Task wrapper around GenIC.
40  *
41  * @author Guillaume Sauthier
42  */

43 public class GenICTask extends BootstrapTask {
44
45     /** WsGen class name */
46     private static final String JavaDoc GENIC_CLASS = "org.objectweb.jonas_ejb.genic.GenIC";
47
48     /** validation of XML files ? */
49     private boolean validation = true;
50
51     /** name of javac command */
52     private String JavaDoc javac = null;
53
54     /** list of javac options */
55     private String JavaDoc javacOpts = null;
56
57     /** will WsGen keep already generated files ? */
58     private boolean keepGen = false;
59
60     /** protocol list */
61     private String JavaDoc protocols = null;
62
63     /** nocompil */
64     private boolean nocompil = false;
65
66     /** Invoke Javac with tools.jar */
67     private boolean invokeCmd = false;
68
69     /** Options for rmic compiler */
70     private String JavaDoc rmicOpts = null;
71
72     /** extra arguments to be passed to GenIC */
73     private String JavaDoc additionalArgs = null;
74
75     /** verbose mode */
76     private boolean verbose = false;
77
78     /** additionnal classpath for libs */
79     private Path libraryClasspath = null;
80
81     /** inner FileSet list */
82     private List JavaDoc filesets = new ArrayList JavaDoc();
83
84     /** debug field */
85     private boolean debug = false;
86
87     /**
88      * @return Returns an empty Path for inner element classpath
89      */

90     public Path createClasspath() {
91         libraryClasspath = new Path(getProject());
92         return libraryClasspath;
93     }
94
95     /**
96      * @return Returns an empty FileSet
97      */

98     public FileSet createFileSet() {
99         FileSet set = new FileSet();
100         set.setProject(getProject());
101         filesets.add(set);
102         return set;
103     }
104
105     /**
106      * Set additional arguments for GenIC command line
107      * @param added additional args
108      */

109     public void setAdditionalargs(String JavaDoc added) {
110         additionalArgs = added;
111     }
112
113     /**
114      * Set verbose mode on/off
115      * @param v boolean
116      */

117     public void setVerbose(boolean v) {
118         verbose = v;
119     }
120
121     /**
122      * Set debug mode on/off. Used only by developpers that wants to Debug GenIC
123      * @param d boolean
124      */

125     public void setJvmdebug(boolean d) {
126         debug = d;
127     }
128
129     /**
130      * Use InvokeCmd option on/off
131      * @param inv boolean
132      */

133     public void setInvokecmd(boolean inv) {
134         invokeCmd = inv;
135     }
136
137     /**
138      * Do not compile generated java files
139      * @param noc on/off
140      */

141     public void setNocompil(boolean noc) {
142         nocompil = noc;
143     }
144
145     /**
146      * Set the optios to be passed to the RMI compiler
147      * @param opts list of options
148      */

149     public void setRmicopts(String JavaDoc opts) {
150         rmicOpts = opts;
151     }
152
153     /**
154      * Validate XML descriptors
155      * @param v on/off
156      */

157     public void setValidation(boolean v) {
158         validation = v;
159     }
160
161     /**
162      * Set the javac command line to be used
163      * @param j path to javac executable
164      */

165     public void setJavac(String JavaDoc j) {
166         javac = j;
167     }
168
169     /**
170      * Set the options to be given to javac
171      * @param opts options
172      */

173     public void setJavacopts(String JavaDoc opts) {
174         javacOpts = opts;
175     }
176
177     /**
178      * Keep already generated files
179      * @param k on/off
180      */

181     public void setKeepgenerated(boolean k) {
182         keepGen = k;
183     }
184
185     /**
186      * Set the set of protocols for the generation
187      * @param p protocol list (comma separated)
188      */

189     public void setProtocols(String JavaDoc p) {
190         protocols = p;
191     }
192
193     /**
194      * Execute the WsGen Ant Task.
195      * @throws BuildException if something goes wrong
196      */

197     public void execute() throws BuildException {
198
199         // avoid a -n jonas in the GenIC command line
200
setServerName(null);
201
202         for (Iterator JavaDoc fsIterator = filesets.iterator(); fsIterator.hasNext();) {
203             FileSet set = (FileSet) fsIterator.next();
204             DirectoryScanner ds = set.getDirectoryScanner(getProject());
205             ds.scan();
206             String JavaDoc[] files = ds.getIncludedFiles();
207             File JavaDoc srcDirectory = set.getDir(getProject());
208             for (int i = 0; i < files.length; i++) {
209
210                 Java genic = getBootstraptask(GENIC_CLASS);
211
212                 configureGenIC(genic, srcDirectory + File.separator + files[i]);
213
214                 // calling GenIC task
215
log("Calling GenIC task for '" + srcDirectory + File.separator + files[i] + "'.", Project.MSG_VERBOSE);
216
217                 if (genic.executeJava() != 0) {
218                     throw new BuildException("GenIC reported an error.");
219                 }
220             }
221         }
222
223     }
224
225     /**
226      * @param genicJavaTask GenIC Task to be configured for GenIC
227      * @param filename name of the file to pass into GenIC
228      * @return a configured Java Task
229      * @throws BuildException if something goes wrong
230      */

231     private Java configureGenIC(Java genicJavaTask, String JavaDoc filename) throws BuildException {
232
233         // keepgenerated
234
if (keepGen) {
235             genicJavaTask.createArg().setValue("-keepgenerated");
236         }
237
238         // novalidation
239
if (!validation) {
240             genicJavaTask.createArg().setValue("-novalidation");
241         }
242
243         // classpath
244
if (libraryClasspath != null) {
245             genicJavaTask.createArg().setValue("-classpath");
246             genicJavaTask.createArg().setPath(libraryClasspath);
247         }
248
249         // nocompil
250
if (nocompil) {
251             genicJavaTask.createArg().setValue("-nocompil");
252         }
253
254         // invokecmd
255
if (invokeCmd) {
256             genicJavaTask.createArg().setValue("-invokecmd");
257         }
258
259         // javac
260
if (javac != null) {
261             genicJavaTask.createArg().setValue("-javac");
262             genicJavaTask.createArg().setLine(javac);
263         }
264
265         // javacopts
266
if (javacOpts != null && !javacOpts.equals("")) {
267             genicJavaTask.createArg().setValue("-javacopts");
268             genicJavaTask.createArg().setLine(javacOpts);
269         }
270
271         // rmicopts
272
if (rmicOpts != null && !rmicOpts.equals("")) {
273             genicJavaTask.createArg().setValue("-rmicopts");
274             genicJavaTask.createArg().setValue(rmicOpts);
275         }
276
277         // verbose
278
if (verbose) {
279             genicJavaTask.createArg().setValue("-verbose");
280         }
281
282         // debug
283
if (debug) {
284             genicJavaTask.createJvmarg().setLine("-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=12345,suspend=y");
285         }
286
287         // additionalargs
288
if (additionalArgs != null) {
289             genicJavaTask.createArg().setLine(additionalArgs);
290         }
291
292         // protocols
293
if (protocols != null) {
294             genicJavaTask.createArg().setValue("-protocols");
295             genicJavaTask.createArg().setValue(protocols);
296         }
297
298         // input file to process by GenIC
299
genicJavaTask.createArg().setValue(filename);
300
301         return genicJavaTask;
302
303     }
304
305 }
306
Popular Tags