001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-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: WsGenTask.java 7508 2005-10-14 12:44:23Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ant;
025:
026: import java.io.File;
027:
028: import org.apache.tools.ant.AntClassLoader;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.DirectoryScanner;
031: import org.apache.tools.ant.Project;
032: import org.apache.tools.ant.taskdefs.Java;
033: import org.apache.tools.ant.taskdefs.MatchingTask;
034: import org.apache.tools.ant.types.Path;
035:
036: /**
037: * Launch WsGen from Ant.
038: *
039: * @author Guillaume Sauthier
040: */
041: public class WsGenTask extends MatchingTask {
042:
043: /** Bootstrap class name. */
044: private static final String BOOTSTRAP_CLASS = "org.objectweb.jonas.server.Bootstrap";
045:
046: /** WsGen class name (JOnAS 3.3). */
047: private static final String WSGEN_CLASS = "org.objectweb.jonas_ws.wsgen.WsGen";
048:
049: /** destination directory */
050: private File destination = null;
051:
052: /** source directory */
053: private File source = null;
054:
055: /** validation of XML files ? */
056: private boolean validation = true;
057:
058: /** name of javac command */
059: private String javac = null;
060:
061: /** list of javac options */
062: private String javacOpts = null;
063:
064: /** will WsGen keep already generated files ? */
065: private boolean keepGenerated = false;
066:
067: /** Will WsGen create configuration files ? */
068: private boolean noConfig = false;
069:
070: /** verbose mode */
071: private boolean verbose = false;
072:
073: /** debug mode */
074: private boolean debug = false;
075:
076: /** JVM debug mode */
077: private boolean jvmDebug = false;
078:
079: /** $JONAS_ROOT */
080: private File jonasRoot = null;
081:
082: /** $JONAS_BASE */
083: private File jonasBase = null;
084:
085: /**
086: * Set the output Directory
087: * @param d output dir
088: */
089: public void setDestdir(File d) {
090: destination = d;
091: }
092:
093: /**
094: * Set where source jar files are located
095: * @param s sources dir
096: */
097: public void setSrcdir(File s) {
098: source = s;
099: }
100:
101: /**
102: * Set XML Validation on/off
103: * @param v on/off
104: */
105: public void setValidation(boolean v) {
106: validation = v;
107: }
108:
109: /**
110: * Set the javac command line
111: * @param j javac to use
112: */
113: public void setJavac(String j) {
114: javac = j;
115: }
116:
117: /**
118: * Set Java Compiler Options
119: * @param opts Options
120: */
121: public void setJavacopts(String opts) {
122: javacOpts = opts;
123: }
124:
125: /**
126: * Set keep Generated mode to on/off
127: * @param k on/off
128: */
129: public void setKeepgen(boolean k) {
130: keepGenerated = k;
131: }
132:
133: /**
134: * Set noConfig mode to on/off
135: * @param c on/off
136: */
137: public void setNoconfig(boolean c) {
138: noConfig = c;
139: }
140:
141: /**
142: * Set WSDL2Java verbose mode to on/off
143: * @param v on/off
144: */
145: public void setVerbose(boolean v) {
146: verbose = v;
147: }
148:
149: /**
150: * Set WSDL2Java debug mode to on/off
151: * @param b on/off
152: */
153: public void setDebug(boolean b) {
154: debug = b;
155: }
156:
157: /**
158: * Set debug mode on/off. Used only by developpers that wants to Debug GenIC
159: * @param d boolean
160: */
161: public void setJvmdebug(boolean d) {
162: jvmDebug = d;
163: }
164:
165: /**
166: * Set the JONAS_ROOT to use
167: * @param jr JONAS_ROOT
168: */
169: public void setJonasroot(File jr) {
170: jonasRoot = jr;
171: }
172:
173: /**
174: * Set the JONAS_BASE to use
175: * @param jb JONAS_BASE
176: */
177: public void setJonasbase(File jb) {
178: jonasBase = jb;
179: }
180:
181: /**
182: * Execute the WsGen Ant Task.
183: * @throws BuildException if wsgen cannot finish
184: */
185: public void execute() throws BuildException {
186:
187: // init the wsgen task
188: initWsGenTask();
189:
190: // execute
191: DirectoryScanner ds = getDirectoryScanner(source);
192: ds.scan();
193: String[] files = ds.getIncludedFiles();
194:
195: for (int i = 0; i < files.length; i++) {
196:
197: Java wsgen = createWsGen(source + File.separator + files[i]);
198:
199: // calling WsGen task
200: log("Calling WsGen task for '" + source + File.separator
201: + files[i] + "'.", Project.MSG_VERBOSE);
202:
203: if (wsgen.executeJava() != 0) {
204: throw new BuildException("WsGen reported an error.");
205: }
206: }
207:
208: }
209:
210: /**
211: * Create and configure the WsGen Java task.
212: * @param filename input file name to be processed by WsGen
213: * @return the configured Java Ant Task
214: * @throws BuildException if the Java Task cannot be created
215: */
216: private Java createWsGen(String filename) throws BuildException {
217:
218: Java wsgenTask = null; // WsGen task
219:
220: wsgenTask = (Java) getProject().createTask("java");
221: wsgenTask.setTaskName("wsgen");
222: wsgenTask.setFork(true);
223:
224: // jonas root
225: wsgenTask.createJvmarg()
226: .setValue("-Dinstall.root=" + jonasRoot);
227:
228: // jonas base
229: wsgenTask.createJvmarg().setValue("-Djonas.base=" + jonasBase);
230:
231: // Endorsed directory
232: File endorsedDir = new File(new File(jonasRoot, "lib"),
233: "endorsed");
234: wsgenTask.createJvmarg().setValue(
235: "-Djava.endorsed.dirs=" + endorsedDir);
236:
237: // java policy file
238: String jonasConfigDir = jonasRoot + File.separator + "conf";
239: File javaPolicyFile = new File(jonasConfigDir, "java.policy");
240: if (javaPolicyFile.exists()) {
241: wsgenTask.createJvmarg().setValue(
242: "-Djava.security.policy="
243: + javaPolicyFile.toString());
244: }
245:
246: // JVM Debug
247: if (jvmDebug) {
248: this
249: .log(
250: "Launching in debug mode on port 12345, waiting for connection ...",
251: Project.MSG_INFO);
252: wsgenTask
253: .createJvmarg()
254: .setLine(
255: "-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=12345,suspend=y");
256: }
257:
258: // The bootstrap class must launch the GenIC class
259: wsgenTask.createArg().setValue(WSGEN_CLASS);
260:
261: wsgenTask.createArg().setValue("-d");
262: wsgenTask.createArg().setFile(destination);
263:
264: // add ow_jonas_bootstrap.jar
265: String bootJar = jonasRoot + File.separator + "lib"
266: + File.separator + "common" + File.separator
267: + "ow_jonas_bootstrap.jar";
268: Path classpath = new Path(getProject(), bootJar);
269:
270: log("Using classpath: " + classpath.toString(),
271: Project.MSG_VERBOSE);
272: wsgenTask.setClasspath(classpath);
273:
274: if (!checkBootstrapClassName(classpath)) {
275: log("Cannot find bootstrap class in classpath.",
276: Project.MSG_ERR);
277: throw new BuildException(
278: "Bootstrap class not found, please check the classpath.");
279: } else {
280: wsgenTask.setClassname(BOOTSTRAP_CLASS);
281: }
282:
283: // keepgenerated
284: if (keepGenerated) {
285: wsgenTask.createArg().setValue("-keepgenerated");
286: }
287:
288: // noconfig
289: if (noConfig) {
290: wsgenTask.createArg().setValue("-noconfig");
291: }
292:
293: // novalidation
294: if (!validation) {
295: wsgenTask.createArg().setValue("-novalidation");
296: }
297:
298: // javac
299: if (javac != null) {
300: wsgenTask.createArg().setValue("-javac");
301: wsgenTask.createArg().setLine(javac);
302: }
303:
304: // javacopts
305: if (javacOpts != null && !javacOpts.equals("")) {
306: wsgenTask.createArg().setValue("-javacopts");
307: wsgenTask.createArg().setLine(javacOpts);
308: }
309:
310: // verbose
311: if (verbose) {
312: wsgenTask.createArg().setValue("-verbose");
313: }
314:
315: // debug
316: if (debug) {
317: wsgenTask.createArg().setValue("-debug");
318: }
319:
320: // input file to process by GenIC
321: wsgenTask.createArg().setValue(filename);
322:
323: return wsgenTask;
324:
325: }
326:
327: /**
328: * Initialize build properties
329: * @throws BuildException if required properties are not set
330: */
331: private void initWsGenTask() throws BuildException {
332: // try to define jonasRoot if not set
333: if (jonasRoot == null) {
334: // get ant property
335: String jr = getProject().getProperty("jonas.root");
336: jonasRoot = new File(jr);
337:
338: if (jr == null) {
339: throw new BuildException(
340: "attribute jonasroot is necessary.");
341: }
342: }
343:
344: // use jonasRoot as jonasBase if not set
345: if (jonasBase == null) {
346: String jb = getProject().getProperty("jonas.base");
347: if (jb == null) {
348: jonasBase = jonasRoot;
349: } else {
350: jonasBase = new File(jb);
351: }
352:
353: }
354:
355: // default destination : project directory
356: if (destination == null) {
357: destination = getProject().getBaseDir();
358: }
359:
360: // javac javacOpts
361: // by default WsGen already handle them
362:
363: }
364:
365: /**
366: * Check the bootstrap class name to use in the given classpath.
367: *
368: * @param classpath classpath where the boostrap class must be searched.
369: * @return true if the bootstrap is available in the classpath
370: */
371: private boolean checkBootstrapClassName(Path classpath) {
372: log("Looking for bootstrap class in classpath: "
373: + classpath.toString(), Project.MSG_VERBOSE);
374: AntClassLoader cl = new AntClassLoader(classpath.getProject(),
375: classpath);
376: try {
377: cl.loadClass(BOOTSTRAP_CLASS);
378: log("Found Bootstrap class '" + BOOTSTRAP_CLASS
379: + "' in classpath.", Project.MSG_VERBOSE);
380: } catch (ClassNotFoundException cnf1) {
381: log("Bootstrap class '" + BOOTSTRAP_CLASS
382: + "' not found in classpath.", Project.MSG_VERBOSE);
383: return false;
384: }
385: return true;
386: }
387:
388: }
|