001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: GenICTask.java 7508 2005-10-14 12:44:23Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ant;
025:
026: import java.io.File;
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030: import org.apache.tools.ant.BuildException;
031: import org.apache.tools.ant.DirectoryScanner;
032: import org.apache.tools.ant.Project;
033: import org.apache.tools.ant.taskdefs.Java;
034: import org.apache.tools.ant.types.FileSet;
035: import org.apache.tools.ant.types.Path;
036:
037: /**
038: * GenIC Task. That's basically an Ant Task wrapper around GenIC.
039: *
040: * @author Guillaume Sauthier
041: */
042: public class GenICTask extends BootstrapTask {
043:
044: /** WsGen class name */
045: private static final String GENIC_CLASS = "org.objectweb.jonas_ejb.genic.GenIC";
046:
047: /** validation of XML files ? */
048: private boolean validation = true;
049:
050: /** name of javac command */
051: private String javac = null;
052:
053: /** list of javac options */
054: private String javacOpts = null;
055:
056: /** will WsGen keep already generated files ? */
057: private boolean keepGen = false;
058:
059: /**
060: * specifies which RMIC compiler to user: the built-in one or the external
061: * one
062: */
063: private boolean noFastRMIC = false;
064:
065: /** protocol list */
066: private String protocols = null;
067:
068: /** nocompil */
069: private boolean nocompil = false;
070:
071: /** Invoke Javac with tools.jar */
072: private boolean invokeCmd = false;
073:
074: /** Options for rmic compiler */
075: private String rmicOpts = null;
076:
077: /** extra arguments to be passed to GenIC */
078: private String additionalArgs = null;
079:
080: /** verbose mode */
081: private boolean verbose = false;
082:
083: /** additionnal classpath for libs */
084: private Path libraryClasspath = null;
085:
086: /** inner FileSet list */
087: private List filesets = new ArrayList();
088:
089: /** debug field */
090: private boolean debug = false;
091:
092: /**
093: * @return Returns an empty Path for inner element classpath
094: */
095: public Path createClasspath() {
096: libraryClasspath = new Path(getProject());
097: return libraryClasspath;
098: }
099:
100: /**
101: * @return Returns an empty FileSet
102: */
103: public FileSet createFileSet() {
104: FileSet set = new FileSet();
105: set.setProject(getProject());
106: filesets.add(set);
107: return set;
108: }
109:
110: /**
111: * Set additional arguments for GenIC command line
112: * @param added additional args
113: */
114: public void setAdditionalargs(String added) {
115: additionalArgs = added;
116: }
117:
118: /**
119: * Set verbose mode on/off
120: * @param v boolean
121: */
122: public void setVerbose(boolean v) {
123: verbose = v;
124: }
125:
126: /**
127: * Set debug mode on/off. Used only by developpers that wants to Debug GenIC
128: * @param d boolean
129: */
130: public void setJvmdebug(boolean d) {
131: debug = d;
132: }
133:
134: /**
135: * Use InvokeCmd option on/off
136: * @param inv boolean
137: */
138: public void setInvokecmd(boolean inv) {
139: invokeCmd = inv;
140: }
141:
142: /**
143: * Do not compile generated java files
144: * @param noc on/off
145: */
146: public void setNocompil(boolean noc) {
147: nocompil = noc;
148: }
149:
150: /**
151: * Set the optios to be passed to the RMI compiler
152: * @param opts list of options
153: */
154: public void setRmicopts(String opts) {
155: rmicOpts = opts;
156: }
157:
158: /**
159: * Validate XML descriptors
160: * @param v on/off
161: */
162: public void setValidation(boolean v) {
163: validation = v;
164: }
165:
166: /**
167: * Set the javac command line to be used
168: * @param j path to javac executable
169: */
170: public void setJavac(String j) {
171: javac = j;
172: }
173:
174: /**
175: * Set the options to be given to javac
176: * @param opts options
177: */
178: public void setJavacopts(String opts) {
179: javacOpts = opts;
180: }
181:
182: /**
183: * Keep already generated files
184: * @param k on/off
185: */
186: public void setKeepgenerated(boolean k) {
187: keepGen = k;
188: }
189:
190: /**
191: * Specifies which RMIC compiler to use: the built-in fast one or the
192: * slower external one.
193: * @param value if true, use the external RMIC compiler
194: */
195: public void setNoFastRMIC(boolean value) {
196: noFastRMIC = value;
197: }
198:
199: /**
200: * Set the set of protocols for the generation
201: * @param p protocol list (comma separated)
202: */
203: public void setProtocols(String p) {
204: protocols = p;
205: }
206:
207: /**
208: * Execute the WsGen Ant Task.
209: * @throws BuildException if something goes wrong
210: */
211: public void execute() throws BuildException {
212:
213: // avoid a -n jonas in the GenIC command line
214: setServerName(null);
215:
216: for (Iterator fsIterator = filesets.iterator(); fsIterator
217: .hasNext();) {
218: FileSet set = (FileSet) fsIterator.next();
219: DirectoryScanner ds = set.getDirectoryScanner(getProject());
220: ds.scan();
221: String[] files = ds.getIncludedFiles();
222: File srcDirectory = set.getDir(getProject());
223: for (int i = 0; i < files.length; i++) {
224:
225: Java genic = getBootstraptask(GENIC_CLASS);
226:
227: configureGenIC(genic, srcDirectory + File.separator
228: + files[i]);
229:
230: // calling GenIC task
231: log("Calling GenIC task for '" + srcDirectory
232: + File.separator + files[i] + "'.",
233: Project.MSG_VERBOSE);
234:
235: if (genic.executeJava() != 0) {
236: throw new BuildException("GenIC reported an error.");
237: }
238: }
239: }
240:
241: }
242:
243: /**
244: * @param genicJavaTask GenIC Task to be configured for GenIC
245: * @param filename name of the file to pass into GenIC
246: * @return a configured Java Task
247: * @throws BuildException if something goes wrong
248: */
249: private Java configureGenIC(Java genicJavaTask, String filename)
250: throws BuildException {
251:
252: // keepgenerated
253: if (keepGen) {
254: genicJavaTask.createArg().setValue("-keepgenerated");
255: }
256:
257: if (noFastRMIC) {
258: genicJavaTask.createArg().setValue("-nofastrmic");
259: }
260:
261: // novalidation
262: if (!validation) {
263: genicJavaTask.createArg().setValue("-novalidation");
264: }
265:
266: // classpath
267: if (libraryClasspath != null) {
268: genicJavaTask.createArg().setValue("-classpath");
269: genicJavaTask.createArg().setPath(libraryClasspath);
270: }
271:
272: // nocompil
273: if (nocompil) {
274: genicJavaTask.createArg().setValue("-nocompil");
275: }
276:
277: // invokecmd
278: if (invokeCmd) {
279: genicJavaTask.createArg().setValue("-invokecmd");
280: }
281:
282: // javac
283: if (javac != null) {
284: genicJavaTask.createArg().setValue("-javac");
285: genicJavaTask.createArg().setLine(javac);
286: }
287:
288: // javacopts
289: if (javacOpts != null && !javacOpts.equals("")) {
290: genicJavaTask.createArg().setValue("-javacopts");
291: genicJavaTask.createArg().setLine(javacOpts);
292: }
293:
294: // rmicopts
295: if (rmicOpts != null && !rmicOpts.equals("")) {
296: genicJavaTask.createArg().setValue("-rmicopts");
297: genicJavaTask.createArg().setValue(rmicOpts);
298: }
299:
300: // verbose
301: if (verbose) {
302: genicJavaTask.createArg().setValue("-verbose");
303: }
304:
305: // debug
306: if (debug) {
307: this
308: .log(
309: "Launching in debug mode on port 12345, waiting for connection ...",
310: Project.MSG_INFO);
311: genicJavaTask
312: .createJvmarg()
313: .setLine(
314: "-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=12345,suspend=y");
315: }
316:
317: // additionalargs
318: if (additionalArgs != null) {
319: genicJavaTask.createArg().setLine(additionalArgs);
320: }
321:
322: // protocols
323: if (protocols != null) {
324: genicJavaTask.createArg().setValue("-protocols");
325: genicJavaTask.createArg().setValue(protocols);
326: }
327:
328: // input file to process by GenIC
329: genicJavaTask.createArg().setValue(filename);
330:
331: return genicJavaTask;
332:
333: }
334:
335: }
|