KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-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: WsGenTask.java,v 1.6 2005/03/09 12:55:50 sauthieg Exp $
23  * --------------------------------------------------------------------------
24 */

25 package org.objectweb.jonas.ant;
26
27 import java.io.File JavaDoc;
28
29 import org.apache.tools.ant.AntClassLoader;
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.DirectoryScanner;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.taskdefs.Java;
34 import org.apache.tools.ant.taskdefs.MatchingTask;
35 import org.apache.tools.ant.types.Path;
36
37 /**
38  * Launch WsGen from Ant.
39  *
40  * @author Guillaume Sauthier
41  */

42 public class WsGenTask extends MatchingTask {
43
44     /** Bootstrap class name. */
45     private static final String JavaDoc BOOTSTRAP_CLASS = "org.objectweb.jonas.server.Bootstrap";
46
47     /** WsGen class name (JOnAS 3.3). */
48     private static final String JavaDoc WSGEN_CLASS = "org.objectweb.jonas_ws.wsgen.WsGen";
49
50     /** destination directory */
51     private File JavaDoc destination = null;
52
53     /** source directory */
54     private File JavaDoc source = null;
55
56     /** validation of XML files ? */
57     private boolean validation = true;
58
59     /** name of javac command */
60     private String JavaDoc javac = null;
61
62     /** list of javac options */
63     private String JavaDoc javac_opts = null;
64
65     /** will WsGen keep already generated files ? */
66     private boolean keep_gen = false;
67
68     /** Will WsGen create configuration files ? */
69     private boolean no_config = false;
70
71     /** verbose mode */
72     private boolean verbose = false;
73
74     /** debug mode */
75     private boolean debug = false;
76
77     /** JVM debug mode */
78     private boolean jvmDebug = false;
79
80     /** $JONAS_ROOT */
81     private File JavaDoc jonas_root = null;
82
83     /** $JONAS_BASE */
84     private File JavaDoc jonas_base = null;
85
86     public void setDestdir(File JavaDoc d) {
87         destination = d;
88     }
89
90     public void setSrcdir(File JavaDoc s) {
91         source = s;
92     }
93
94     public void setValidation(boolean v) {
95         validation = v;
96     }
97
98     public void setJavac(String JavaDoc j) {
99         javac = j;
100     }
101
102     public void setJavacopts(String JavaDoc opts) {
103         javac_opts = opts;
104     }
105
106     public void setKeepgen(boolean k) {
107         keep_gen = k;
108     }
109
110     public void setNoconfig(boolean c) {
111         no_config = c;
112     }
113
114     public void setVerbose(boolean v) {
115         verbose = v;
116     }
117
118     public void setDebug(boolean b) {
119         debug = b;
120     }
121
122     /**
123      * Set debug mode on/off. Used only by developpers that wants to Debug GenIC
124      * @param d boolean
125      */

126     public void setJvmdebug(boolean d) {
127         jvmDebug = d;
128     }
129
130     public void setJonasroot(File JavaDoc jr) {
131         jonas_root = jr;
132     }
133
134     public void setJonasbase(File JavaDoc jb) {
135         jonas_base = jb;
136     }
137
138     /**
139      * Execute the WsGen Ant Task.
140      */

141     public void execute() throws BuildException {
142
143         // init the wsgen task
144
initWsGenTask();
145
146         // execute
147
DirectoryScanner ds = getDirectoryScanner(source);
148         ds.scan();
149         String JavaDoc[] files = ds.getIncludedFiles();
150
151         for (int i = 0; i < files.length; i++) {
152             
153             Java wsgen = createWsGen(source + File.separator + files[i]);
154             
155             // calling WsGen task
156
log("Calling WsGen task for '" + source + File.separator + files[i] + "'.", Project.MSG_VERBOSE);
157             
158             if (wsgen.executeJava() != 0) {
159                 throw new BuildException("WsGen reported an error.");
160             }
161         }
162
163     }
164
165     private Java createWsGen(String JavaDoc filename) throws BuildException {
166
167         Java wsgenTask = null; // WsGen task
168
boolean error = false; // true if an error occurs during the GenIC call
169

170         wsgenTask = (Java) getProject().createTask("java");
171         wsgenTask.setTaskName("wsgen");
172         wsgenTask.setFork(true);
173
174         // jonas root
175
wsgenTask.createJvmarg().setValue("-Dinstall.root=" + jonas_root);
176
177         // jonas base
178
wsgenTask.createJvmarg().setValue("-Djonas.base=" + jonas_base);
179
180         // Endorsed directory
181
File JavaDoc endorsedDir = new File JavaDoc(new File JavaDoc(jonas_root, "lib"), "endorsed");
182         wsgenTask.createJvmarg().setValue("-Djava.endorsed.dirs=" + endorsedDir);
183
184         // java policy file
185
String JavaDoc jonasConfigDir = jonas_root + File.separator + "conf";
186         File JavaDoc javaPolicyFile = new File JavaDoc(jonasConfigDir, "java.policy");
187         if (javaPolicyFile.exists()) {
188             wsgenTask.createJvmarg().setValue("-Djava.security.policy="
189                                               + javaPolicyFile.toString());
190         }
191
192         // JVM Debug
193
if (jvmDebug) {
194             wsgenTask.createJvmarg().setLine("-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=12345,suspend=y");
195         }
196
197         // The bootstrap class must launch the GenIC class
198
wsgenTask.createArg().setValue(WSGEN_CLASS);
199
200         wsgenTask.createArg().setValue("-d");
201         wsgenTask.createArg().setFile(destination);
202
203         // add ow_jonas_bootstrap.jar
204
String JavaDoc bootJar = jonas_root + File.separator + "lib" + File.separator + "common"
205             + File.separator + "ow_jonas_bootstrap.jar";
206         Path classpath = new Path(getProject(), bootJar);
207       
208         log("Using classpath: " + classpath.toString(), Project.MSG_VERBOSE);
209         wsgenTask.setClasspath(classpath);
210
211         if (!checkBootstrapClassName(classpath)) {
212             log("Cannot find bootstrap class in classpath.", Project.MSG_ERR);
213             throw new BuildException("Bootstrap class not found, please check the classpath.");
214         } else {
215             wsgenTask.setClassname(BOOTSTRAP_CLASS);
216         }
217
218         // keepgenerated
219
if (keep_gen) {
220             wsgenTask.createArg().setValue("-keepgenerated");
221         }
222
223         // noconfig
224
if (no_config) {
225             wsgenTask.createArg().setValue("-noconfig");
226         }
227
228         // novalidation
229
if (!validation) {
230             wsgenTask.createArg().setValue("-novalidation");
231         }
232
233         // javac
234
if (javac != null) {
235             wsgenTask.createArg().setValue("-javac");
236             wsgenTask.createArg().setLine(javac);
237         }
238
239         // javacopts
240
if (javac_opts != null && !javac_opts.equals("")) {
241             wsgenTask.createArg().setValue("-javacopts");
242             wsgenTask.createArg().setLine(javac_opts);
243         }
244
245         // verbose
246
if (verbose) {
247             wsgenTask.createArg().setValue("-verbose");
248         }
249
250         // debug
251
if (debug) {
252             wsgenTask.createArg().setValue("-debug");
253         }
254
255         // input file to process by GenIC
256
wsgenTask.createArg().setValue(filename);
257
258         return wsgenTask;
259         
260     }
261
262     private void initWsGenTask() throws BuildException {
263         // try to define jonas_root if not set
264
if (jonas_root == null ) {
265             // get ant property
266
String JavaDoc jr = getProject().getProperty("jonas.root");
267             jonas_root = new File JavaDoc(jr);
268             
269             if (jr == null) {
270                 throw new BuildException("attribute jonasroot is necessary.");
271             }
272         }
273
274         // use jonas_root as jonas_base if not set
275
if (jonas_base == null) {
276             String JavaDoc jb = getProject().getProperty("jonas.base");
277             if (jb == null) {
278                 jonas_base = jonas_root;
279             } else {
280                 jonas_base = new File JavaDoc(jb);
281             }
282             
283         }
284
285         // default destination : project directory
286
if (destination == null) {
287             destination = getProject().getBaseDir();
288         }
289
290         // javac javac_opts
291
// by default WsGen already handle them
292

293     }
294     /**
295      * Check the bootstrap class name to use in the given classpath.
296      *
297      * @param classpath classpath where the boostrap class must be searched.
298      * @return true if the bootstrap is available in the classpath
299      */

300     private boolean checkBootstrapClassName(Path classpath) {
301         log("Looking for bootstrap class in classpath: " + classpath.toString(), Project.MSG_VERBOSE);
302         AntClassLoader cl = new AntClassLoader(classpath.getProject(), classpath);
303         try {
304             cl.loadClass(BOOTSTRAP_CLASS);
305             log("Found Bootstrap class '" + BOOTSTRAP_CLASS
306                 + "' in classpath.", Project.MSG_VERBOSE);
307         } catch (ClassNotFoundException JavaDoc cnf1) {
308             log("Bootstrap class '" + BOOTSTRAP_CLASS
309                 + "' not found in classpath.", Project.MSG_VERBOSE);
310             return false;
311         }
312         return true;
313     }
314
315 }
316
Popular Tags