< prev index next >

jaxws/src/jdk.xml.ws/share/classes/com/sun/tools/internal/ws/wscompile/WsimportTool.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


  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     /** JAXWS module name. JAXWS dependency is mandatory in generated Java module. */
  67     private static final String JAXWS_MODULE = "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 {


 229         } catch (BadCommandLineException e) {
 230             if (e.getMessage() != null) {
 231                 System.out.println(e.getMessage());
 232                 System.out.println();
 233             }
 234             usage(e.getOptions());
 235             return false;
 236         } finally{
 237             deleteGeneratedFiles();
 238             if (!options.disableAuthenticator) {
 239                 DefaultAuthenticator.reset();
 240             }
 241         }
 242         if(receiver.hadError()) {
 243             return false;
 244         }
 245         return true;
 246     }
 247 
 248     private void deleteGeneratedFiles() {
 249         Set<File> trackedRootPackages = new HashSet<File>();
 250 
 251         if (options.clientjar != null) {
 252             //remove all non-java artifacts as they will packaged in jar.
 253             Iterable<File> generatedFiles = options.getGeneratedFiles();
 254             synchronized (generatedFiles) {
 255                 for (File file : generatedFiles) {
 256                     if (!file.getName().endsWith(".java")) {
 257                         boolean deleted = file.delete();
 258                         if (options.verbose && !deleted) {
 259                             System.out.println(MessageFormat.format("{0} could not be deleted.", file));
 260                         }
 261                         trackedRootPackages.add(file.getParentFile());
 262                     }
 263                 }
 264             }
 265             //remove empty package dirs
 266             for(File pkg:trackedRootPackages) {
 267 
 268                 while(pkg.list() != null && pkg.list().length ==0 && !pkg.equals(options.destDir)) {
 269                     File parentPkg = pkg.getParentFile();
 270                     boolean deleted = pkg.delete();
 271                     if (options.verbose && !deleted) {
 272                         System.out.println(MessageFormat.format("{0} could not be deleted.", pkg));
 273                     }
 274                     pkg = parentPkg;
 275                 }
 276             }
 277         }
 278         if(!options.keep) {
 279             options.removeGeneratedFiles();
 280         }
 281     }
 282 
 283     private void addClassesToGeneratedFiles() throws IOException {
 284         Iterable<File> generatedFiles = options.getGeneratedFiles();
 285         final List<File> trackedClassFiles = new ArrayList<File>();
 286         for(File f: generatedFiles) {
 287             if(f.getName().endsWith(".java")) {
 288                 String relativeDir = DirectoryUtil.getRelativePathfromCommonBase(f.getParentFile(),options.sourceDir);
 289                 final String className = f.getName().substring(0,f.getName().indexOf(".java"));
 290                 File classDir = new File(options.destDir,relativeDir);
 291                 if(classDir.exists()) {
 292                     classDir.listFiles(new FilenameFilter() {
 293                         @Override
 294                         public boolean accept(File dir, String name) {
 295                             if(name.equals(className+".class") || (name.startsWith(className+"$") && name.endsWith(".class"))) {
 296                                 trackedClassFiles.add(new File(dir,name));
 297                                 return true;
 298                             }
 299                             return false;
 300                         }
 301                     });
 302                 }
 303             }
 304         }
 305         for(File f: trackedClassFiles) {


 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_MODULE);
 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 
 547             if(options.verbose){
 548                 StringBuilder argstr = new StringBuilder();
 549                 for(String arg:args){
 550                     argstr.append(arg).append(" ");
 551                 }
 552                 listener.message("javac "+ argstr.toString());
 553             }
 554 
 555             return JavaCompilerHelper.compile(args.toArray(new String[args.size()]), out, receiver);
 556         }
 557         //there are no files to compile, so return true?
 558         return true;
 559     }
 560 
 561     private String createClasspathString() {
 562         StringBuilder classpathStr = new StringBuilder(System.getProperty("java.class.path"));
 563         for(String s: options.cmdlineJars) {
 564             classpathStr.append(File.pathSeparator);
 565             classpathStr.append(new File(s).toString());
 566         }
 567         return classpathStr.toString();
 568     }
 569 
 570     protected void usage(Options options) {
 571         System.out.println(WscompileMessages.WSIMPORT_HELP(WSIMPORT));
 572         System.out.println(WscompileMessages.WSIMPORT_USAGE_EXTENSIONS());
 573         System.out.println(WscompileMessages.WSIMPORT_USAGE_EXAMPLES());




 574     }
 575 }
   1 /*
   2  * Copyright (c) 2016, 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


  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 {


 229         } catch (BadCommandLineException e) {
 230             if (e.getMessage() != null) {
 231                 System.out.println(e.getMessage());
 232                 System.out.println();
 233             }
 234             usage(e.getOptions());
 235             return false;
 236         } finally{
 237             deleteGeneratedFiles();
 238             if (!options.disableAuthenticator) {
 239                 DefaultAuthenticator.reset();
 240             }
 241         }
 242         if(receiver.hadError()) {
 243             return false;
 244         }
 245         return true;
 246     }
 247 
 248     private void deleteGeneratedFiles() {
 249         Set<File> trackedRootPackages = new HashSet<>();
 250 
 251         if (options.clientjar != null) {
 252             //remove all non-java artifacts as they will packaged in jar.
 253             Iterable<File> generatedFiles = options.getGeneratedFiles();
 254             synchronized (generatedFiles) {
 255                 for (File file : generatedFiles) {
 256                     if (!file.getName().endsWith(".java")) {
 257                         boolean deleted = file.delete();
 258                         if (options.verbose && !deleted) {
 259                             System.out.println(MessageFormat.format("{0} could not be deleted.", file));
 260                         }
 261                         trackedRootPackages.add(file.getParentFile());
 262                     }
 263                 }
 264             }
 265             //remove empty package dirs
 266             for(File pkg:trackedRootPackages) {
 267 
 268                 while(pkg.list() != null && pkg.list().length ==0 && !pkg.equals(options.destDir)) {
 269                     File parentPkg = pkg.getParentFile();
 270                     boolean deleted = pkg.delete();
 271                     if (options.verbose && !deleted) {
 272                         System.out.println(MessageFormat.format("{0} could not be deleted.", pkg));
 273                     }
 274                     pkg = parentPkg;
 275                 }
 276             }
 277         }
 278         if(!options.keep) {
 279             options.removeGeneratedFiles();
 280         }
 281     }
 282 
 283     private void addClassesToGeneratedFiles() throws IOException {
 284         Iterable<File> generatedFiles = options.getGeneratedFiles();
 285         final List<File> trackedClassFiles = new ArrayList<>();
 286         for(File f: generatedFiles) {
 287             if(f.getName().endsWith(".java")) {
 288                 String relativeDir = DirectoryUtil.getRelativePathfromCommonBase(f.getParentFile(),options.sourceDir);
 289                 final String className = f.getName().substring(0,f.getName().indexOf(".java"));
 290                 File classDir = new File(options.destDir,relativeDir);
 291                 if(classDir.exists()) {
 292                     classDir.listFiles(new FilenameFilter() {
 293                         @Override
 294                         public boolean accept(File dir, String name) {
 295                             if(name.equals(className+".class") || (name.startsWith(className+"$") && name.endsWith(".class"))) {
 296                                 trackedClassFiles.add(new File(dir,name));
 297                                 return true;
 298                             }
 299                             return false;
 300                         }
 301                     });
 302                 }
 303             }
 304         }
 305         for(File f: trackedClassFiles) {


 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<>();
 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<>();



 519 
 520             args.add("-d");
 521             args.add(classDir);
 522             args.add("-classpath");
 523             args.add(classpathString);
 524 
 525             if (options.debug) {
 526                 args.add("-g");
 527             }
 528 
 529             if (options.encoding != null) {
 530                 args.add("-encoding");
 531                 args.add(options.encoding);
 532             }
 533 
 534             boolean addModules = true;
 535             if (options.javacOptions != null) {
 536                 List<String> javacOptions = options.getJavacOptions(args, listener);
 537                 for (int i = 0; i < javacOptions.size(); i++) {
 538                     String opt = javacOptions.get(i);
 539                     if ("-source".equals(opt) && 9 >= getVersion(javacOptions.get(i + 1))) {
 540                         addModules = false;
 541                     }
 542                     if ("-target".equals(opt) && 9 >= getVersion(javacOptions.get(i + 1))) {
 543                         addModules = false;
 544                     }
 545                     if ("-release".equals(opt) && 9 >= getVersion(javacOptions.get(i + 1))) {
 546                         addModules = false;
 547                     }
 548                     args.add(opt);
 549                 }
 550             }
 551             if (addModules) {
 552                 args.add("--add-modules");
 553                 args.add("java.xml.ws");
 554             }
 555 
 556             for (int i = 0; i < sourceFiles.size(); ++i) {
 557                 args.add(sourceFiles.get(i));
 558             }
 559 
 560             if (!options.quiet) listener.message(WscompileMessages.WSIMPORT_COMPILING_CODE());
 561 
 562             if(options.verbose){
 563                 StringBuilder argstr = new StringBuilder();
 564                 for(String arg:args){
 565                     argstr.append(arg).append(" ");
 566                 }
 567                 listener.message("javac "+ argstr.toString());
 568             }
 569 
 570             return JavaCompilerHelper.compile(args.toArray(new String[args.size()]), out, receiver);
 571         }
 572         //there are no files to compile, so return true?
 573         return true;
 574     }
 575 
 576     private String createClasspathString() {
 577         StringBuilder classpathStr = new StringBuilder(System.getProperty("java.class.path"));
 578         for(String s: options.cmdlineJars) {
 579             classpathStr.append(File.pathSeparator);
 580             classpathStr.append(new File(s).toString());
 581         }
 582         return classpathStr.toString();
 583     }
 584 
 585     protected void usage(Options options) {
 586         System.out.println(WscompileMessages.WSIMPORT_HELP(WSIMPORT));
 587         System.out.println(WscompileMessages.WSIMPORT_USAGE_EXTENSIONS());
 588         System.out.println(WscompileMessages.WSIMPORT_USAGE_EXAMPLES());
 589     }
 590 
 591     private float getVersion(String s) {
 592         return Float.parseFloat(s);
 593     }
 594 }
< prev index next >