001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wscompile;
038:
039: import com.sun.codemodel.CodeWriter;
040: import com.sun.codemodel.writer.ProgressCodeWriter;
041: import com.sun.tools.ws.ToolVersion;
042: import com.sun.tools.ws.api.TJavaGeneratorExtension;
043: import com.sun.tools.ws.processor.generator.CustomExceptionGenerator;
044: import com.sun.tools.ws.processor.generator.SeiGenerator;
045: import com.sun.tools.ws.processor.generator.ServiceGenerator;
046: import com.sun.tools.ws.processor.model.Model;
047: import com.sun.tools.ws.processor.modeler.wsdl.ConsoleErrorReporter;
048: import com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler;
049: import com.sun.tools.ws.resources.WscompileMessages;
050: import com.sun.tools.ws.resources.WsdlMessages;
051: import com.sun.tools.xjc.util.NullStream;
052: import com.sun.xml.ws.api.server.Container;
053: import com.sun.xml.ws.util.ServiceFinder;
054: import org.xml.sax.EntityResolver;
055: import org.xml.sax.SAXParseException;
056:
057: import javax.xml.bind.annotation.XmlSeeAlso;
058: import javax.xml.ws.EndpointReference;
059: import java.io.File;
060: import java.io.IOException;
061: import java.io.OutputStream;
062: import java.io.PrintStream;
063: import java.util.ArrayList;
064: import java.util.List;
065:
066: /**
067: * @author Vivek Pandey
068: */
069: public class WsimportTool {
070: private static final String WSIMPORT = "wsimport";
071: private final PrintStream out;
072: private final Container container;
073:
074: /**
075: * Wsimport specific options
076: */
077: private final WsimportOptions options = new WsimportOptions();
078:
079: public WsimportTool(OutputStream out) {
080: this (out, null);
081: }
082:
083: public WsimportTool(OutputStream logStream, Container container) {
084: this .out = (logStream instanceof PrintStream) ? (PrintStream) logStream
085: : new PrintStream(logStream);
086: this .container = container;
087: }
088:
089: public boolean run(String[] args) {
090: class Listener extends WsimportListener {
091: ConsoleErrorReporter cer = new ConsoleErrorReporter(
092: out == null ? new PrintStream(new NullStream())
093: : out);
094:
095: @Override
096: public void generatedFile(String fileName) {
097: message(fileName);
098: }
099:
100: @Override
101: public void message(String msg) {
102: out.println(msg);
103: }
104:
105: @Override
106: public void error(SAXParseException exception) {
107: cer.error(exception);
108: }
109:
110: @Override
111: public void fatalError(SAXParseException exception) {
112: cer.fatalError(exception);
113: }
114:
115: @Override
116: public void warning(SAXParseException exception) {
117: cer.warning(exception);
118: }
119:
120: @Override
121: public void info(SAXParseException exception) {
122: cer.info(exception);
123: }
124:
125: public void enableDebugging() {
126: cer.enableDebugging();
127: }
128: }
129: final Listener listener = new Listener();
130: ErrorReceiverFilter receiver = new ErrorReceiverFilter(listener) {
131: public void info(SAXParseException exception) {
132: if (options.verbose)
133: super .info(exception);
134: }
135:
136: public void warning(SAXParseException exception) {
137: if (!options.quiet)
138: super .warning(exception);
139: }
140:
141: @Override
142: public void pollAbort() throws AbortException {
143: if (listener.isCanceled())
144: throw new AbortException();
145: }
146: };
147:
148: for (String arg : args) {
149: if (arg.equals("-version")) {
150: listener.message(ToolVersion.VERSION.BUILD_VERSION);
151: return true;
152: }
153: }
154: try {
155: options.parseArguments(args);
156: options.validate();
157: if (options.debugMode)
158: listener.enableDebugging();
159: options.parseBindings(receiver);
160:
161: try {
162: if (!options.quiet)
163: listener.message(WscompileMessages
164: .WSIMPORT_PARSING_WSDL());
165:
166: WSDLModeler wsdlModeler = new WSDLModeler(options,
167: receiver);
168: Model wsdlModel = wsdlModeler.buildModel();
169: if (wsdlModel == null) {
170: listener.message(WsdlMessages
171: .PARSING_PARSE_FAILED());
172: return false;
173: }
174:
175: //generated code
176: if (!options.quiet)
177: listener.message(WscompileMessages
178: .WSIMPORT_GENERATING_CODE());
179:
180: TJavaGeneratorExtension[] genExtn = ServiceFinder.find(
181: TJavaGeneratorExtension.class).toArray();
182: CustomExceptionGenerator.generate(wsdlModel, options,
183: receiver);
184: SeiGenerator.generate(wsdlModel, options, receiver,
185: genExtn);
186: if (receiver.hadError()) {
187: throw new AbortException();
188: }
189: ServiceGenerator.generate(wsdlModel, options, receiver);
190: CodeWriter cw = new WSCodeWriter(options.sourceDir,
191: options);
192: if (options.verbose)
193: cw = new ProgressCodeWriter(cw, out);
194: options.getCodeModel().build(cw);
195: } catch (AbortException e) {
196: //error might have been reported
197: } catch (IOException e) {
198: receiver.error(e);
199: }
200:
201: if (!options.nocompile) {
202: if (!compileGeneratedClasses(receiver, listener)) {
203: listener.message(WscompileMessages
204: .WSCOMPILE_COMPILATION_FAILED());
205: return false;
206: }
207: }
208:
209: } catch (Options.WeAreDone done) {
210: usage(done.getOptions());
211: } catch (BadCommandLineException e) {
212: if (e.getMessage() != null) {
213: System.out.println(e.getMessage());
214: System.out.println();
215: }
216: usage(e.getOptions());
217: return false;
218: } finally {
219: if (!options.keep) {
220: options.removeGeneratedFiles();
221: }
222: }
223: return true;
224: }
225:
226: public void setEntityResolver(EntityResolver resolver) {
227: this .options.entityResolver = resolver;
228: }
229:
230: protected boolean compileGeneratedClasses(ErrorReceiver receiver,
231: WsimportListener listener) {
232: List<String> sourceFiles = new ArrayList<String>();
233:
234: for (File f : options.getGeneratedFiles()) {
235: if (f.exists() && f.getName().endsWith(".java")) {
236: sourceFiles.add(f.getAbsolutePath());
237: }
238: }
239:
240: if (sourceFiles.size() > 0) {
241: String classDir = options.destDir.getAbsolutePath();
242: String classpathString = createClasspathString();
243: String[] args = new String[5 + (options.debug ? 1 : 0)
244: + sourceFiles.size()];
245: args[0] = "-d";
246: args[1] = classDir;
247: args[2] = "-classpath";
248: args[3] = classpathString;
249: args[4] = "-Xbootclasspath/p:"
250: + JavaCompilerHelper
251: .getJarFile(EndpointReference.class)
252: + File.pathSeparator
253: + JavaCompilerHelper.getJarFile(XmlSeeAlso.class);
254: int baseIndex = 5;
255: if (options.debug) {
256: args[baseIndex++] = "-g";
257: }
258: for (int i = 0; i < sourceFiles.size(); ++i) {
259: args[baseIndex + i] = sourceFiles.get(i);
260: }
261:
262: listener.message(WscompileMessages
263: .WSIMPORT_COMPILING_CODE());
264: if (options.verbose) {
265: StringBuffer argstr = new StringBuffer();
266: for (String arg : args) {
267: argstr.append(arg).append(" ");
268: }
269: listener.message("javac " + argstr.toString());
270: }
271:
272: return JavaCompilerHelper.compile(args, out, receiver);
273: }
274: //there are no files to compile, so return true?
275: return true;
276: }
277:
278: private String createClasspathString() {
279: return System.getProperty("java.class.path");
280: }
281:
282: protected void usage(Options options) {
283: System.out.println(WscompileMessages.WSIMPORT_HELP(WSIMPORT));
284: System.out.println(WscompileMessages.WSIMPORT_USAGE_EXAMPLES());
285: }
286: }
|