src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/WsgenTool.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.ws.wscompile;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
ohair@286 29 import com.sun.tools.internal.ws.ToolVersion;
ohair@286 30 import com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceAp;
ohair@286 31 import com.sun.tools.internal.ws.processor.modeler.wsdl.ConsoleErrorReporter;
ohair@286 32 import com.sun.tools.internal.ws.resources.WscompileMessages;
ohair@286 33 import com.sun.tools.internal.xjc.util.NullStream;
ohair@286 34 import com.sun.xml.internal.txw2.TXW;
ohair@286 35 import com.sun.xml.internal.txw2.TypedXmlWriter;
ohair@286 36 import com.sun.xml.internal.txw2.annotation.XmlAttribute;
ohair@286 37 import com.sun.xml.internal.txw2.annotation.XmlElement;
ohair@286 38 import com.sun.xml.internal.txw2.output.StreamSerializer;
ohair@286 39 import com.sun.xml.internal.ws.api.BindingID;
ohair@286 40 import com.sun.xml.internal.ws.api.databinding.DatabindingConfig;
ohair@286 41 import com.sun.xml.internal.ws.api.databinding.DatabindingFactory;
ohair@286 42 import com.sun.xml.internal.ws.api.databinding.WSDLGenInfo;
ohair@286 43 import com.sun.xml.internal.ws.api.server.Container;
ohair@286 44 import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
ohair@286 45 import com.sun.xml.internal.ws.binding.WebServiceFeatureList;
ohair@286 46 import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
ohair@286 47 import com.sun.xml.internal.ws.util.ServiceFinder;
ohair@286 48 import com.sun.xml.internal.ws.wsdl.writer.WSDLResolver;
ohair@286 49 import org.xml.sax.SAXParseException;
ohair@286 50
ohair@286 51 import javax.tools.DiagnosticCollector;
ohair@286 52 import javax.tools.JavaCompiler;
ohair@286 53 import javax.tools.JavaFileObject;
ohair@286 54 import javax.tools.StandardJavaFileManager;
ohair@286 55 import javax.tools.ToolProvider;
ohair@286 56 import javax.xml.bind.annotation.XmlSeeAlso;
ohair@286 57 import javax.xml.namespace.QName;
ohair@286 58 import javax.xml.transform.Result;
ohair@286 59 import javax.xml.transform.stream.StreamResult;
ohair@286 60 import javax.xml.ws.EndpointReference;
ohair@286 61 import javax.xml.ws.Holder;
ohair@286 62 import java.io.BufferedOutputStream;
ohair@286 63 import java.io.File;
ohair@286 64 import java.io.FileNotFoundException;
ohair@286 65 import java.io.FileOutputStream;
ohair@286 66 import java.io.IOException;
ohair@286 67 import java.io.OutputStream;
ohair@286 68 import java.io.PrintStream;
ohair@286 69 import java.net.URLClassLoader;
ohair@286 70 import java.util.ArrayList;
ohair@286 71 import java.util.Collection;
ohair@286 72 import java.util.Collections;
ohair@286 73 import java.util.HashMap;
ohair@286 74 import java.util.Map;
ohair@286 75
ohair@286 76 /**
ohair@286 77 * @author Vivek Pandey
ohair@286 78 */
ohair@286 79
ohair@286 80 /*
ohair@286 81 * All annotation types are supported.
ohair@286 82 */
ohair@286 83 public class WsgenTool {
ohair@286 84 private final PrintStream out;
ohair@286 85 private final WsgenOptions options = new WsgenOptions();
ohair@286 86
ohair@286 87
ohair@286 88 public WsgenTool(OutputStream out, Container container) {
ohair@286 89 this.out = (out instanceof PrintStream)?(PrintStream)out:new PrintStream(out);
ohair@286 90 this.container = container;
ohair@286 91 }
ohair@286 92
ohair@286 93
ohair@286 94 public WsgenTool(OutputStream out) {
ohair@286 95 this(out, null);
ohair@286 96 }
ohair@286 97
ohair@286 98 public boolean run(String[] args){
ohair@286 99 final Listener listener = new Listener();
ohair@286 100 for (String arg : args) {
ohair@286 101 if (arg.equals("-version")) {
ohair@286 102 listener.message(
ohair@286 103 WscompileMessages.WSGEN_VERSION(ToolVersion.VERSION.MAJOR_VERSION));
ohair@286 104 return true;
ohair@286 105 }
ohair@286 106 if (arg.equals("-fullversion")) {
ohair@286 107 listener.message(
ohair@286 108 WscompileMessages.WSGEN_FULLVERSION(ToolVersion.VERSION.toString()));
ohair@286 109 return true;
ohair@286 110 }
ohair@286 111 }
ohair@286 112 try {
ohair@286 113 options.parseArguments(args);
ohair@286 114 options.validate();
ohair@286 115 if(!buildModel(options.endpoint.getName(), listener)){
ohair@286 116 return false;
ohair@286 117 }
ohair@286 118 }catch (Options.WeAreDone done){
ohair@286 119 usage((WsgenOptions)done.getOptions());
ohair@286 120 }catch (BadCommandLineException e) {
ohair@286 121 if(e.getMessage()!=null) {
ohair@286 122 System.out.println(e.getMessage());
ohair@286 123 System.out.println();
ohair@286 124 }
ohair@286 125 usage((WsgenOptions)e.getOptions());
ohair@286 126 return false;
ohair@286 127 }catch(AbortException e){
ohair@286 128 //error might have been reported
ohair@286 129 }finally{
ohair@286 130 if(!options.keep){
ohair@286 131 options.removeGeneratedFiles();
ohair@286 132 }
ohair@286 133 }
ohair@286 134 return true;
ohair@286 135 }
ohair@286 136
ohair@286 137 private final Container container;
ohair@286 138
ohair@286 139 private int round = 0;
ohair@286 140
ohair@286 141 /*
ohair@286 142 * To take care of JDK6-JDK6u3, where 2.1 API classes are not there
ohair@286 143 */
ohair@286 144 private static boolean useBootClasspath(Class clazz) {
ohair@286 145 try {
ohair@286 146 ParallelWorldClassLoader.toJarUrl(clazz.getResource('/'+clazz.getName().replace('.','/')+".class"));
ohair@286 147 return true;
ohair@286 148 } catch(Exception e) {
ohair@286 149 return false;
ohair@286 150 }
ohair@286 151 }
ohair@286 152
ohair@286 153
ohair@286 154 public boolean buildModel(String endpoint, Listener listener) throws BadCommandLineException {
ohair@286 155 final ErrorReceiverFilter errReceiver = new ErrorReceiverFilter(listener);
ohair@286 156
ohair@286 157 boolean bootCP = useBootClasspath(EndpointReference.class) || useBootClasspath(XmlSeeAlso.class);
ohair@286 158 Collection<String> args = new ArrayList<String>(6 + (bootCP ? 1 : 0) + (options.nocompile ? 1 : 0)
ohair@286 159 + (options.encoding != null ? 2 : 0));
ohair@286 160 args.add("-d");
ohair@286 161 args.add(options.destDir.getAbsolutePath());
ohair@286 162 args.add("-classpath");
ohair@286 163 args.add(options.classpath);
ohair@286 164 args.add("-s");
ohair@286 165 args.add(options.sourceDir.getAbsolutePath());
ohair@286 166 if (options.nocompile) {
ohair@286 167 args.add("-proc:only");
ohair@286 168 }
ohair@286 169 if (options.encoding != null) {
ohair@286 170 args.add("-encoding");
ohair@286 171 args.add(options.encoding);
ohair@286 172 }
ohair@286 173 if (bootCP) {
ohair@286 174 args.add(new StringBuilder()
ohair@286 175 .append("-Xbootclasspath/p:")
ohair@286 176 .append(JavaCompilerHelper.getJarFile(EndpointReference.class))
ohair@286 177 .append(File.pathSeparator)
ohair@286 178 .append(JavaCompilerHelper.getJarFile(XmlSeeAlso.class)).toString());
ohair@286 179 }
ohair@286 180
ohair@286 181 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
ohair@286 182 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
ohair@286 183 StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
ohair@286 184 JavaCompiler.CompilationTask task = compiler.getTask(
ohair@286 185 null,
ohair@286 186 fileManager,
ohair@286 187 diagnostics,
ohair@286 188 args,
ohair@286 189 Collections.singleton(endpoint.replaceAll("\\$", ".")),
ohair@286 190 null);
ohair@286 191 task.setProcessors(Collections.singleton(new WebServiceAp(options, out)));
ohair@286 192 boolean result = task.call();
ohair@286 193
ohair@286 194 if (!result) {
ohair@286 195 out.println(WscompileMessages.WSCOMPILE_ERROR(WscompileMessages.WSCOMPILE_COMPILATION_FAILED()));
ohair@286 196 return false;
ohair@286 197 }
ohair@286 198 if (options.genWsdl) {
ohair@286 199 DatabindingConfig config = new DatabindingConfig();
ohair@286 200 String tmpPath = options.destDir.getAbsolutePath()+ File.pathSeparator+options.classpath;
ohair@286 201 ClassLoader classLoader = new URLClassLoader(Options.pathToURLs(tmpPath),
ohair@286 202 this.getClass().getClassLoader());
ohair@286 203 Class<?> endpointClass;
ohair@286 204 try {
ohair@286 205 endpointClass = classLoader.loadClass(endpoint);
ohair@286 206 } catch (ClassNotFoundException e) {
ohair@286 207 throw new BadCommandLineException(WscompileMessages.WSGEN_CLASS_NOT_FOUND(endpoint));
ohair@286 208 }
ohair@286 209
ohair@286 210 BindingID bindingID = options.getBindingID(options.protocol);
ohair@286 211 if (!options.protocolSet) {
ohair@286 212 bindingID = BindingID.parse(endpointClass);
ohair@286 213 }
ohair@286 214 WebServiceFeatureList wsfeatures = new WebServiceFeatureList(endpointClass);
ohair@286 215 // RuntimeModeler rtModeler = new RuntimeModeler(endpointClass, options.serviceName, bindingID, wsfeatures.toArray());
ohair@286 216 // rtModeler.setClassLoader(classLoader);
ohair@286 217 if (options.portName != null)
ohair@286 218 config.getMappingInfo().setPortName(options.portName);//rtModeler.setPortName(options.portName);
ohair@286 219 // AbstractSEIModelImpl rtModel = rtModeler.buildRuntimeModel();
ohair@286 220
ohair@286 221 DatabindingFactory fac = DatabindingFactory.newInstance();
ohair@286 222 config.setEndpointClass(endpointClass);
ohair@286 223 config.getMappingInfo().setServiceName(options.serviceName);
ohair@286 224 config.setFeatures(wsfeatures.toArray());
ohair@286 225 config.setClassLoader(classLoader);
ohair@286 226 config.getMappingInfo().setBindingID(bindingID);
ohair@286 227 com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl)fac.createRuntime(config);
ohair@286 228
ohair@286 229 final File[] wsdlFileName = new File[1]; // used to capture the generated WSDL file.
ohair@286 230 final Map<String,File> schemaFiles = new HashMap<String,File>();
ohair@286 231
ohair@286 232 WSDLGenInfo wsdlGenInfo = new WSDLGenInfo();
ohair@286 233 wsdlGenInfo.setWsdlResolver(
ohair@286 234 new WSDLResolver() {
ohair@286 235 private File toFile(String suggestedFilename) {
ohair@286 236 return new File(options.nonclassDestDir, suggestedFilename);
ohair@286 237 }
ohair@286 238 private Result toResult(File file) {
ohair@286 239 Result result;
ohair@286 240 try {
ohair@286 241 result = new StreamResult(new FileOutputStream(file));
ohair@286 242 result.setSystemId(file.getPath().replace('\\', '/'));
ohair@286 243 } catch (FileNotFoundException e) {
ohair@286 244 errReceiver.error(e);
ohair@286 245 return null;
ohair@286 246 }
ohair@286 247 return result;
ohair@286 248 }
ohair@286 249
ohair@286 250 public Result getWSDL(String suggestedFilename) {
ohair@286 251 File f = toFile(suggestedFilename);
ohair@286 252 wsdlFileName[0] = f;
ohair@286 253 return toResult(f);
ohair@286 254 }
ohair@286 255 public Result getSchemaOutput(String namespace, String suggestedFilename) {
ohair@286 256 if (namespace == null)
ohair@286 257 return null;
ohair@286 258 File f = toFile(suggestedFilename);
ohair@286 259 schemaFiles.put(namespace,f);
ohair@286 260 return toResult(f);
ohair@286 261 }
ohair@286 262 public Result getAbstractWSDL(Holder<String> filename) {
ohair@286 263 return toResult(toFile(filename.value));
ohair@286 264 }
ohair@286 265 public Result getSchemaOutput(String namespace, Holder<String> filename) {
ohair@286 266 return getSchemaOutput(namespace, filename.value);
ohair@286 267 }
ohair@286 268 // TODO pass correct impl's class name
ohair@286 269 });
ohair@286 270
ohair@286 271 wsdlGenInfo.setContainer(container);
ohair@286 272 wsdlGenInfo.setExtensions(ServiceFinder.find(WSDLGeneratorExtension.class).toArray());
ohair@286 273 wsdlGenInfo.setInlineSchemas(options.inlineSchemas);
ohair@286 274 rt.generateWSDL(wsdlGenInfo);
ohair@286 275
ohair@286 276
ohair@286 277 if(options.wsgenReport!=null)
ohair@286 278 generateWsgenReport(endpointClass,(AbstractSEIModelImpl)rt.getModel(),wsdlFileName[0],schemaFiles);
ohair@286 279 }
ohair@286 280 return true;
ohair@286 281 }
ohair@286 282
ohair@286 283 /**
ohair@286 284 * Generates a small XML file that captures the key activity of wsgen,
ohair@286 285 * so that test harness can pick up artifacts.
ohair@286 286 */
ohair@286 287 private void generateWsgenReport(Class<?> endpointClass, AbstractSEIModelImpl rtModel, File wsdlFile, Map<String,File> schemaFiles) {
ohair@286 288 try {
ohair@286 289 ReportOutput.Report report = TXW.create(ReportOutput.Report.class,
ohair@286 290 new StreamSerializer(new BufferedOutputStream(new FileOutputStream(options.wsgenReport))));
ohair@286 291
ohair@286 292 report.wsdl(wsdlFile.getAbsolutePath());
ohair@286 293 ReportOutput.writeQName(rtModel.getServiceQName(), report.service());
ohair@286 294 ReportOutput.writeQName(rtModel.getPortName(), report.port());
ohair@286 295 ReportOutput.writeQName(rtModel.getPortTypeName(), report.portType());
ohair@286 296
ohair@286 297 report.implClass(endpointClass.getName());
ohair@286 298
ohair@286 299 for (Map.Entry<String,File> e : schemaFiles.entrySet()) {
ohair@286 300 ReportOutput.Schema s = report.schema();
ohair@286 301 s.ns(e.getKey());
ohair@286 302 s.location(e.getValue().getAbsolutePath());
ohair@286 303 }
ohair@286 304
ohair@286 305 report.commit();
ohair@286 306 } catch (IOException e) {
ohair@286 307 // this is code for the test, so we can be lousy in the error handling
ohair@286 308 throw new Error(e);
ohair@286 309 }
ohair@286 310 }
ohair@286 311
ohair@286 312 /**
ohair@286 313 * "Namespace" for code needed to generate the report file.
ohair@286 314 */
ohair@286 315 static class ReportOutput {
ohair@286 316 @XmlElement("report")
ohair@286 317 interface Report extends TypedXmlWriter {
ohair@286 318 @XmlElement
ohair@286 319 void wsdl(String file); // location of WSDL
ohair@286 320 @XmlElement
ohair@286 321 QualifiedName portType();
ohair@286 322 @XmlElement
ohair@286 323 QualifiedName service();
ohair@286 324 @XmlElement
ohair@286 325 QualifiedName port();
ohair@286 326
ohair@286 327 /**
ohair@286 328 * Name of the class that has {@link javax.jws.WebService}.
ohair@286 329 */
ohair@286 330 @XmlElement
ohair@286 331 void implClass(String name);
ohair@286 332
ohair@286 333 @XmlElement
ohair@286 334 Schema schema();
ohair@286 335 }
ohair@286 336
ohair@286 337 interface QualifiedName extends TypedXmlWriter {
ohair@286 338 @XmlAttribute
ohair@286 339 void uri(String ns);
ohair@286 340 @XmlAttribute
ohair@286 341 void localName(String localName);
ohair@286 342 }
ohair@286 343
ohair@286 344 interface Schema extends TypedXmlWriter {
ohair@286 345 @XmlAttribute
ohair@286 346 void ns(String ns);
ohair@286 347 @XmlAttribute
ohair@286 348 void location(String filePath);
ohair@286 349 }
ohair@286 350
ohair@286 351 private static void writeQName( QName n, QualifiedName w ) {
ohair@286 352 w.uri(n.getNamespaceURI());
ohair@286 353 w.localName(n.getLocalPart());
ohair@286 354 }
ohair@286 355 }
ohair@286 356
ohair@286 357 protected void usage(WsgenOptions options) {
ohair@286 358 // Just don't see any point in passing WsgenOptions
ohair@286 359 // BadCommandLineException also shouldn't have options
ohair@286 360 if (options == null)
ohair@286 361 options = this.options;
ohair@286 362 System.out.println(WscompileMessages.WSGEN_HELP("WSGEN", options.protocols, options.nonstdProtocols.keySet()));
ohair@286 363 System.out.println(WscompileMessages.WSGEN_USAGE_EXAMPLES());
ohair@286 364 }
ohair@286 365
ohair@286 366 class Listener extends WsimportListener {
ohair@286 367 ConsoleErrorReporter cer = new ConsoleErrorReporter(out == null ? new PrintStream(new NullStream()) : out);
ohair@286 368
ohair@286 369 @Override
ohair@286 370 public void generatedFile(String fileName) {
ohair@286 371 message(fileName);
ohair@286 372 }
ohair@286 373
ohair@286 374 @Override
ohair@286 375 public void message(String msg) {
ohair@286 376 out.println(msg);
ohair@286 377 }
ohair@286 378
ohair@286 379 @Override
ohair@286 380 public void error(SAXParseException exception) {
ohair@286 381 cer.error(exception);
ohair@286 382 }
ohair@286 383
ohair@286 384 @Override
ohair@286 385 public void fatalError(SAXParseException exception) {
ohair@286 386 cer.fatalError(exception);
ohair@286 387 }
ohair@286 388
ohair@286 389 @Override
ohair@286 390 public void warning(SAXParseException exception) {
ohair@286 391 cer.warning(exception);
ohair@286 392 }
ohair@286 393
ohair@286 394 @Override
ohair@286 395 public void info(SAXParseException exception) {
ohair@286 396 cer.info(exception);
ohair@286 397 }
ohair@286 398 }
ohair@286 399 }

mercurial