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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -23,10 +23,11 @@
  * questions.
  */
 
 package com.sun.tools.internal.ws.wscompile;
 
+import com.oracle.webservices.internal.api.databinding.WSDLResolver;
 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
 import com.sun.tools.internal.ws.ToolVersion;
 import com.sun.tools.internal.ws.processor.modeler.annotation.WebServiceAp;
 import com.sun.tools.internal.ws.processor.modeler.wsdl.ConsoleErrorReporter;
 import com.sun.tools.internal.ws.resources.WscompileMessages;

@@ -41,13 +42,13 @@
 import com.sun.xml.internal.ws.api.databinding.DatabindingFactory;
 import com.sun.xml.internal.ws.api.databinding.WSDLGenInfo;
 import com.sun.xml.internal.ws.api.server.Container;
 import com.sun.xml.internal.ws.api.wsdl.writer.WSDLGeneratorExtension;
 import com.sun.xml.internal.ws.binding.WebServiceFeatureList;
+import com.sun.xml.internal.ws.model.ExternalMetadataReader;
 import com.sun.xml.internal.ws.model.AbstractSEIModelImpl;
 import com.sun.xml.internal.ws.util.ServiceFinder;
-import com.sun.xml.internal.ws.wsdl.writer.WSDLResolver;
 import org.xml.sax.SAXParseException;
 
 import javax.tools.DiagnosticCollector;
 import javax.tools.JavaCompiler;
 import javax.tools.JavaFileObject;

@@ -69,10 +70,11 @@
 import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
  * @author Vivek Pandey
  */

@@ -84,20 +86,20 @@
     private final PrintStream out;
     private final WsgenOptions options = new WsgenOptions();
 
 
     public WsgenTool(OutputStream out, Container container) {
-        this.out = (out instanceof PrintStream)?(PrintStream)out:new PrintStream(out);
+        this.out = (out instanceof PrintStream) ? (PrintStream) out : new PrintStream(out);
         this.container = container;
     }
 
 
     public WsgenTool(OutputStream out) {
         this(out, null);
     }
 
