001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004-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: ClientStubGen.java 6658 2005-04-27 12:47:40Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.genclientstub;
025:
026: import java.io.File;
027: import java.util.StringTokenizer;
028:
029: import org.objectweb.jonas_lib.genbase.archive.Archive;
030: import org.objectweb.jonas_lib.genbase.generator.Config;
031: import org.objectweb.jonas_lib.genbase.generator.GeneratorFactories;
032: import org.objectweb.jonas_lib.genbase.modifier.ArchiveModifier;
033: import org.objectweb.jonas_lib.genbase.utils.TempRepository;
034: import org.objectweb.jonas_lib.genclientstub.generator.GeneratorFactory;
035: import org.objectweb.jonas_lib.genclientstub.modifier.ModifierFactory;
036:
037: import org.objectweb.jonas_ws.wsgen.NoJ2EEWebservicesException;
038:
039: import org.objectweb.jonas.common.Log;
040:
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: * Main class of the client stub generator Class. Some code come from WsGen
046: * classes (Guillaume Sauthier)
047: * @author Florent Benoit
048: */
049: public class ClientStubGen {
050:
051: /**
052: * logger
053: */
054: private static Logger logger = Log
055: .getLogger(Log.JONAS_CLIENTSTUBGEN_PREFIX);
056:
057: /**
058: * Set to false if input file cannot be processed (DTDs).
059: */
060: private boolean inputModified = true;
061:
062: /**
063: * Private Constructor for Utility class.
064: */
065: public ClientStubGen() {
066: }
067:
068: /**
069: * Main method.
070: * @param args Commend line args
071: * @throws Exception When the execute method fails
072: */
073: public static void main(String[] args) throws Exception {
074: ClientStubGen stubGen = new ClientStubGen();
075: stubGen.execute(args);
076: }
077:
078: /**
079: * Method used by main method and by external class to retrieve the name of
080: * the modified or created file by ClientStubGen
081: * @param args command line arguments
082: * @return the name of the modified or built
083: * @throws Exception When a failure occurs
084: */
085: public String execute(String[] args) throws Exception {
086: GeneratorFactory gf = GeneratorFactory.getInstance();
087:
088: // store configuration properties
089: Config config = parseInput(args);
090:
091: if (config.isHelp() || config.isError()
092: || config.getInputname() == null) {
093: // display usage message
094: usage();
095: return null;
096: }
097:
098: // For this client generator, the use of DTDs is allowed
099: 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 '"
112: + config.getInputname() + "'");
113:
114: // modify the given archive
115: modifiedArchive = am.modify();
116:
117: // Get path
118: String path = modifiedArchive.getRootFile()
119: .getCanonicalPath();
120:
121: return path;
122: } catch (NoJ2EEWebservicesException e) {
123: logger.log(BasicLevel.WARN, config.getInputname()
124: + "Error while generating stubs : '"
125: + e.getMessage() + "'");
126: inputModified = false;
127: return config.getInputname();
128: } finally {
129: // close archive
130: if (modifiedArchive != null) {
131: modifiedArchive.close();
132: }
133: modifiedArchive = null;
134: am = null;
135:
136: // delete all the temporary files created
137: TempRepository.getInstance().deleteAll();
138: }
139:
140: }
141:
142: /**
143: * Parse Command Line input and returns a Config object
144: * @param args Command Lines params
145: * @return a Config object
146: */
147: private static Config parseInput(String[] args) {
148: Config config = new Config();
149:
150: // Get args
151: for (int argn = 0; argn < args.length; argn++) {
152: String arg = args[argn];
153:
154: if (arg.equals("-help") || arg.equals("-?")) {
155: config.setHelp(true);
156:
157: continue;
158: }
159:
160: if (arg.equals("-verbose")) {
161: config.setVerbose(true);
162:
163: continue;
164: }
165:
166: if (arg.equals("-debug")) {
167: config.setDebug(true);
168: config.setVerbose(true);
169:
170: continue;
171: }
172:
173: if (arg.equals("-keepgenerated")) {
174: config.setKeepGenerated(true);
175:
176: continue;
177: }
178:
179: if (arg.equals("-noconfig")) {
180: config.setNoConfig(true);
181:
182: continue;
183: }
184:
185: if (arg.equals("-novalidation")) {
186: config.setParseWithValidation(false);
187:
188: continue;
189: }
190:
191: if (arg.equals("-javac")) {
192: config.setNameJavac(args[++argn]);
193:
194: continue;
195: }
196:
197: if (arg.equals("-javacopts")) {
198: argn++;
199:
200: if (argn < args.length) {
201: StringTokenizer st = new StringTokenizer(args[argn]);
202:
203: while (st.hasMoreTokens()) {
204: config.getJavacOpts().add(st.nextToken());
205: }
206: } else {
207: config.setError(true);
208: }
209:
210: continue;
211: }
212:
213: if (arg.equals("-d")) {
214: argn++;
215:
216: if (argn < args.length) {
217: config.setOut(new File(args[argn]));
218: } else {
219: config.setError(true);
220: }
221:
222: continue;
223: }
224:
225: if (args[argn] != null) {
226: config.setInputname(args[argn]);
227: } else {
228: config.setError(true);
229: }
230: }
231:
232: return config;
233:
234: }
235:
236: /**
237: * Display the usage
238: */
239: public static void usage() {
240: StringBuffer msg = new StringBuffer();
241: msg
242: .append("Usage: java org.objectweb.jonas_lib.genclientstub.ClientStubgen -help \n");
243: msg.append(" to print this help message \n");
244: msg
245: .append(" or java org.objectweb.jonas_lib.genclientstub.ClientStubgen <Options> <Input_File> \n");
246: msg.append("Options include: \n");
247: msg
248: .append(" -d <output_dir> specify where to place the generated files \n");
249: msg
250: .append(" -novalidation parse the XML deployment descriptors without \n");
251: msg.append(" validation \n");
252: msg
253: .append(" -javac <opt> specify the java compiler to use \n");
254: msg
255: .append(" -javacopts <opt> specify the options to pass to the java compiler \n");
256: msg
257: .append(" -keepgenerated do not delete intermediate generated files \n");
258: msg
259: .append(" -noconfig do not generate configuration files (require \n");
260: msg.append(" user provided files) \n");
261: msg.append(" -verbose \n");
262: msg.append(" -debug \n");
263: msg.append(" \n");
264: msg
265: .append(" Input_File the ejb-jar, war or ear filename\n");
266: System.out.println(msg.toString());
267: }
268:
269: /**
270: * @return the inputModified flag
271: */
272: public boolean isInputModified() {
273: return inputModified;
274: }
275: }
|