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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 408
b0610cd08440
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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
alanb@368 28 import com.oracle.webservices.internal.api.databinding.WSDLResolver;
ohair@286 29 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
ohair@286 30 import com.sun.tools.internal.ws.ToolVersion;
ohair@286 31 import com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceAp;
ohair@286 32 import com.sun.tools.internal.ws.processor.modeler.wsdl.ConsoleErrorReporter;
ohair@286 33 import com.sun.tools.internal.ws.resources.WscompileMessages;
ohair@286 34 import com.sun.tools.internal.xjc.util.NullStream;
ohair@286 35 import com.sun.xml.internal.txw2.TXW;
ohair@286 36 import com.sun.xml.internal.txw2.TypedXmlWriter;
ohair@286 37 import com.sun.xml.internal.txw2.annotation.XmlAttribute;
ohair@286 38 import com.sun.xml.internal.txw2.annotation.XmlElement;
ohair@286 39 import com.sun.xml.internal.txw2.output.StreamSerializer;
ohair@286 40 import com.sun.xml.internal.ws.api.BindingID;
ohair@286 41 import com.sun.xml.internal.ws.api.databinding.DatabindingConfig;
ohair@286 42 import com.sun.xml.internal.ws.api.databinding.DatabindingFactory;
ohair@286 43 import com.sun.xml.internal.ws.api.databinding.WSDLGenInfo;
ohair@286 44 import com.sun.xml.internal.ws.api.server.Container;
ohair@286 45 import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
ohair@286 46 import com.sun.xml.internal.ws.binding.WebServiceFeatureList;
alanb@368 47 import com.sun.xml.internal.ws.model.ExternalMetadataReader;
ohair@286 48 import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
ohair@286 49 import com.sun.xml.internal.ws.util.ServiceFinder;
ohair@286 50 import org.xml.sax.SAXParseException;
ohair@286 51
ohair@286 52 import javax.tools.DiagnosticCollector;
ohair@286 53 import javax.tools.JavaCompiler;
ohair@286 54 import javax.tools.JavaFileObject;
ohair@286 55 import javax.tools.StandardJavaFileManager;
ohair@286 56 import javax.tools.ToolProvider;
ohair@286 57 import javax.xml.bind.annotation.XmlSeeAlso;
ohair@286 58 import javax.xml.namespace.QName;
ohair@286 59 import javax.xml.transform.Result;
ohair@286 60 import javax.xml.transform.stream.StreamResult;
ohair@286 61 import javax.xml.ws.EndpointReference;
ohair@286 62 import javax.xml.ws.Holder;
ohair@286 63 import java.io.BufferedOutputStream;
ohair@286 64 import java.io.File;
ohair@286 65 import java.io.FileNotFoundException;
ohair@286 66 import java.io.FileOutputStream;
ohair@286 67 import java.io.IOException;
ohair@286 68 import java.io.OutputStream;
ohair@286 69 import java.io.PrintStream;
ohair@286 70 import java.net.URLClassLoader;
ohair@286 71 import java.util.ArrayList;
ohair@286 72 import java.util.Collection;
ohair@286 73 import java.util.Collections;
ohair@286 74 import java.util.HashMap;
alanb@368 75 import java.util.List;
ohair@286 76 import java.util.Map;
ohair@286 77
ohair@286 78 /**
ohair@286 79 * @author Vivek Pandey
ohair@286 80 */
ohair@286 81
ohair@286 82 /*
ohair@286 83 * All annotation types are supported.
ohair@286 84 */
ohair@286 85 public class WsgenTool {
ohair@286 86 private final PrintStream out;
ohair@286 87 private final WsgenOptions options = new WsgenOptions();
ohair@286 88
ohair@286 89
ohair@286 90 public WsgenTool(OutputStream out, Container container) {
alanb@368 91 this.out = (out instanceof PrintStream) ? (PrintStream) out : new PrintStream(out);
ohair@286 92 this.container = container;
ohair@286 93 }
ohair@286 94
ohair@286 95
ohair@286 96 public WsgenTool(OutputStream out) {
ohair@286 97 this(out, null);
ohair@286 98 }
ohair@286 99
alanb@368 100 public boolean run(String[] args) {
ohair@286 101 final Listener listener = new Listener();
ohair@286 102 for (String arg : args) {
ohair@286 103 if (arg.equals("-version")) {
ohair@286 104 listener.message(
ohair@286 105 WscompileMessages.WSGEN_VERSION(ToolVersion.VERSION.MAJOR_VERSION));
ohair@286 106 return true;
ohair@286 107 }
ohair@286 108 if (arg.equals("-fullversion")) {
ohair@286 109 listener.message(
ohair@286 110 WscompileMessages.WSGEN_FULLVERSION(ToolVersion.VERSION.toString()));
ohair@286 111 return true;
ohair@286 112 }
ohair@286 113 }
ohair@286 114 try {
ohair@286 115 options.parseArguments(args);
ohair@286 116 options.validate();
alanb@368 117 if (!buildModel(options.endpoint.getName(), listener)) {
ohair@286 118 return false;
ohair@286 119 }
alanb@368 120 } catch (Options.WeAreDone done) {
alanb@368 121 usage(done.getOptions());
alanb@368 122 } catch (BadCommandLineException e) {
alanb@368 123 if (e.getMessage() != null) {
ohair@286 124 System.out.println(e.getMessage());
ohair@286 125 System.out.println();
ohair@286 126 }
alanb@368 127 usage(e.getOptions());
ohair@286 128 return false;
alanb@368 129 } catch (AbortException e) {
ohair@286 130 //error might have been reported
alanb@368 131 } finally {
alanb@368 132 if (!options.keep) {
ohair@286 133 options.removeGeneratedFiles();
ohair@286 134 }
ohair@286 135 }
ohair@286 136 return true;
ohair@286 137 }
ohair@286 138
ohair@286 139 private final Container container;
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 {
alanb@368 146 ParallelWorldClassLoader.toJarUrl(clazz.getResource('/' + clazz.getName().replace('.', '/') + ".class"));
ohair@286 147 return true;
alanb@368 148 } catch (Exception e) {
ohair@286 149 return false;
ohair@286 150 }
ohair@286 151 }
ohair@286 152
alanb@368 153 /**
alanb@368 154 *
alanb@368 155 * @param endpoint
alanb@368 156 * @param listener
alanb@368 157 * @return
alanb@368 158 * @throws BadCommandLineException
alanb@368 159 */
ohair@286 160 public boolean buildModel(String endpoint, Listener listener) throws BadCommandLineException {
ohair@286 161 final ErrorReceiverFilter errReceiver = new ErrorReceiverFilter(listener);
ohair@286 162
ohair@286 163 boolean bootCP = useBootClasspath(EndpointReference.class) || useBootClasspath(XmlSeeAlso.class);
ohair@286 164 Collection<String> args = new ArrayList<String>(6 + (bootCP ? 1 : 0) + (options.nocompile ? 1 : 0)
ohair@286 165 + (options.encoding != null ? 2 : 0));
ohair@286 166 args.add("-d");
ohair@286 167 args.add(options.destDir.getAbsolutePath());
ohair@286 168 args.add("-classpath");
ohair@286 169 args.add(options.classpath);
ohair@286 170 args.add("-s");
ohair@286 171 args.add(options.sourceDir.getAbsolutePath());
ohair@286 172 if (options.nocompile) {
ohair@286 173 args.add("-proc:only");
ohair@286 174 }
ohair@286 175 if (options.encoding != null) {
ohair@286 176 args.add("-encoding");
ohair@286 177 args.add(options.encoding);
ohair@286 178 }
ohair@286 179 if (bootCP) {
ohair@286 180 args.add(new StringBuilder()
ohair@286 181 .append("-Xbootclasspath/p:")
ohair@286 182 .append(JavaCompilerHelper.getJarFile(EndpointReference.class))
ohair@286 183 .append(File.pathSeparator)
ohair@286 184 .append(JavaCompilerHelper.getJarFile(XmlSeeAlso.class)).toString());
ohair@286 185 }
ohair@286 186
alanb@368 187 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();// compiler = JavacTool.create();
ohair@286 188 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
ohair@286 189 StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
ohair@286 190 JavaCompiler.CompilationTask task = compiler.getTask(
ohair@286 191 null,
ohair@286 192 fileManager,
ohair@286 193 diagnostics,
ohair@286 194 args,
ohair@286 195 Collections.singleton(endpoint.replaceAll("\\$", ".")),
ohair@286 196 null);
ohair@286 197 task.setProcessors(Collections.singleton(new WebServiceAp(options, out)));
ohair@286 198 boolean result = task.call();
ohair@286 199
ohair@286 200 if (!result) {
ohair@286 201 out.println(WscompileMessages.WSCOMPILE_ERROR(WscompileMessages.WSCOMPILE_COMPILATION_FAILED()));
ohair@286 202 return false;
ohair@286 203 }
ohair@286 204 if (options.genWsdl) {
alanb@368 205 DatabindingConfig config = new DatabindingConfig();
alanb@368 206
alanb@368 207 List<String> externalMetadataFileNames = options.externalMetadataFiles;
alanb@368 208 boolean disableSecureXmlProcessing = options.disableSecureXmlProcessing;
alanb@368 209 if (externalMetadataFileNames != null && externalMetadataFileNames.size() > 0) {
alanb@368 210 config.setMetadataReader(new ExternalMetadataReader(getExternalFiles(externalMetadataFileNames), null, null, true, disableSecureXmlProcessing));
alanb@368 211 }
alanb@368 212
alanb@368 213 String tmpPath = options.destDir.getAbsolutePath() + File.pathSeparator + options.classpath;
ohair@286 214 ClassLoader classLoader = new URLClassLoader(Options.pathToURLs(tmpPath),
ohair@286 215 this.getClass().getClassLoader());
ohair@286 216 Class<?> endpointClass;
ohair@286 217 try {
ohair@286 218 endpointClass = classLoader.loadClass(endpoint);
ohair@286 219 } catch (ClassNotFoundException e) {
ohair@286 220 throw new BadCommandLineException(WscompileMessages.WSGEN_CLASS_NOT_FOUND(endpoint));
ohair@286 221 }
ohair@286 222
ohair@286 223 BindingID bindingID = options.getBindingID(options.protocol);
ohair@286 224 if (!options.protocolSet) {
ohair@286 225 bindingID = BindingID.parse(endpointClass);
ohair@286 226 }
ohair@286 227 WebServiceFeatureList wsfeatures = new WebServiceFeatureList(endpointClass);
ohair@286 228 // RuntimeModeler rtModeler = new RuntimeModeler(endpointClass, options.serviceName, bindingID, wsfeatures.toArray());
ohair@286 229 // rtModeler.setClassLoader(classLoader);
ohair@286 230 if (options.portName != null)
ohair@286 231 config.getMappingInfo().setPortName(options.portName);//rtModeler.setPortName(options.portName);
ohair@286 232 // AbstractSEIModelImpl rtModel = rtModeler.buildRuntimeModel();
ohair@286 233
alanb@368 234 DatabindingFactory fac = DatabindingFactory.newInstance();
alanb@368 235 config.setEndpointClass(endpointClass);
alanb@368 236 config.getMappingInfo().setServiceName(options.serviceName);
alanb@368 237 config.setFeatures(wsfeatures.toArray());
alanb@368 238 config.setClassLoader(classLoader);
alanb@368 239 config.getMappingInfo().setBindingID(bindingID);
alanb@368 240 com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl) fac.createRuntime(config);
ohair@286 241
ohair@286 242 final File[] wsdlFileName = new File[1]; // used to capture the generated WSDL file.
alanb@368 243 final Map<String, File> schemaFiles = new HashMap<String, File>();
ohair@286 244
ohair@286 245 WSDLGenInfo wsdlGenInfo = new WSDLGenInfo();
alanb@368 246 wsdlGenInfo.setSecureXmlProcessingDisabled(disableSecureXmlProcessing);
alanb@368 247
ohair@286 248 wsdlGenInfo.setWsdlResolver(
ohair@286 249 new WSDLResolver() {
ohair@286 250 private File toFile(String suggestedFilename) {
ohair@286 251 return new File(options.nonclassDestDir, suggestedFilename);
ohair@286 252 }
alanb@368 253
ohair@286 254 private Result toResult(File file) {
ohair@286 255 Result result;
ohair@286 256 try {
ohair@286 257 result = new StreamResult(new FileOutputStream(file));
ohair@286 258 result.setSystemId(file.getPath().replace('\\', '/'));
ohair@286 259 } catch (FileNotFoundException e) {
ohair@286 260 errReceiver.error(e);
ohair@286 261 return null;
ohair@286 262 }
ohair@286 263 return result;
ohair@286 264 }
ohair@286 265
alanb@368 266 @Override
ohair@286 267 public Result getWSDL(String suggestedFilename) {
ohair@286 268 File f = toFile(suggestedFilename);
ohair@286 269 wsdlFileName[0] = f;
ohair@286 270 return toResult(f);
ohair@286 271 }
alanb@368 272
ohair@286 273 public Result getSchemaOutput(String namespace, String suggestedFilename) {
ohair@286 274 if (namespace == null)
ohair@286 275 return null;
ohair@286 276 File f = toFile(suggestedFilename);
alanb@368 277 schemaFiles.put(namespace, f);
ohair@286 278 return toResult(f);
ohair@286 279 }
alanb@368 280
alanb@368 281 @Override
ohair@286 282 public Result getAbstractWSDL(Holder<String> filename) {
ohair@286 283 return toResult(toFile(filename.value));
ohair@286 284 }
alanb@368 285
alanb@368 286 @Override
ohair@286 287 public Result getSchemaOutput(String namespace, Holder<String> filename) {
ohair@286 288 return getSchemaOutput(namespace, filename.value);
ohair@286 289 }
ohair@286 290 // TODO pass correct impl's class name
ohair@286 291 });
ohair@286 292
ohair@286 293 wsdlGenInfo.setContainer(container);
ohair@286 294 wsdlGenInfo.setExtensions(ServiceFinder.find(WSDLGeneratorExtension.class).toArray());
ohair@286 295 wsdlGenInfo.setInlineSchemas(options.inlineSchemas);
ohair@286 296 rt.generateWSDL(wsdlGenInfo);
ohair@286 297
ohair@286 298
alanb@368 299 if (options.wsgenReport != null)
alanb@368 300 generateWsgenReport(endpointClass, (AbstractSEIModelImpl) rt.getModel(), wsdlFileName[0], schemaFiles);
ohair@286 301 }
ohair@286 302 return true;
ohair@286 303 }
ohair@286 304
alanb@368 305 private List<File> getExternalFiles(List<String> exts) {
alanb@368 306 List<File> files = new ArrayList<File>();
alanb@368 307 for (String ext : exts) {
alanb@368 308 // first try absolute path ...
alanb@368 309 File file = new File(ext);
alanb@368 310 if (!file.exists()) {
alanb@368 311 // then relative path ...
alanb@368 312 file = new File(options.sourceDir.getAbsolutePath() + File.separator + ext);
alanb@368 313 }
alanb@368 314 files.add(file);
alanb@368 315 }
alanb@368 316 return files;
alanb@368 317 }
alanb@368 318
ohair@286 319 /**
ohair@286 320 * Generates a small XML file that captures the key activity of wsgen,
ohair@286 321 * so that test harness can pick up artifacts.
ohair@286 322 */
alanb@368 323 private void generateWsgenReport(Class<?> endpointClass, AbstractSEIModelImpl rtModel, File wsdlFile, Map<String, File> schemaFiles) {
ohair@286 324 try {
ohair@286 325 ReportOutput.Report report = TXW.create(ReportOutput.Report.class,
alanb@368 326 new StreamSerializer(new BufferedOutputStream(new FileOutputStream(options.wsgenReport))));
ohair@286 327
ohair@286 328 report.wsdl(wsdlFile.getAbsolutePath());
ohair@286 329 ReportOutput.writeQName(rtModel.getServiceQName(), report.service());
ohair@286 330 ReportOutput.writeQName(rtModel.getPortName(), report.port());
ohair@286 331 ReportOutput.writeQName(rtModel.getPortTypeName(), report.portType());
ohair@286 332
ohair@286 333 report.implClass(endpointClass.getName());
ohair@286 334
alanb@368 335 for (Map.Entry<String, File> e : schemaFiles.entrySet()) {
ohair@286 336 ReportOutput.Schema s = report.schema();
ohair@286 337 s.ns(e.getKey());
ohair@286 338 s.location(e.getValue().getAbsolutePath());
ohair@286 339 }
ohair@286 340
ohair@286 341 report.commit();
ohair@286 342 } catch (IOException e) {
ohair@286 343 // this is code for the test, so we can be lousy in the error handling
ohair@286 344 throw new Error(e);
ohair@286 345 }
ohair@286 346 }
ohair@286 347
ohair@286 348 /**
ohair@286 349 * "Namespace" for code needed to generate the report file.
ohair@286 350 */
ohair@286 351 static class ReportOutput {
ohair@286 352 @XmlElement("report")
ohair@286 353 interface Report extends TypedXmlWriter {
ohair@286 354 @XmlElement
ohair@286 355 void wsdl(String file); // location of WSDL
alanb@368 356
ohair@286 357 @XmlElement
ohair@286 358 QualifiedName portType();
alanb@368 359
ohair@286 360 @XmlElement
ohair@286 361 QualifiedName service();
alanb@368 362
ohair@286 363 @XmlElement
ohair@286 364 QualifiedName port();
ohair@286 365
ohair@286 366 /**
ohair@286 367 * Name of the class that has {@link javax.jws.WebService}.
ohair@286 368 */
ohair@286 369 @XmlElement
ohair@286 370 void implClass(String name);
ohair@286 371
ohair@286 372 @XmlElement
ohair@286 373 Schema schema();
ohair@286 374 }
ohair@286 375
ohair@286 376 interface QualifiedName extends TypedXmlWriter {
ohair@286 377 @XmlAttribute
ohair@286 378 void uri(String ns);
alanb@368 379
ohair@286 380 @XmlAttribute
ohair@286 381 void localName(String localName);
ohair@286 382 }
ohair@286 383
ohair@286 384 interface Schema extends TypedXmlWriter {
ohair@286 385 @XmlAttribute
ohair@286 386 void ns(String ns);
alanb@368 387
ohair@286 388 @XmlAttribute
ohair@286 389 void location(String filePath);
ohair@286 390 }
ohair@286 391
alanb@368 392 private static void writeQName(QName n, QualifiedName w) {
ohair@286 393 w.uri(n.getNamespaceURI());
ohair@286 394 w.localName(n.getLocalPart());
ohair@286 395 }
ohair@286 396 }
ohair@286 397
alanb@368 398 protected void usage(Options options) {
ohair@286 399 // Just don't see any point in passing WsgenOptions
ohair@286 400 // BadCommandLineException also shouldn't have options
ohair@286 401 if (options == null)
ohair@286 402 options = this.options;
alanb@368 403 if (options instanceof WsgenOptions) {
alanb@368 404 System.out.println(WscompileMessages.WSGEN_HELP("WSGEN",
alanb@368 405 ((WsgenOptions)options).protocols,
alanb@368 406 ((WsgenOptions)options).nonstdProtocols.keySet()));
alanb@368 407 System.out.println(WscompileMessages.WSGEN_USAGE_EXAMPLES());
alanb@368 408 }
ohair@286 409 }
ohair@286 410
ohair@286 411 class Listener extends WsimportListener {
ohair@286 412 ConsoleErrorReporter cer = new ConsoleErrorReporter(out == null ? new PrintStream(new NullStream()) : out);
ohair@286 413
ohair@286 414 @Override
ohair@286 415 public void generatedFile(String fileName) {
ohair@286 416 message(fileName);
ohair@286 417 }
ohair@286 418
ohair@286 419 @Override
ohair@286 420 public void message(String msg) {
ohair@286 421 out.println(msg);
ohair@286 422 }
ohair@286 423
ohair@286 424 @Override
ohair@286 425 public void error(SAXParseException exception) {
ohair@286 426 cer.error(exception);
ohair@286 427 }
ohair@286 428
ohair@286 429 @Override
ohair@286 430 public void fatalError(SAXParseException exception) {
ohair@286 431 cer.fatalError(exception);
ohair@286 432 }
ohair@286 433
ohair@286 434 @Override
ohair@286 435 public void warning(SAXParseException exception) {
ohair@286 436 cer.warning(exception);
ohair@286 437 }
ohair@286 438
ohair@286 439 @Override
ohair@286 440 public void info(SAXParseException exception) {
ohair@286 441 cer.info(exception);
ohair@286 442 }
ohair@286 443 }
ohair@286 444 }

mercurial