1 /*
2 * Copyright (c) 1997, 2014, 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
30 import com.sun.istack.internal.tools.DefaultAuthenticator;
31 import com.sun.tools.internal.ws.ToolVersion;
32 import com.sun.tools.internal.ws.api.TJavaGeneratorExtension;
33 import com.sun.tools.internal.ws.processor.generator.CustomExceptionGenerator;
34 import com.sun.tools.internal.ws.processor.generator.GeneratorBase;
35 import com.sun.tools.internal.ws.processor.generator.SeiGenerator;
36 import com.sun.tools.internal.ws.processor.generator.ServiceGenerator;
37 import com.sun.tools.internal.ws.processor.generator.JwsImplGenerator;
38 import com.sun.tools.internal.ws.processor.model.Model;
39 import com.sun.tools.internal.ws.processor.modeler.wsdl.ConsoleErrorReporter;
40 import com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModeler;
41 import com.sun.tools.internal.ws.processor.util.DirectoryUtil;
42 import com.sun.tools.internal.ws.resources.WscompileMessages;
43 import com.sun.tools.internal.ws.resources.WsdlMessages;
44 import com.sun.tools.internal.ws.util.WSDLFetcher;
45 import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
46 import com.sun.tools.internal.ws.wsdl.parser.WSDLInternalizationLogic;
47 import com.sun.tools.internal.xjc.util.NullStream;
48 import com.sun.xml.internal.ws.api.server.Container;
49 import com.sun.xml.internal.ws.util.ServiceFinder;
50 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
51 import org.xml.sax.EntityResolver;
52 import org.xml.sax.SAXParseException;
53
54 import javax.xml.bind.JAXBPermission;
55 import javax.xml.stream.*;
56 import javax.xml.ws.EndpointContext;
57 import java.io.*;
58 import java.util.*;
59 import java.text.MessageFormat;
60 import java.util.jar.JarEntry;
61 import java.util.jar.JarOutputStream;
62 import org.xml.sax.Locator;
63 import org.xml.sax.SAXException;
64
65 /**
66 * @author Vivek Pandey
67 */
68 public class WsimportTool {
69 private static final String WSIMPORT = "wsimport";
70 private final PrintStream out;
71 private final Container container;
72
73 /**
74 * Wsimport specific options
75 */
76 protected WsimportOptions options = new WsimportOptions();
77
78 public WsimportTool(OutputStream out) {
79 this(out, null);
80 }
81
82 public WsimportTool(OutputStream logStream, Container container) {
83 this.out = (logStream instanceof PrintStream)?(PrintStream)logStream:new PrintStream(logStream);
84 this.container = container;
85 }
86
87 protected class Listener extends WsimportListener {
88 ConsoleErrorReporter cer = new ConsoleErrorReporter(out == null ? new PrintStream(new NullStream()) : out);
459 }
460 for (GeneratorBase g : ServiceFinder.find(GeneratorBase.class)) {
461 g.init(wsdlModel, options, receiver);
462 g.doGeneration();
463 }
464
465 List<String> implFiles = null;
466 if (options.isGenerateJWS) {
467 implFiles = JwsImplGenerator.generate(wsdlModel, options, receiver);
468 }
469
470 for (Plugin plugin: options.activePlugins) {
471 try {
472 plugin.run(wsdlModel, options, receiver);
473 } catch (SAXException sex) {
474 // fatal error. error should have been reported
475 return false;
476 }
477 }
478
479 CodeWriter cw;
480 if (options.filer != null) {
481 cw = new FilerCodeWriter(options.sourceDir, options);
482 } else {
483 cw = new WSCodeWriter(options.sourceDir, options);
484 }
485
486 if (options.verbose)
487 cw = new ProgressCodeWriter(cw, out);
488 options.getCodeModel().build(cw);
489
490 if (options.isGenerateJWS) {
491 //move Impl files to implDestDir
492 return JwsImplGenerator.moveToImplDestDir(implFiles, options, receiver);
493 }
494
495 return true;
496 }
497
498 public void setEntityResolver(EntityResolver resolver){
499 this.options.entityResolver = resolver;
500 }
501
502 /*
503 * To take care of JDK6-JDK6u3, where 2.1 API classes are not there
504 */
505 private static boolean useBootClasspath(Class clazz) {
506 try {
507 ParallelWorldClassLoader.toJarUrl(clazz.getResource('/'+clazz.getName().replace('.','/')+".class"));
508 return true;
509 } catch(Exception e) {
510 return false;
511 }
512 }
513
514 protected boolean compileGeneratedClasses(ErrorReceiver receiver, WsimportListener listener){
515 List<String> sourceFiles = new ArrayList<String>();
516
517 for (File f : options.getGeneratedFiles()) {
518 if (f.exists() && f.getName().endsWith(".java")) {
519 sourceFiles.add(f.getAbsolutePath());
520 }
521 }
522
523 if (sourceFiles.size() > 0) {
524 String classDir = options.destDir.getAbsolutePath();
525 String classpathString = createClasspathString();
526 boolean bootCP = useBootClasspath(EndpointContext.class) || useBootClasspath(JAXBPermission.class);
527 List<String> args = new ArrayList<String>();
528 args.add("--add-modules");
529 args.add("java.xml.ws");
530 args.add("-d");
531 args.add(classDir);
532 args.add("-classpath");
533 args.add(classpathString);
534 //javac is not working in osgi as the url starts with a bundle
535 if (bootCP) {
536 args.add("-Xbootclasspath/p:"
537 + JavaCompilerHelper.getJarFile(EndpointContext.class)
538 + File.pathSeparator
539 + JavaCompilerHelper.getJarFile(JAXBPermission.class));
540 }
541
542 if (options.debug) {
543 args.add("-g");
544 }
545
546 if (options.encoding != null) {
547 args.add("-encoding");
548 args.add(options.encoding);
549 }
550
551 if (options.javacOptions != null) {
552 args.addAll(options.getJavacOptions(args, listener));
553 }
554
555 for (int i = 0; i < sourceFiles.size(); ++i) {
556 args.add(sourceFiles.get(i));
557 }
558
559 if (!options.quiet) listener.message(WscompileMessages.WSIMPORT_COMPILING_CODE());
560
|
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
30 import com.sun.istack.internal.tools.DefaultAuthenticator;
31 import com.sun.tools.internal.ws.ToolVersion;
32 import com.sun.tools.internal.ws.api.TJavaGeneratorExtension;
33 import com.sun.tools.internal.ws.processor.generator.CustomExceptionGenerator;
34 import com.sun.tools.internal.ws.processor.generator.GeneratorBase;
35 import com.sun.tools.internal.ws.processor.generator.SeiGenerator;
36 import com.sun.tools.internal.ws.processor.generator.ServiceGenerator;
37 import com.sun.tools.internal.ws.processor.generator.JwsImplGenerator;
38 import com.sun.tools.internal.ws.processor.model.Model;
39 import com.sun.tools.internal.ws.processor.modeler.wsdl.ConsoleErrorReporter;
40 import com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModeler;
41 import com.sun.tools.internal.ws.processor.util.DirectoryUtil;
42 import com.sun.tools.internal.ws.resources.WscompileMessages;
43 import com.sun.tools.internal.ws.resources.WsdlMessages;
44 import com.sun.tools.internal.ws.util.WSDLFetcher;
45 import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
46 import com.sun.tools.internal.ws.wsdl.parser.WSDLInternalizationLogic;
47 import com.sun.tools.internal.xjc.util.NullStream;
48 import com.sun.xml.internal.ws.api.server.Container;
49 import com.sun.xml.internal.ws.util.ServiceFinder;
50 import org.xml.sax.EntityResolver;
51 import org.xml.sax.SAXParseException;
52
53 import javax.xml.stream.*;
54 import java.io.*;
55 import java.util.*;
56 import java.text.MessageFormat;
57 import java.util.jar.JarEntry;
58 import java.util.jar.JarOutputStream;
59 import org.xml.sax.Locator;
60 import org.xml.sax.SAXException;
61
62 /**
63 * @author Vivek Pandey
64 */
65 public class WsimportTool {
66 /** JAXB module name. JAXB dependency is mandatory in generated Java module. */
67 private static final String JAXWS_PACKAGE = "java.xml.ws";
68
69 private static final String WSIMPORT = "wsimport";
70 private final PrintStream out;
71 private final Container container;
72
73 /**
74 * Wsimport specific options
75 */
76 protected WsimportOptions options = new WsimportOptions();
77
78 public WsimportTool(OutputStream out) {
79 this(out, null);
80 }
81
82 public WsimportTool(OutputStream logStream, Container container) {
83 this.out = (logStream instanceof PrintStream)?(PrintStream)logStream:new PrintStream(logStream);
84 this.container = container;
85 }
86
87 protected class Listener extends WsimportListener {
88 ConsoleErrorReporter cer = new ConsoleErrorReporter(out == null ? new PrintStream(new NullStream()) : out);
459 }
460 for (GeneratorBase g : ServiceFinder.find(GeneratorBase.class)) {
461 g.init(wsdlModel, options, receiver);
462 g.doGeneration();
463 }
464
465 List<String> implFiles = null;
466 if (options.isGenerateJWS) {
467 implFiles = JwsImplGenerator.generate(wsdlModel, options, receiver);
468 }
469
470 for (Plugin plugin: options.activePlugins) {
471 try {
472 plugin.run(wsdlModel, options, receiver);
473 } catch (SAXException sex) {
474 // fatal error. error should have been reported
475 return false;
476 }
477 }
478
479 if (options.getModuleName() != null) {
480 options.getCodeModel()._prepareModuleInfo(options.getModuleName(), JAXWS_PACKAGE);
481 }
482
483 CodeWriter cw;
484 if (options.filer != null) {
485 cw = new FilerCodeWriter(options);
486 } else {
487 cw = new WSCodeWriter(options.sourceDir, options);
488 }
489
490 if (options.verbose)
491 cw = new ProgressCodeWriter(cw, out);
492 options.getCodeModel().build(cw);
493
494 if (options.isGenerateJWS) {
495 //move Impl files to implDestDir
496 return JwsImplGenerator.moveToImplDestDir(implFiles, options, receiver);
497 }
498
499 return true;
500 }
501
502 public void setEntityResolver(EntityResolver resolver){
503 this.options.entityResolver = resolver;
504 }
505
506 protected boolean compileGeneratedClasses(ErrorReceiver receiver, WsimportListener listener){
507 List<String> sourceFiles = new ArrayList<String>();
508
509 for (File f : options.getGeneratedFiles()) {
510 if (f.exists() && f.getName().endsWith(".java")) {
511 sourceFiles.add(f.getAbsolutePath());
512 }
513 }
514
515 if (sourceFiles.size() > 0) {
516 String classDir = options.destDir.getAbsolutePath();
517 String classpathString = createClasspathString();
518 List<String> args = new ArrayList<String>();
519
520 args.add("--add-modules");
521 args.add("java.xml.ws");
522
523 args.add("-d");
524 args.add(classDir);
525 args.add("-classpath");
526 args.add(classpathString);
527
528 if (options.debug) {
529 args.add("-g");
530 }
531
532 if (options.encoding != null) {
533 args.add("-encoding");
534 args.add(options.encoding);
535 }
536
537 if (options.javacOptions != null) {
538 args.addAll(options.getJavacOptions(args, listener));
539 }
540
541 for (int i = 0; i < sourceFiles.size(); ++i) {
542 args.add(sourceFiles.get(i));
543 }
544
545 if (!options.quiet) listener.message(WscompileMessages.WSIMPORT_COMPILING_CODE());
546
|