KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > WsGen


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: WsGen.java,v 1.27 2005/04/27 12:47:40 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ws.wsgen;
27
28 import java.io.File JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import org.objectweb.jonas_lib.genbase.archive.Archive;
32 import org.objectweb.jonas_lib.genbase.generator.Config;
33 import org.objectweb.jonas_lib.genbase.generator.GeneratorFactories;
34 import org.objectweb.jonas_lib.genbase.modifier.ArchiveModifier;
35
36 import org.objectweb.jonas_lib.genbase.utils.TempRepository;
37
38 import org.objectweb.jonas_ws.wsgen.generator.GeneratorFactory;
39 import org.objectweb.jonas_ws.wsgen.modifier.ModifierFactory;
40
41 import org.objectweb.jonas.common.Log;
42
43 import org.objectweb.util.monolog.api.BasicLevel;
44 import org.objectweb.util.monolog.api.Logger;
45
46 /**
47  * Main WsGen Class.
48  * @author Guillaume Sauthier
49  */

50 public class WsGen {
51
52     /** logger */
53     private static Logger logger = Log.getLogger(Log.JONAS_WSGEN_PREFIX);
54
55     /**
56      * Set to false if input file cannot be processed by WsGen (DTDs).
57      */

58     private boolean inputModified = true;
59
60     /**
61      * Private Constructor for Utility class WsGen.
62      */

63     public WsGen() { }
64
65     /**
66      * Main method.
67      * @param args Commend line args
68      * @throws Exception When the execute method fails
69      */

70     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
71         WsGen wsgen = new WsGen();
72         wsgen.execute(args);
73     }
74
75     /**
76      * Method used by main method and by external class to retrieve the name of
77      * the modified or created file by WsGen
78      * @param args command line arguments
79      * @return the name of the modified or built
80      * @throws Exception When a failure occurs
81      */

82     public String JavaDoc execute(String JavaDoc[] args) throws Exception JavaDoc {
83         GeneratorFactory gf = GeneratorFactory.getInstance();
84
85         // store configuration properties
86
Config config = parseInput(args);
87
88         if (config.isHelp() || config.isError() || config.getInputname() == null) {
89             // display usage message
90
usage();
91             return null;
92         }
93
94         // Make sure that DTD is turned to off
95
config.setDTDsAllowed(false);
96
97         // setup configuration
98
gf.setConfiguration(config);
99         GeneratorFactories.setCurrentFactory(gf);
100         Archive modifiedArchive = null;
101         ArchiveModifier am = null;
102         // get the ArchiveModifier
103
try {
104             am = ModifierFactory.getModifier(config.getInputname());
105
106             logger.log(BasicLevel.INFO, "WebServices generation for '" + config.getInputname() + "'");
107
108             // modify the given archive
109
modifiedArchive = am.modify();
110
111             // delete all the temporary files created
112
TempRepository.getInstance().deleteAll();
113
114             return modifiedArchive.getRootFile().getCanonicalPath();
115
116         } catch (NoJ2EEWebservicesException e) {
117             logger.log(BasicLevel.WARN, config.getInputname() + " is using DTDs, WsGen needs Schema only : "
118                     + e.getMessage());
119             inputModified = false;
120             return config.getInputname();
121         } finally {
122             // close archive
123
if (modifiedArchive != null) {
124                 modifiedArchive.close();
125             }
126             modifiedArchive = null;
127             am = null;
128
129             // delete all the temporary files created
130
TempRepository.getInstance().deleteAll();
131         }
132
133     }
134
135     /**
136      * Parse Command Line input and returns a Config object
137      * @param args Command Lines params
138      * @return a Config object
139      */

140     private static Config parseInput(String JavaDoc[] args) {
141         Config config = new Config();
142
143         // Get args
144
for (int argn = 0; argn < args.length; argn++) {
145             String JavaDoc arg = args[argn];
146
147             if (arg.equals("-help") || arg.equals("-?")) {
148                 config.setHelp(true);
149
150                 continue;
151             }
152
153             if (arg.equals("-verbose")) {
154                 config.setVerbose(true);
155
156                 continue;
157             }
158
159             if (arg.equals("-debug")) {
160                 config.setDebug(true);
161                 config.setVerbose(true);
162
163                 continue;
164             }
165
166             if (arg.equals("-keepgenerated")) {
167                 config.setKeepGenerated(true);
168
169                 continue;
170             }
171
172             if (arg.equals("-noconfig")) {
173                 config.setNoConfig(true);
174
175                 continue;
176             }
177
178             if (arg.equals("-novalidation")) {
179                 config.setParseWithValidation(false);
180
181                 continue;
182             }
183
184             if (arg.equals("-javac")) {
185                 config.setNameJavac(args[++argn]);
186
187                 continue;
188             }
189
190             if (arg.equals("-javacopts")) {
191                 argn++;
192
193                 if (argn < args.length) {
194                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args[argn]);
195
196                     while (st.hasMoreTokens()) {
197                         config.getJavacOpts().add(st.nextToken());
198                     }
199                 } else {
200                     config.setError(true);
201                 }
202
203                 continue;
204             }
205
206             if (arg.equals("-d")) {
207                 argn++;
208
209                 if (argn < args.length) {
210                     config.setOut(new File JavaDoc(args[argn]));
211                 } else {
212                     config.setError(true);
213                 }
214
215                 continue;
216             }
217
218             if (args[argn] != null) {
219                 config.setInputname(args[argn]);
220             } else {
221                 config.setError(true);
222             }
223         }
224
225         return config;
226
227     }
228
229     /**
230      * Display the usage
231      */

232     public static void usage() {
233         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
234         msg.append("Usage: java org.objectweb.jonas_ws.wsgen.WsGen -help \n");
235         msg.append(" to print this help message \n");
236         msg.append(" or java org.objectweb.jonas_ws.wsgen.WsGen <Options> <Input_File> \n");
237         msg.append("Options include: \n");
238         msg.append(" -d <output_dir> specify where to place the generated files \n");
239         msg.append(" -novalidation parse the XML deployment descriptors without \n");
240         msg.append(" validation \n");
241         msg.append(" -javac <opt> specify the java compiler to use \n");
242         msg.append(" -javacopts <opt> specify the options to pass to the java compiler \n");
243         msg.append(" -keepgenerated do not delete intermediate generated files \n");
244         msg.append(" -noconfig do not generate configuration files (require \n");
245         msg.append(" user provided files) \n");
246         msg.append(" -verbose \n");
247         msg.append(" -debug \n");
248         msg.append(" \n");
249         msg.append(" Input_File the ejb-jar, war or ear filename\n");
250         System.out.println(msg.toString());
251     }
252
253     /**
254      * @return Returns the inputModified.
255      */

256     public boolean isInputModified() {
257         return inputModified;
258     }
259 }
Popular Tags