-    public boolean run(String[] args){
+    public boolean run(String[] args) {
         final Listener listener = new Listener();
         for (String arg : args) {
             if (arg.equals("-version")) {
                 listener.message(
                         WscompileMessages.WSGEN_VERSION(ToolVersion.VERSION.MAJOR_VERSION));

@@ -110,49 +112,53 @@
             }
         }
         try {
             options.parseArguments(args);
             options.validate();
-            if(!buildModel(options.endpoint.getName(), listener)){
+            if (!buildModel(options.endpoint.getName(), listener)) {
                 return false;
             }
-        }catch (Options.WeAreDone done){
-            usage((WsgenOptions)done.getOptions());
-        }catch (BadCommandLineException e) {
-            if(e.getMessage()!=null) {
+        } catch (Options.WeAreDone done) {
+            usage(done.getOptions());
+        } catch (BadCommandLineException e) {
+            if (e.getMessage() != null) {
                 System.out.println(e.getMessage());
                 System.out.println();
             }
-            usage((WsgenOptions)e.getOptions());
+            usage(e.getOptions());
             return false;
-        }catch(AbortException e){
+        } catch (AbortException e) {
             //error might have been reported
-        }finally{
-            if(!options.keep){
+        } finally {
+            if (!options.keep) {
                 options.removeGeneratedFiles();
             }
         }
         return true;
     }
 
     private final Container container;
 
-    private int round = 0;
-
     /*
      * To take care of JDK6-JDK6u3, where 2.1 API classes are not there
      */
     private static boolean useBootClasspath(Class clazz) {
         try {
-            ParallelWorldClassLoader.toJarUrl(clazz.getResource('/'+clazz.getName().replace('.','/')+".class"));
+            ParallelWorldClassLoader.toJarUrl(clazz.getResource('/' + clazz.getName().replace('.', '/') + ".class"));
             return true;
-        } catch(Exception e) {
+        } catch (Exception e) {
             return false;
         }
     }
 
-
+    /**
+     *
+     * @param endpoint
+     * @param listener
+     * @return
+     * @throws BadCommandLineException
+     */
     public boolean buildModel(String endpoint, Listener listener) throws BadCommandLineException {
         final ErrorReceiverFilter errReceiver = new ErrorReceiverFilter(listener);
 
         boolean bootCP = useBootClasspath(EndpointReference.class) || useBootClasspath(XmlSeeAlso.class);
         Collection<String> args = new ArrayList<String>(6 + (bootCP ? 1 : 0) + (options.nocompile ? 1 : 0)

@@ -176,11 +182,11 @@
                     .append(JavaCompilerHelper.getJarFile(EndpointReference.class))
                     .append(File.pathSeparator)
                     .append(JavaCompilerHelper.getJarFile(XmlSeeAlso.class)).toString());
         }
 
-        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();//        compiler = JavacTool.create();
         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
         StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
         JavaCompiler.CompilationTask task = compiler.getTask(
                 null,
                 fileManager,

@@ -195,11 +201,18 @@
             out.println(WscompileMessages.WSCOMPILE_ERROR(WscompileMessages.WSCOMPILE_COMPILATION_FAILED()));
             return false;
         }
         if (options.genWsdl) {
                 DatabindingConfig config = new DatabindingConfig();
-            String tmpPath = options.destDir.getAbsolutePath()+ File.pathSeparator+options.classpath;
+
+            List<String> externalMetadataFileNames = options.externalMetadataFiles;
+            boolean disableSecureXmlProcessing = options.disableSecureXmlProcessing;
+            if (externalMetadataFileNames != null && externalMetadataFileNames.size() > 0) {
+                config.setMetadataReader(new ExternalMetadataReader(getExternalFiles(externalMetadataFileNames), null, null, true, disableSecureXmlProcessing));
+            }
+
+            String tmpPath = options.destDir.getAbsolutePath() + File.pathSeparator + options.classpath;
             ClassLoader classLoader = new URLClassLoader(Options.pathToURLs(tmpPath),
                     this.getClass().getClassLoader());
             Class<?> endpointClass;
             try {
                 endpointClass = classLoader.loadClass(endpoint);

@@ -222,21 +235,24 @@
                 config.setEndpointClass(endpointClass);
                 config.getMappingInfo().setServiceName(options.serviceName);
                 config.setFeatures(wsfeatures.toArray());
                 config.setClassLoader(classLoader);
                 config.getMappingInfo().setBindingID(bindingID);
-                com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl)fac.createRuntime(config);
+            com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl) fac.createRuntime(config);
 
             final File[] wsdlFileName = new File[1]; // used to capture the generated WSDL file.
-            final Map<String,File> schemaFiles = new HashMap<String,File>();
+            final Map<String, File> schemaFiles = new HashMap<String, File>();
 
             WSDLGenInfo wsdlGenInfo = new WSDLGenInfo();
+            wsdlGenInfo.setSecureXmlProcessingDisabled(disableSecureXmlProcessing);
+
             wsdlGenInfo.setWsdlResolver(
                     new WSDLResolver() {
                         private File toFile(String suggestedFilename) {
                             return new File(options.nonclassDestDir, suggestedFilename);
                         }
+
                         private Result toResult(File file) {
                             Result result;
                             try {
                                 result = new StreamResult(new FileOutputStream(file));
                                 result.setSystemId(file.getPath().replace('\\', '/'));

@@ -245,25 +261,31 @@
                                 return null;
                             }
                             return result;
                         }
 
+                        @Override
                         public Result getWSDL(String suggestedFilename) {
                             File f = toFile(suggestedFilename);
                             wsdlFileName[0] = f;
                             return toResult(f);
                         }
+
                         public Result getSchemaOutput(String namespace, String suggestedFilename) {
                             if (namespace == null)
                                 return null;
                             File f = toFile(suggestedFilename);
-                            schemaFiles.put(namespace,f);
+                            schemaFiles.put(namespace, f);
                             return toResult(f);
                         }
+
+                        @Override
                         public Result getAbstractWSDL(Holder<String> filename) {
                             return toResult(toFile(filename.value));
                         }
+
+                        @Override
                         public Result getSchemaOutput(String namespace, Holder<String> filename) {
                             return getSchemaOutput(namespace, filename.value);
                         }
                         // TODO pass correct impl's class name
                     });

@@ -272,21 +294,35 @@
             wsdlGenInfo.setExtensions(ServiceFinder.find(WSDLGeneratorExtension.class).toArray());
             wsdlGenInfo.setInlineSchemas(options.inlineSchemas);
             rt.generateWSDL(wsdlGenInfo);
 
 
-            if(options.wsgenReport!=null)
-                generateWsgenReport(endpointClass,(AbstractSEIModelImpl)rt.getModel(),wsdlFileName[0],schemaFiles);
+            if (options.wsgenReport != null)
+                generateWsgenReport(endpointClass, (AbstractSEIModelImpl) rt.getModel(), wsdlFileName[0], schemaFiles);
         }
         return true;
     }
 
+    private List<File> getExternalFiles(List<String> exts) {
+        List<File> files = new ArrayList<File>();
+        for (String ext : exts) {
+            // first try absolute path ...
+            File file = new File(ext);
+            if (!file.exists()) {
+                // then relative path ...
+                file = new File(options.sourceDir.getAbsolutePath() + File.separator + ext);
+            }
+            files.add(file);
+        }
+        return files;
+    }
+
     /**
      * Generates a small XML file that captures the key activity of wsgen,
      * so that test harness can pick up artifacts.
      */
-    private void generateWsgenReport(Class<?> endpointClass, AbstractSEIModelImpl rtModel, File wsdlFile, Map<String,File> schemaFiles) {
+    private void generateWsgenReport(Class<?> endpointClass, AbstractSEIModelImpl rtModel, File wsdlFile, Map<String, File> schemaFiles) {
         try {
             ReportOutput.Report report = TXW.create(ReportOutput.Report.class,
                 new StreamSerializer(new BufferedOutputStream(new FileOutputStream(options.wsgenReport))));
 
             report.wsdl(wsdlFile.getAbsolutePath());

@@ -294,11 +330,11 @@
             ReportOutput.writeQName(rtModel.getPortName(), report.port());
             ReportOutput.writeQName(rtModel.getPortTypeName(), report.portType());
 
             report.implClass(endpointClass.getName());
 
-            for (Map.Entry<String,File> e : schemaFiles.entrySet()) {
+            for (Map.Entry<String, File> e : schemaFiles.entrySet()) {
                 ReportOutput.Schema s = report.schema();
                 s.ns(e.getKey());
                 s.location(e.getValue().getAbsolutePath());
             }
 

@@ -315,14 +351,17 @@
     static class ReportOutput {
         @XmlElement("report")
         interface Report extends TypedXmlWriter {
             @XmlElement
             void wsdl(String file); // location of WSDL
+
             @XmlElement
             QualifiedName portType();
+
             @XmlElement
             QualifiedName service();
+
             @XmlElement
             QualifiedName port();
 
             /**
              * Name of the class that has {@link javax.jws.WebService}.

@@ -335,35 +374,41 @@
         }
 
         interface QualifiedName extends TypedXmlWriter {
             @XmlAttribute
             void uri(String ns);
+
             @XmlAttribute
             void localName(String localName);
         }
 
         interface Schema extends TypedXmlWriter {
             @XmlAttribute
             void ns(String ns);
+
             @XmlAttribute
             void location(String filePath);
         }
 
-        private static void writeQName( QName n, QualifiedName w ) {
+        private static void writeQName(QName n, QualifiedName w) {
             w.uri(n.getNamespaceURI());
             w.localName(n.getLocalPart());
         }
     }
 
-    protected void usage(WsgenOptions options) {
+    protected void usage(Options options) {
         // Just don't see any point in passing WsgenOptions
         // BadCommandLineException also shouldn't have options
         if (options == null)
             options = this.options;
-        System.out.println(WscompileMessages.WSGEN_HELP("WSGEN", options.protocols, options.nonstdProtocols.keySet()));
+        if (options instanceof WsgenOptions) {
+            System.out.println(WscompileMessages.WSGEN_HELP("WSGEN",
+                    ((WsgenOptions)options).protocols,
+                    ((WsgenOptions)options).nonstdProtocols.keySet()));
         System.out.println(WscompileMessages.WSGEN_USAGE_EXAMPLES());
     }
+    }
 
     class Listener extends WsimportListener {
         ConsoleErrorReporter cer = new ConsoleErrorReporter(out == null ? new PrintStream(new NullStream()) : out);
 
         @Override