KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genclientstub > ClientStubGen


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004-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: ClientStubGen.java,v 1.6 2005/04/27 12:47:40 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genclientstub;
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 import org.objectweb.jonas_lib.genbase.utils.TempRepository;
36 import org.objectweb.jonas_lib.genclientstub.generator.GeneratorFactory;
37 import org.objectweb.jonas_lib.genclientstub.modifier.ModifierFactory;
38
39 import org.objectweb.jonas_ws.wsgen.NoJ2EEWebservicesException;
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 class of the client stub generator Class. Some code come from WsGen
48  * classes (Guillaume Sauthier)
49  * @author Florent Benoit
50  */

51 public class ClientStubGen {
52
53     /**
54      * logger
55      */

56     private static Logger logger = Log.getLogger(Log.JONAS_CLIENTSTUBGEN_PREFIX);
57
58     /**
59      * Set to false if input file cannot be processed (DTDs).
60      */

61     private boolean inputModified = true;
62
63     /**
64      * Private Constructor for Utility class.
65      */

66     public ClientStubGen() {
67     }
68
69     /**
70      * Main method.
71      * @param args Commend line args
72      * @throws Exception When the execute method fails
73      */

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

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

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

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

260     public boolean isInputModified() {
261         return inputModified;
262     }
263 }
Popular Tags