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: WsGen.java 8253 2006-04-18 11:40:19Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen;
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:
034: import org.objectweb.jonas_lib.genbase.utils.TempRepository;
035:
036: import org.objectweb.jonas_ws.wsgen.finder.GeneralWSFinder;
037: import org.objectweb.jonas_ws.wsgen.finder.J2EEWebServicesFinder;
038: import org.objectweb.jonas_ws.wsgen.generator.GeneratorFactory;
039: import org.objectweb.jonas_ws.wsgen.modifier.ModifierFactory;
040:
041: import org.objectweb.jonas.common.Log;
042:
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: /**
047: * Main WsGen Class.
048: * @author Guillaume Sauthier
049: */
050: public class WsGen {
051:
052: /** logger */
053: private static Logger logger = Log
054: .getLogger(Log.JONAS_WSGEN_PREFIX);
055:
056: /**
057: * Set to false if input file cannot be processed by WsGen (DTDs).
058: */
059: private boolean inputModified = true;
060:
061: /**
062: * Private Constructor for Utility class WsGen.
063: */
064: public WsGen() {
065: }
066:
067: /**
068: * Main method.
069: * @param args Commend line args
070: * @throws Exception When the execute method fails
071: */
072: public static void main(String[] args) throws Exception {
073: WsGen wsgen = new WsGen();
074: wsgen.execute(args);
075: }
076:
077: /**
078: * Method used by main method and by external class to retrieve the name of
079: * the modified or created file by WsGen
080: * @param args command line arguments
081: * @return the name of the modified or built
082: * @throws Exception When a failure occurs
083: */
084: public String execute(String[] args) throws Exception {
085: GeneratorFactory gf = GeneratorFactory.getInstance();
086:
087: // store configuration properties
088: Config config = parseInput(args);
089:
090: if (config.isHelp() || config.isError()
091: || config.getInputname() == null) {
092: // display usage message
093: usage();
094: return null;
095: }
096:
097: // Make sure that DTD is turned to off
098: config.setDTDsAllowed(false);
099:
100: // setup configuration
101: gf.setConfiguration(config);
102: GeneratorFactories.setCurrentFactory(gf);
103: Archive modifiedArchive = null;
104: ArchiveModifier am = null;
105: // get the ArchiveModifier
106: try {
107: am = ModifierFactory.getModifier(config.getInputname(),
108: false);
109:
110: J2EEWebServicesFinder finder = new GeneralWSFinder(am
111: .getArchive());
112:
113: // Do not touch the archive unless necessary !
114: if (finder.find()) {
115:
116: logger.log(BasicLevel.INFO,
117: "WebServices generation for '"
118: + config.getInputname() + "'");
119:
120: am.getArchive().initialize();
121:
122: // modify the given archive
123: modifiedArchive = am.modify();
124:
125: return modifiedArchive.getRootFile().getCanonicalPath();
126: }
127:
128: } catch (NoJ2EEWebservicesException e) {
129: logger.log(BasicLevel.WARN, config.getInputname()
130: + " is using DTDs, WsGen needs Schema only : "
131: + e.getMessage());
132: inputModified = false;
133: return config.getInputname();
134: } finally {
135: // close archive
136: if (modifiedArchive != null) {
137: modifiedArchive.close();
138: }
139: modifiedArchive = null;
140: am = null;
141:
142: // delete all the temporary files created
143: TempRepository.getInstance().deleteAll();
144: }
145: // no changes needed ...
146: inputModified = false;
147: return config.getInputname();
148:
149: }
150:
151: /**
152: * Parse Command Line input and returns a Config object
153: * @param args Command Lines params
154: * @return a Config object
155: */
156: private static Config parseInput(String[] args) {
157: Config config = new Config();
158:
159: // Get args
160: for (int argn = 0; argn < args.length; argn++) {
161: String arg = args[argn];
162:
163: if (arg.equals("-help") || arg.equals("-?")) {
164: config.setHelp(true);
165:
166: continue;
167: }
168:
169: if (arg.equals("-verbose")) {
170: config.setVerbose(true);
171:
172: continue;
173: }
174:
175: if (arg.equals("-debug")) {
176: config.setDebug(true);
177: config.setVerbose(true);
178:
179: continue;
180: }
181:
182: if (arg.equals("-keepgenerated")) {
183: config.setKeepGenerated(true);
184:
185: continue;
186: }
187:
188: if (arg.equals("-noconfig")) {
189: config.setNoConfig(true);
190:
191: continue;
192: }
193:
194: if (arg.equals("-novalidation")) {
195: config.setParseWithValidation(false);
196:
197: continue;
198: }
199:
200: if (arg.equals("-javac")) {
201: config.setNameJavac(args[++argn]);
202:
203: continue;
204: }
205:
206: if (arg.equals("-javacopts")) {
207: argn++;
208:
209: if (argn < args.length) {
210: StringTokenizer st = new StringTokenizer(args[argn]);
211:
212: while (st.hasMoreTokens()) {
213: config.getJavacOpts().add(st.nextToken());
214: }
215: } else {
216: config.setError(true);
217: }
218:
219: continue;
220: }
221:
222: if (arg.equals("-d")) {
223: argn++;
224:
225: if (argn < args.length) {
226: config.setOut(new File(args[argn]));
227: } else {
228: config.setError(true);
229: }
230:
231: continue;
232: }
233:
234: if (arg.equals("-unpacked")) {
235: config.setSaveUnpacked();
236:
237: continue;
238: }
239:
240: if (args[argn] != null) {
241: config.setInputname(args[argn]);
242: } else {
243: config.setError(true);
244: }
245: }
246:
247: return config;
248:
249: }
250:
251: /**
252: * Display the usage
253: */
254: public static void usage() {
255: StringBuffer msg = new StringBuffer();
256: msg
257: .append("Usage: java org.objectweb.jonas_ws.wsgen.WsGen -help \n");
258: msg.append(" to print this help message \n");
259: msg
260: .append(" or java org.objectweb.jonas_ws.wsgen.WsGen <Options> <Input_File> \n");
261: msg.append("Options include: \n");
262: msg
263: .append(" -d <output_dir> specify where to place the generated files \n");
264: msg
265: .append(" -novalidation parse the XML deployment descriptors without \n");
266: msg.append(" validation \n");
267: msg
268: .append(" -javac <opt> specify the java compiler to use \n");
269: msg
270: .append(" -javacopts <opt> specify the options to pass to the java compiler \n");
271: msg
272: .append(" -keepgenerated do not delete intermediate generated files \n");
273: msg
274: .append(" -noconfig do not generate configuration files (require \n");
275: msg.append(" user provided files) \n");
276: msg.append(" -verbose \n");
277: msg.append(" -debug \n");
278: msg.append(" \n");
279: msg
280: .append(" Input_File the ejb-jar, war or ear filename\n");
281: System.out.println(msg.toString());
282: }
283:
284: /**
285: * @return Returns the inputModified.
286: */
287: public boolean isInputModified() {
288: return inputModified;
289: }
290: }
|