1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/WsimportTool.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,528 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.wscompile; 1.30 + 1.31 +import com.sun.codemodel.internal.CodeWriter; 1.32 +import com.sun.codemodel.internal.writer.ProgressCodeWriter; 1.33 +import com.sun.tools.internal.ws.ToolVersion; 1.34 +import com.sun.tools.internal.ws.api.TJavaGeneratorExtension; 1.35 +import com.sun.tools.internal.ws.processor.generator.CustomExceptionGenerator; 1.36 +import com.sun.tools.internal.ws.processor.generator.GeneratorBase; 1.37 +import com.sun.tools.internal.ws.processor.generator.SeiGenerator; 1.38 +import com.sun.tools.internal.ws.processor.generator.ServiceGenerator; 1.39 +import com.sun.tools.internal.ws.processor.generator.JwsImplGenerator; 1.40 +import com.sun.tools.internal.ws.processor.model.Model; 1.41 +import com.sun.tools.internal.ws.processor.modeler.wsdl.ConsoleErrorReporter; 1.42 +import com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModeler; 1.43 +import com.sun.tools.internal.ws.processor.util.DirectoryUtil; 1.44 +import com.sun.tools.internal.ws.resources.WscompileMessages; 1.45 +import com.sun.tools.internal.ws.resources.WsdlMessages; 1.46 +import com.sun.tools.internal.ws.util.WSDLFetcher; 1.47 +import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder; 1.48 +import com.sun.tools.internal.ws.wsdl.parser.WSDLInternalizationLogic; 1.49 +import com.sun.tools.internal.xjc.util.NullStream; 1.50 +import com.sun.xml.internal.ws.api.server.Container; 1.51 +import com.sun.xml.internal.ws.util.ServiceFinder; 1.52 +import com.sun.istack.internal.tools.ParallelWorldClassLoader; 1.53 +import org.xml.sax.EntityResolver; 1.54 +import org.xml.sax.SAXParseException; 1.55 + 1.56 +import javax.xml.bind.JAXBPermission; 1.57 +import javax.xml.stream.*; 1.58 +import javax.xml.ws.EndpointContext; 1.59 +import java.io.*; 1.60 +import java.util.*; 1.61 +import java.net.Authenticator; 1.62 +import java.util.jar.JarEntry; 1.63 +import java.util.jar.JarOutputStream; 1.64 +import org.xml.sax.SAXException; 1.65 + 1.66 +/** 1.67 + * @author Vivek Pandey 1.68 + */ 1.69 +public class WsimportTool { 1.70 + private static final String WSIMPORT = "wsimport"; 1.71 + private final PrintStream out; 1.72 + private final Container container; 1.73 + 1.74 + /** 1.75 + * Wsimport specific options 1.76 + */ 1.77 + protected WsimportOptions options = new WsimportOptions(); 1.78 + 1.79 + public WsimportTool(OutputStream out) { 1.80 + this(out, null); 1.81 + } 1.82 + 1.83 + public WsimportTool(OutputStream logStream, Container container) { 1.84 + this.out = (logStream instanceof PrintStream)?(PrintStream)logStream:new PrintStream(logStream); 1.85 + this.container = container; 1.86 + } 1.87 + 1.88 + protected class Listener extends WsimportListener { 1.89 + ConsoleErrorReporter cer = new ConsoleErrorReporter(out == null ? new PrintStream(new NullStream()) : out); 1.90 + 1.91 + @Override 1.92 + public void generatedFile(String fileName) { 1.93 + message(fileName); 1.94 + } 1.95 + 1.96 + @Override 1.97 + public void message(String msg) { 1.98 + out.println(msg); 1.99 + } 1.100 + 1.101 + @Override 1.102 + public void error(SAXParseException exception) { 1.103 + cer.error(exception); 1.104 + } 1.105 + 1.106 + @Override 1.107 + public void fatalError(SAXParseException exception) { 1.108 + cer.fatalError(exception); 1.109 + } 1.110 + 1.111 + @Override 1.112 + public void warning(SAXParseException exception) { 1.113 + cer.warning(exception); 1.114 + } 1.115 + 1.116 + @Override 1.117 + public void debug(SAXParseException exception) { 1.118 + cer.debug(exception); 1.119 + } 1.120 + 1.121 + @Override 1.122 + public void info(SAXParseException exception) { 1.123 + cer.info(exception); 1.124 + } 1.125 + 1.126 + public void enableDebugging(){ 1.127 + cer.enableDebugging(); 1.128 + } 1.129 + } 1.130 + 1.131 + protected class Receiver extends ErrorReceiverFilter { 1.132 + 1.133 + private Listener listener; 1.134 + 1.135 + public Receiver(Listener listener) { 1.136 + super(listener); 1.137 + this.listener = listener; 1.138 + } 1.139 + 1.140 + public void info(SAXParseException exception) { 1.141 + if (options.verbose) 1.142 + super.info(exception); 1.143 + } 1.144 + 1.145 + public void warning(SAXParseException exception) { 1.146 + if (!options.quiet) 1.147 + super.warning(exception); 1.148 + } 1.149 + 1.150 + @Override 1.151 + public void pollAbort() throws AbortException { 1.152 + if (listener.isCanceled()) 1.153 + throw new AbortException(); 1.154 + } 1.155 + 1.156 + @Override 1.157 + public void debug(SAXParseException exception){ 1.158 + if(options.debugMode){ 1.159 + listener.debug(exception); 1.160 + } 1.161 + } 1.162 + } 1.163 + 1.164 + public boolean run(String[] args) { 1.165 + Listener listener = new Listener(); 1.166 + Receiver receiver = new Receiver(listener); 1.167 + return run(args, listener, receiver); 1.168 + } 1.169 + 1.170 + protected boolean run(String[] args, Listener listener, 1.171 + Receiver receiver) { 1.172 + for (String arg : args) { 1.173 + if (arg.equals("-version")) { 1.174 + listener.message( 1.175 + WscompileMessages.WSIMPORT_VERSION(ToolVersion.VERSION.MAJOR_VERSION)); 1.176 + return true; 1.177 + } 1.178 + if (arg.equals("-fullversion")) { 1.179 + listener.message( 1.180 + WscompileMessages.WSIMPORT_FULLVERSION(ToolVersion.VERSION.toString())); 1.181 + return true; 1.182 + } 1.183 + } 1.184 + 1.185 + Authenticator orig = null; 1.186 + try { 1.187 + parseArguments(args, listener, receiver); 1.188 + 1.189 + try { 1.190 + orig = DefaultAuthenticator.getCurrentAuthenticator(); 1.191 + 1.192 + Model wsdlModel = buildWsdlModel(listener, receiver); 1.193 + if (wsdlModel == null) 1.194 + return false; 1.195 + 1.196 + if (!generateCode(listener, receiver, wsdlModel, true)) 1.197 + return false; 1.198 + 1.199 + /* Not so fast! 1.200 + } catch(AbortException e){ 1.201 + //error might have been reported 1.202 + * 1.203 + */ 1.204 + }catch (IOException e) { 1.205 + receiver.error(e); 1.206 + return false; 1.207 + }catch (XMLStreamException e) { 1.208 + receiver.error(e); 1.209 + return false; 1.210 + } 1.211 + if (!options.nocompile){ 1.212 + if(!compileGeneratedClasses(receiver, listener)){ 1.213 + listener.message(WscompileMessages.WSCOMPILE_COMPILATION_FAILED()); 1.214 + return false; 1.215 + } 1.216 + } 1.217 + try { 1.218 + if (options.clientjar != null) { 1.219 + //add all the generated class files to the list of generated files 1.220 + addClassesToGeneratedFiles(); 1.221 + jarArtifacts(listener); 1.222 + 1.223 + } 1.224 + } catch (IOException e) { 1.225 + receiver.error(e); 1.226 + return false; 1.227 + } 1.228 + 1.229 + } catch (Options.WeAreDone done) { 1.230 + usage(done.getOptions()); 1.231 + } catch (BadCommandLineException e) { 1.232 + if (e.getMessage() != null) { 1.233 + System.out.println(e.getMessage()); 1.234 + System.out.println(); 1.235 + } 1.236 + usage(e.getOptions()); 1.237 + return false; 1.238 + } finally{ 1.239 + deleteGeneratedFiles(); 1.240 + if (!options.disableAuthenticator) { 1.241 + Authenticator.setDefault(orig); 1.242 + } 1.243 + } 1.244 + if(receiver.hadError()) { 1.245 + return false; 1.246 + } 1.247 + return true; 1.248 + } 1.249 + 1.250 + private void deleteGeneratedFiles() { 1.251 + Set<File> trackedRootPackages = new HashSet<File>(); 1.252 + 1.253 + if (options.clientjar != null) { 1.254 + //remove all non-java artifacts as they will packaged in jar. 1.255 + Iterable<File> generatedFiles = options.getGeneratedFiles(); 1.256 + synchronized (generatedFiles) { 1.257 + for (File file : generatedFiles) { 1.258 + if (!file.getName().endsWith(".java")) { 1.259 + file.delete(); 1.260 + trackedRootPackages.add(file.getParentFile()); 1.261 + 1.262 + } 1.263 + 1.264 + } 1.265 + } 1.266 + //remove empty package dirs 1.267 + for(File pkg:trackedRootPackages) { 1.268 + 1.269 + while(pkg.list() != null && pkg.list().length ==0 && !pkg.equals(options.destDir)) { 1.270 + File parentPkg = pkg.getParentFile(); 1.271 + pkg.delete(); 1.272 + pkg = parentPkg; 1.273 + } 1.274 + } 1.275 + } 1.276 + if(!options.keep) { 1.277 + options.removeGeneratedFiles(); 1.278 + } 1.279 + 1.280 + } 1.281 + 1.282 + private void addClassesToGeneratedFiles() throws IOException { 1.283 + Iterable<File> generatedFiles = options.getGeneratedFiles(); 1.284 + final List<File> trackedClassFiles = new ArrayList<File>(); 1.285 + for(File f: generatedFiles) { 1.286 + if(f.getName().endsWith(".java")) { 1.287 + String relativeDir = DirectoryUtil.getRelativePathfromCommonBase(f.getParentFile(),options.sourceDir); 1.288 + final String className = f.getName().substring(0,f.getName().indexOf(".java")); 1.289 + File classDir = new File(options.destDir,relativeDir); 1.290 + if(classDir.exists()) { 1.291 + classDir.listFiles(new FilenameFilter() { 1.292 + 1.293 + public boolean accept(File dir, String name) { 1.294 + if(name.equals(className+".class") || (name.startsWith(className+"$") && name.endsWith(".class"))) { 1.295 + trackedClassFiles.add(new File(dir,name)); 1.296 + return true; 1.297 + } 1.298 + return false; 1.299 + } 1.300 + }); 1.301 + } 1.302 + } 1.303 + } 1.304 + for(File f: trackedClassFiles) { 1.305 + options.addGeneratedFile(f); 1.306 + } 1.307 + } 1.308 + 1.309 + private void jarArtifacts(WsimportListener listener) throws IOException { 1.310 + File zipFile = new File(options.clientjar); 1.311 + if(!zipFile.isAbsolute()) { 1.312 + zipFile = new File(options.destDir, options.clientjar); 1.313 + } 1.314 + 1.315 + if (zipFile.exists()) { 1.316 + //TODO 1.317 + } 1.318 + FileOutputStream fos = null; 1.319 + if( !options.quiet ) 1.320 + listener.message(WscompileMessages.WSIMPORT_ARCHIVING_ARTIFACTS(zipFile)); 1.321 + 1.322 + 1.323 + fos = new FileOutputStream(zipFile); 1.324 + JarOutputStream jos = new JarOutputStream(fos); 1.325 + 1.326 + String base = options.destDir.getCanonicalPath(); 1.327 + for(File f: options.getGeneratedFiles()) { 1.328 + //exclude packaging the java files in the jar 1.329 + if(f.getName().endsWith(".java")) { 1.330 + continue; 1.331 + } 1.332 + if(options.verbose) { 1.333 + listener.message(WscompileMessages.WSIMPORT_ARCHIVE_ARTIFACT(f, options.clientjar)); 1.334 + } 1.335 + String entry = f.getCanonicalPath().substring(base.length()+1); 1.336 + BufferedInputStream bis = new BufferedInputStream( 1.337 + new FileInputStream(f)); 1.338 + JarEntry jarEntry = new JarEntry(entry); 1.339 + jos.putNextEntry(jarEntry); 1.340 + int bytesRead; 1.341 + byte[] buffer = new byte[1024]; 1.342 + while ((bytesRead = bis.read(buffer)) != -1) { 1.343 + jos.write(buffer, 0, bytesRead); 1.344 + } 1.345 + bis.close(); 1.346 + 1.347 + } 1.348 + 1.349 + jos.close(); 1.350 + 1.351 + } 1.352 + 1.353 + protected void parseArguments(String[] args, Listener listener, 1.354 + Receiver receiver) throws BadCommandLineException { 1.355 + options.parseArguments(args); 1.356 + options.validate(); 1.357 + if (options.debugMode) 1.358 + listener.enableDebugging(); 1.359 + options.parseBindings(receiver); 1.360 + } 1.361 + 1.362 + protected Model buildWsdlModel(Listener listener, 1.363 + Receiver receiver) throws BadCommandLineException, XMLStreamException, IOException { 1.364 + if( !options.quiet ) 1.365 + listener.message(WscompileMessages.WSIMPORT_PARSING_WSDL()); 1.366 + 1.367 + //set auth info 1.368 + //if(options.authFile != null) 1.369 + if (!options.disableAuthenticator) { 1.370 + Authenticator.setDefault(new DefaultAuthenticator(receiver, options.authFile)); 1.371 + } 1.372 + 1.373 + MetadataFinder forest = new MetadataFinder(new WSDLInternalizationLogic(), options, receiver); 1.374 + forest.parseWSDL(); 1.375 + if (forest.isMexMetadata) 1.376 + receiver.reset(); 1.377 + 1.378 + WSDLModeler wsdlModeler = new WSDLModeler(options, receiver,forest); 1.379 + Model wsdlModel = wsdlModeler.buildModel(); 1.380 + if (wsdlModel == null) { 1.381 + listener.message(WsdlMessages.PARSING_PARSE_FAILED()); 1.382 + } 1.383 + 1.384 + if(options.clientjar != null) { 1.385 + if( !options.quiet ) 1.386 + listener.message(WscompileMessages.WSIMPORT_FETCHING_METADATA()); 1.387 + options.wsdlLocation = new WSDLFetcher(options,listener).fetchWsdls(forest); 1.388 + } 1.389 + 1.390 + return wsdlModel; 1.391 + } 1.392 + 1.393 + protected boolean generateCode(Listener listener, Receiver receiver, 1.394 + Model wsdlModel, boolean generateService) 1.395 + throws IOException { 1.396 + //generated code 1.397 + if( !options.quiet ) 1.398 + listener.message(WscompileMessages.WSIMPORT_GENERATING_CODE()); 1.399 + 1.400 + TJavaGeneratorExtension[] genExtn = ServiceFinder.find(TJavaGeneratorExtension.class).toArray(); 1.401 + CustomExceptionGenerator.generate(wsdlModel, options, receiver); 1.402 + SeiGenerator.generate(wsdlModel, options, receiver, genExtn); 1.403 + if(receiver.hadError()){ 1.404 + throw new AbortException(); 1.405 + } 1.406 + if (generateService) 1.407 + { 1.408 + ServiceGenerator.generate(wsdlModel, options, receiver); 1.409 + } 1.410 + for (GeneratorBase g : ServiceFinder.find(GeneratorBase.class)) { 1.411 + g.init(wsdlModel, options, receiver); 1.412 + g.doGeneration(); 1.413 + } 1.414 + 1.415 + List<String> implFiles = null; 1.416 + if (options.isGenerateJWS) { 1.417 + implFiles = JwsImplGenerator.generate(wsdlModel, options, receiver); 1.418 + } 1.419 + 1.420 + for (Plugin plugin: options.activePlugins) { 1.421 + try { 1.422 + plugin.run(wsdlModel, options, receiver); 1.423 + } catch (SAXException sex) { 1.424 + // fatal error. error should have been reported 1.425 + return false; 1.426 + } 1.427 + } 1.428 + 1.429 + CodeWriter cw; 1.430 + if (options.filer != null) { 1.431 + cw = new FilerCodeWriter(options.sourceDir, options); 1.432 + } else { 1.433 + cw = new WSCodeWriter(options.sourceDir, options); 1.434 + } 1.435 + 1.436 + if (options.verbose) 1.437 + cw = new ProgressCodeWriter(cw, out); 1.438 + options.getCodeModel().build(cw); 1.439 + 1.440 + if (options.isGenerateJWS) { 1.441 + //move Impl files to implDestDir 1.442 + return JwsImplGenerator.moveToImplDestDir(implFiles, options, receiver); 1.443 + } 1.444 + 1.445 + return true; 1.446 + } 1.447 + 1.448 + public void setEntityResolver(EntityResolver resolver){ 1.449 + this.options.entityResolver = resolver; 1.450 + } 1.451 + 1.452 + /* 1.453 + * To take care of JDK6-JDK6u3, where 2.1 API classes are not there 1.454 + */ 1.455 + private static boolean useBootClasspath(Class clazz) { 1.456 + try { 1.457 + ParallelWorldClassLoader.toJarUrl(clazz.getResource('/'+clazz.getName().replace('.','/')+".class")); 1.458 + return true; 1.459 + } catch(Exception e) { 1.460 + return false; 1.461 + } 1.462 + } 1.463 + 1.464 + protected boolean compileGeneratedClasses(ErrorReceiver receiver, WsimportListener listener){ 1.465 + List<String> sourceFiles = new ArrayList<String>(); 1.466 + 1.467 + for (File f : options.getGeneratedFiles()) { 1.468 + if (f.exists() && f.getName().endsWith(".java")) { 1.469 + sourceFiles.add(f.getAbsolutePath()); 1.470 + } 1.471 + } 1.472 + 1.473 + if (sourceFiles.size() > 0) { 1.474 + String classDir = options.destDir.getAbsolutePath(); 1.475 + String classpathString = createClasspathString(); 1.476 + boolean bootCP = useBootClasspath(EndpointContext.class) || useBootClasspath(JAXBPermission.class); 1.477 + String[] args = new String[4 + (bootCP ? 1 : 0) + (options.debug ? 1 : 0) 1.478 + + (options.encoding != null ? 2 : 0) + sourceFiles.size()]; 1.479 + args[0] = "-d"; 1.480 + args[1] = classDir; 1.481 + args[2] = "-classpath"; 1.482 + args[3] = classpathString; 1.483 + int baseIndex = 4; 1.484 + //javac is not working in osgi as the url starts with a bundle 1.485 + if (bootCP) { 1.486 + args[baseIndex++] = "-Xbootclasspath/p:"+JavaCompilerHelper.getJarFile(EndpointContext.class)+File.pathSeparator+JavaCompilerHelper.getJarFile(JAXBPermission.class); 1.487 + } 1.488 + 1.489 + if (options.debug) { 1.490 + args[baseIndex++] = "-g"; 1.491 + } 1.492 + 1.493 + if (options.encoding != null) { 1.494 + args[baseIndex++] = "-encoding"; 1.495 + args[baseIndex++] = options.encoding; 1.496 + } 1.497 + 1.498 + for (int i = 0; i < sourceFiles.size(); ++i) { 1.499 + args[baseIndex + i] = sourceFiles.get(i); 1.500 + } 1.501 + 1.502 + listener.message(WscompileMessages.WSIMPORT_COMPILING_CODE()); 1.503 + if(options.verbose){ 1.504 + StringBuffer argstr = new StringBuffer(); 1.505 + for(String arg:args){ 1.506 + argstr.append(arg).append(" "); 1.507 + } 1.508 + listener.message("javac "+ argstr.toString()); 1.509 + } 1.510 + 1.511 + return JavaCompilerHelper.compile(args, out, receiver); 1.512 + } 1.513 + //there are no files to compile, so return true? 1.514 + return true; 1.515 + } 1.516 + 1.517 + private String createClasspathString() { 1.518 + StringBuilder classpathStr = new StringBuilder(System.getProperty("java.class.path")); 1.519 + for(String s: options.cmdlineJars) { 1.520 + classpathStr.append(File.pathSeparator); 1.521 + classpathStr.append(new File(s).toString()); 1.522 + } 1.523 + return classpathStr.toString(); 1.524 + } 1.525 + 1.526 + protected void usage(Options options) { 1.527 + System.out.println(WscompileMessages.WSIMPORT_HELP(WSIMPORT)); 1.528 + System.out.println(WscompileMessages.WSIMPORT_USAGE_EXTENSIONS()); 1.529 + System.out.println(WscompileMessages.WSIMPORT_USAGE_EXAMPLES()); 1.530 + } 1.531 +}