< prev index next >

jaxws/src/jdk.xml.ws/share/classes/com/sun/tools/internal/ws/wscompile/WsgenTool.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 127         } finally {
 128             if (!options.keep) {
 129                 options.removeGeneratedFiles();
 130             }
 131         }
 132         return true;
 133     }
 134 
 135     private final Container container;
 136 
 137     /**
 138      *
 139      * @param endpoint
 140      * @param listener
 141      * @return
 142      * @throws BadCommandLineException
 143      */
 144     public boolean buildModel(String endpoint, Listener listener) throws BadCommandLineException {
 145         final ErrorReceiverFilter errReceiver = new ErrorReceiverFilter(listener);
 146 
 147         List<String> args = new ArrayList<String>(6 + (options.nocompile ? 1 : 0)
 148                 + (options.encoding != null ? 2 : 0));
 149 
 150         args.add("--add-modules");
 151         args.add("java.xml.ws");
 152 
 153         args.add("-d");
 154         args.add(options.destDir.getAbsolutePath());
 155         args.add("-classpath");
 156         args.add(options.classpath);
 157         args.add("-s");
 158         args.add(options.sourceDir.getAbsolutePath());
 159         if (options.nocompile) {
 160             args.add("-proc:only");
 161         }
 162         if (options.encoding != null) {
 163             args.add("-encoding");
 164             args.add(options.encoding);
 165         }


 166         if (options.javacOptions != null) {
 167             args.addAll(options.getJavacOptions(args, listener));

















 168         }
 169 
 170         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 171         if (compiler == null) {
 172             out.println(WscompileMessages.WSCOMPILE_CANT_GET_COMPILER(property("java.home"), property("java.version"), property("java.vendor")));
 173             return false;
 174         }
 175         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
 176         StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
 177         JavaCompiler.CompilationTask task = compiler.getTask(
 178                 null,
 179                 fileManager,
 180                 diagnostics,
 181                 args,
 182                 Collections.singleton(endpoint.replaceAll("\\$", ".")),
 183                 null);
 184         task.setProcessors(Collections.singleton(new WebServiceAp(options, out)));
 185         boolean result = task.call();
 186 
 187         if (!result) {


 210             BindingID bindingID = options.getBindingID(options.protocol);
 211             if (!options.protocolSet) {
 212                 bindingID = BindingID.parse(endpointClass);
 213             }
 214             WebServiceFeatureList wsfeatures = new WebServiceFeatureList(endpointClass);
 215 //            RuntimeModeler rtModeler = new RuntimeModeler(endpointClass, options.serviceName, bindingID, wsfeatures.toArray());
 216 //            rtModeler.setClassLoader(classLoader);
 217             if (options.portName != null)
 218                 config.getMappingInfo().setPortName(options.portName);//rtModeler.setPortName(options.portName);
 219 //            AbstractSEIModelImpl rtModel = rtModeler.buildRuntimeModel();
 220 
 221             DatabindingFactory fac = DatabindingFactory.newInstance();
 222             config.setEndpointClass(endpointClass);
 223             config.getMappingInfo().setServiceName(options.serviceName);
 224             config.setFeatures(wsfeatures.toArray());
 225             config.setClassLoader(classLoader);
 226             config.getMappingInfo().setBindingID(bindingID);
 227             com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl) fac.createRuntime(config);
 228 
 229             final File[] wsdlFileName = new File[1]; // used to capture the generated WSDL file.
 230             final Map<String, File> schemaFiles = new HashMap<String, File>();
 231 
 232             WSDLGenInfo wsdlGenInfo = new WSDLGenInfo();
 233             wsdlGenInfo.setSecureXmlProcessingDisabled(disableXmlSecurity);
 234 
 235             wsdlGenInfo.setWsdlResolver(
 236                     new WSDLResolver() {
 237                         private File toFile(String suggestedFilename) {
 238                             return new File(options.nonclassDestDir, suggestedFilename);
 239                         }
 240 
 241                         private Result toResult(File file) {
 242                             Result result;
 243                             try {
 244                                 result = new StreamResult(new FileOutputStream(file));
 245                                 result.setSystemId(file.getPath().replace('\\', '/'));
 246                             } catch (FileNotFoundException e) {
 247                                 errReceiver.error(e);
 248                                 return null;
 249                             }
 250                             return result;


 282             wsdlGenInfo.setInlineSchemas(options.inlineSchemas);
 283             rt.generateWSDL(wsdlGenInfo);
 284 
 285 
 286             if (options.wsgenReport != null)
 287                 generateWsgenReport(endpointClass, (AbstractSEIModelImpl) rt.getModel(), wsdlFileName[0], schemaFiles);
 288         }
 289         return true;
 290     }
 291 
 292     private String property(String key) {
 293         try {
 294             String property = System.getProperty(key);
 295             return property != null ? property : "UNKNOWN";
 296         } catch (SecurityException ignored) {
 297             return "UNKNOWN";
 298         }
 299     }
 300 
 301     private List<File> getExternalFiles(List<String> exts) {
 302         List<File> files = new ArrayList<File>();
 303         for (String ext : exts) {
 304             // first try absolute path ...
 305             File file = new File(ext);
 306             if (!file.exists()) {
 307                 // then relative path ...
 308                 file = new File(options.sourceDir.getAbsolutePath() + File.separator + ext);
 309             }
 310             files.add(file);
 311         }
 312         return files;
 313     }
 314 
 315     /**
 316      * Generates a small XML file that captures the key activity of wsgen,
 317      * so that test harness can pick up artifacts.
 318      */
 319     private void generateWsgenReport(Class<?> endpointClass, AbstractSEIModelImpl rtModel, File wsdlFile, Map<String, File> schemaFiles) {
 320         try {
 321             ReportOutput.Report report = TXW.create(ReportOutput.Report.class,
 322                     new StreamSerializer(new BufferedOutputStream(new FileOutputStream(options.wsgenReport))));
 323 
 324             report.wsdl(wsdlFile.getAbsolutePath());
 325             ReportOutput.writeQName(rtModel.getServiceQName(), report.service());
 326             ReportOutput.writeQName(rtModel.getPortName(), report.port());
 327             ReportOutput.writeQName(rtModel.getPortTypeName(), report.portType());
 328 
 329             report.implClass(endpointClass.getName());
 330 
 331             for (Map.Entry<String, File> e : schemaFiles.entrySet()) {
 332                 ReportOutput.Schema s = report.schema();
 333                 s.ns(e.getKey());
 334                 s.location(e.getValue().getAbsolutePath());
 335             }
 336 
 337             report.commit();
 338         } catch (IOException e) {
 339             // this is code for the test, so we can be lousy in the error handling
 340             throw new Error(e);
 341         }




 342     }
 343 
 344     /**
 345      * "Namespace" for code needed to generate the report file.
 346      */
 347     static class ReportOutput {
 348         @XmlElement("report")
 349         interface Report extends TypedXmlWriter {
 350             @XmlElement
 351             void wsdl(String file); // location of WSDL
 352 
 353             @XmlElement
 354             QualifiedName portType();
 355 
 356             @XmlElement
 357             QualifiedName service();
 358 
 359             @XmlElement
 360             QualifiedName port();
 361 


   1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 127         } finally {
 128             if (!options.keep) {
 129                 options.removeGeneratedFiles();
 130             }
 131         }
 132         return true;
 133     }
 134 
 135     private final Container container;
 136 
 137     /**
 138      *
 139      * @param endpoint
 140      * @param listener
 141      * @return
 142      * @throws BadCommandLineException
 143      */
 144     public boolean buildModel(String endpoint, Listener listener) throws BadCommandLineException {
 145         final ErrorReceiverFilter errReceiver = new ErrorReceiverFilter(listener);
 146 
 147         List<String> args = new ArrayList<>(6 + (options.nocompile ? 1 : 0)
 148                 + (options.encoding != null ? 2 : 0));
 149 



 150         args.add("-d");
 151         args.add(options.destDir.getAbsolutePath());
 152         args.add("-classpath");
 153         args.add(options.classpath);
 154         args.add("-s");
 155         args.add(options.sourceDir.getAbsolutePath());
 156         if (options.nocompile) {
 157             args.add("-proc:only");
 158         }
 159         if (options.encoding != null) {
 160             args.add("-encoding");
 161             args.add(options.encoding);
 162         }
 163 
 164         boolean addModules = true;
 165         if (options.javacOptions != null) {
 166             List<String> javacOptions = options.getJavacOptions(args, listener);
 167             for (int i = 0; i < javacOptions.size(); i++) {
 168                 String opt = javacOptions.get(i);
 169                 if ("-source".equals(opt) && 9 >= getVersion(javacOptions.get(i + 1))) {
 170                     addModules = false;
 171                 }
 172                 if ("-target".equals(opt) && 9 >= getVersion(javacOptions.get(i + 1))) {
 173                     addModules = false;
 174                 }
 175                 if ("-release".equals(opt) && 9 >= getVersion(javacOptions.get(i + 1))) {
 176                     addModules = false;
 177                 }
 178                 args.add(opt);
 179             }
 180         }
 181         if (addModules) {
 182             args.add("--add-modules");
 183             args.add("java.xml.ws");
 184         }
 185 
 186         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 187         if (compiler == null) {
 188             out.println(WscompileMessages.WSCOMPILE_CANT_GET_COMPILER(property("java.home"), property("java.version"), property("java.vendor")));
 189             return false;
 190         }
 191         DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
 192         StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
 193         JavaCompiler.CompilationTask task = compiler.getTask(
 194                 null,
 195                 fileManager,
 196                 diagnostics,
 197                 args,
 198                 Collections.singleton(endpoint.replaceAll("\\$", ".")),
 199                 null);
 200         task.setProcessors(Collections.singleton(new WebServiceAp(options, out)));
 201         boolean result = task.call();
 202 
 203         if (!result) {


 226             BindingID bindingID = options.getBindingID(options.protocol);
 227             if (!options.protocolSet) {
 228                 bindingID = BindingID.parse(endpointClass);
 229             }
 230             WebServiceFeatureList wsfeatures = new WebServiceFeatureList(endpointClass);
 231 //            RuntimeModeler rtModeler = new RuntimeModeler(endpointClass, options.serviceName, bindingID, wsfeatures.toArray());
 232 //            rtModeler.setClassLoader(classLoader);
 233             if (options.portName != null)
 234                 config.getMappingInfo().setPortName(options.portName);//rtModeler.setPortName(options.portName);
 235 //            AbstractSEIModelImpl rtModel = rtModeler.buildRuntimeModel();
 236 
 237             DatabindingFactory fac = DatabindingFactory.newInstance();
 238             config.setEndpointClass(endpointClass);
 239             config.getMappingInfo().setServiceName(options.serviceName);
 240             config.setFeatures(wsfeatures.toArray());
 241             config.setClassLoader(classLoader);
 242             config.getMappingInfo().setBindingID(bindingID);
 243             com.sun.xml.internal.ws.db.DatabindingImpl rt = (com.sun.xml.internal.ws.db.DatabindingImpl) fac.createRuntime(config);
 244 
 245             final File[] wsdlFileName = new File[1]; // used to capture the generated WSDL file.
 246             final Map<String, File> schemaFiles = new HashMap<>();
 247 
 248             WSDLGenInfo wsdlGenInfo = new WSDLGenInfo();
 249             wsdlGenInfo.setSecureXmlProcessingDisabled(disableXmlSecurity);
 250 
 251             wsdlGenInfo.setWsdlResolver(
 252                     new WSDLResolver() {
 253                         private File toFile(String suggestedFilename) {
 254                             return new File(options.nonclassDestDir, suggestedFilename);
 255                         }
 256 
 257                         private Result toResult(File file) {
 258                             Result result;
 259                             try {
 260                                 result = new StreamResult(new FileOutputStream(file));
 261                                 result.setSystemId(file.getPath().replace('\\', '/'));
 262                             } catch (FileNotFoundException e) {
 263                                 errReceiver.error(e);
 264                                 return null;
 265                             }
 266                             return result;


 298             wsdlGenInfo.setInlineSchemas(options.inlineSchemas);
 299             rt.generateWSDL(wsdlGenInfo);
 300 
 301 
 302             if (options.wsgenReport != null)
 303                 generateWsgenReport(endpointClass, (AbstractSEIModelImpl) rt.getModel(), wsdlFileName[0], schemaFiles);
 304         }
 305         return true;
 306     }
 307 
 308     private String property(String key) {
 309         try {
 310             String property = System.getProperty(key);
 311             return property != null ? property : "UNKNOWN";
 312         } catch (SecurityException ignored) {
 313             return "UNKNOWN";
 314         }
 315     }
 316 
 317     private List<File> getExternalFiles(List<String> exts) {
 318         List<File> files = new ArrayList<>();
 319         for (String ext : exts) {
 320             // first try absolute path ...
 321             File file = new File(ext);
 322             if (!file.exists()) {
 323                 // then relative path ...
 324                 file = new File(options.sourceDir.getAbsolutePath() + File.separator + ext);
 325             }
 326             files.add(file);
 327         }
 328         return files;
 329     }
 330 
 331     /**
 332      * Generates a small XML file that captures the key activity of wsgen,
 333      * so that test harness can pick up artifacts.
 334      */
 335     private void generateWsgenReport(Class<?> endpointClass, AbstractSEIModelImpl rtModel, File wsdlFile, Map<String, File> schemaFiles) {
 336         try {
 337             ReportOutput.Report report = TXW.create(ReportOutput.Report.class,
 338                     new StreamSerializer(new BufferedOutputStream(new FileOutputStream(options.wsgenReport))));
 339 
 340             report.wsdl(wsdlFile.getAbsolutePath());
 341             ReportOutput.writeQName(rtModel.getServiceQName(), report.service());
 342             ReportOutput.writeQName(rtModel.getPortName(), report.port());
 343             ReportOutput.writeQName(rtModel.getPortTypeName(), report.portType());
 344 
 345             report.implClass(endpointClass.getName());
 346 
 347             for (Map.Entry<String, File> e : schemaFiles.entrySet()) {
 348                 ReportOutput.Schema s = report.schema();
 349                 s.ns(e.getKey());
 350                 s.location(e.getValue().getAbsolutePath());
 351             }
 352 
 353             report.commit();
 354         } catch (IOException e) {
 355             // this is code for the test, so we can be lousy in the error handling
 356             throw new Error(e);
 357         }
 358     }
 359 
 360     private float getVersion(String s) {
 361         return Float.parseFloat(s);
 362     }
 363 
 364     /**
 365      * "Namespace" for code needed to generate the report file.
 366      */
 367     static class ReportOutput {
 368         @XmlElement("report")
 369         interface Report extends TypedXmlWriter {
 370             @XmlElement
 371             void wsdl(String file); // location of WSDL
 372 
 373             @XmlElement
 374             QualifiedName portType();
 375 
 376             @XmlElement
 377             QualifiedName service();
 378 
 379             @XmlElement
 380             QualifiedName port();
 381 


< prev index next >