1 /* |
1 /* |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
25 |
25 |
26 package com.sun.tools.internal.ws.wscompile; |
26 package com.sun.tools.internal.ws.wscompile; |
27 |
27 |
28 import com.sun.codemodel.internal.CodeWriter; |
28 import com.sun.codemodel.internal.CodeWriter; |
29 import com.sun.codemodel.internal.writer.ProgressCodeWriter; |
29 import com.sun.codemodel.internal.writer.ProgressCodeWriter; |
|
30 import com.sun.istack.internal.tools.DefaultAuthenticator; |
30 import com.sun.tools.internal.ws.ToolVersion; |
31 import com.sun.tools.internal.ws.ToolVersion; |
31 import com.sun.tools.internal.ws.api.TJavaGeneratorExtension; |
32 import com.sun.tools.internal.ws.api.TJavaGeneratorExtension; |
32 import com.sun.tools.internal.ws.processor.generator.CustomExceptionGenerator; |
33 import com.sun.tools.internal.ws.processor.generator.CustomExceptionGenerator; |
33 import com.sun.tools.internal.ws.processor.generator.GeneratorBase; |
34 import com.sun.tools.internal.ws.processor.generator.GeneratorBase; |
34 import com.sun.tools.internal.ws.processor.generator.SeiGenerator; |
35 import com.sun.tools.internal.ws.processor.generator.SeiGenerator; |
53 import javax.xml.bind.JAXBPermission; |
54 import javax.xml.bind.JAXBPermission; |
54 import javax.xml.stream.*; |
55 import javax.xml.stream.*; |
55 import javax.xml.ws.EndpointContext; |
56 import javax.xml.ws.EndpointContext; |
56 import java.io.*; |
57 import java.io.*; |
57 import java.util.*; |
58 import java.util.*; |
58 import java.net.Authenticator; |
59 import java.text.MessageFormat; |
59 import java.util.jar.JarEntry; |
60 import java.util.jar.JarEntry; |
60 import java.util.jar.JarOutputStream; |
61 import java.util.jar.JarOutputStream; |
|
62 import org.xml.sax.Locator; |
61 import org.xml.sax.SAXException; |
63 import org.xml.sax.SAXException; |
62 |
64 |
63 /** |
65 /** |
64 * @author Vivek Pandey |
66 * @author Vivek Pandey |
65 */ |
67 */ |
177 WscompileMessages.WSIMPORT_FULLVERSION(ToolVersion.VERSION.toString())); |
181 WscompileMessages.WSIMPORT_FULLVERSION(ToolVersion.VERSION.toString())); |
178 return true; |
182 return true; |
179 } |
183 } |
180 } |
184 } |
181 |
185 |
182 Authenticator orig = null; |
|
183 try { |
186 try { |
184 parseArguments(args, listener, receiver); |
187 parseArguments(args, listener, receiver); |
185 |
188 |
186 try { |
189 try { |
187 orig = DefaultAuthenticator.getCurrentAuthenticator(); |
|
188 |
|
189 Model wsdlModel = buildWsdlModel(listener, receiver); |
190 Model wsdlModel = buildWsdlModel(listener, receiver); |
190 if (wsdlModel == null) |
191 if (wsdlModel == null) |
191 return false; |
192 return false; |
192 |
193 |
193 if (!generateCode(listener, receiver, wsdlModel, true)) |
194 if (!generateCode(listener, receiver, wsdlModel, true)) |
251 //remove all non-java artifacts as they will packaged in jar. |
252 //remove all non-java artifacts as they will packaged in jar. |
252 Iterable<File> generatedFiles = options.getGeneratedFiles(); |
253 Iterable<File> generatedFiles = options.getGeneratedFiles(); |
253 synchronized (generatedFiles) { |
254 synchronized (generatedFiles) { |
254 for (File file : generatedFiles) { |
255 for (File file : generatedFiles) { |
255 if (!file.getName().endsWith(".java")) { |
256 if (!file.getName().endsWith(".java")) { |
256 file.delete(); |
257 boolean deleted = file.delete(); |
|
258 if (options.verbose && !deleted) { |
|
259 System.out.println(MessageFormat.format("{0} could not be deleted.", file)); |
|
260 } |
257 trackedRootPackages.add(file.getParentFile()); |
261 trackedRootPackages.add(file.getParentFile()); |
258 |
|
259 } |
262 } |
260 |
|
261 } |
263 } |
262 } |
264 } |
263 //remove empty package dirs |
265 //remove empty package dirs |
264 for(File pkg:trackedRootPackages) { |
266 for(File pkg:trackedRootPackages) { |
265 |
267 |
266 while(pkg.list() != null && pkg.list().length ==0 && !pkg.equals(options.destDir)) { |
268 while(pkg.list() != null && pkg.list().length ==0 && !pkg.equals(options.destDir)) { |
267 File parentPkg = pkg.getParentFile(); |
269 File parentPkg = pkg.getParentFile(); |
268 pkg.delete(); |
270 boolean deleted = pkg.delete(); |
|
271 if (options.verbose && !deleted) { |
|
272 System.out.println(MessageFormat.format("{0} could not be deleted.", pkg)); |
|
273 } |
269 pkg = parentPkg; |
274 pkg = parentPkg; |
270 } |
275 } |
271 } |
276 } |
272 } |
277 } |
273 if(!options.keep) { |
278 if(!options.keep) { |
274 options.removeGeneratedFiles(); |
279 options.removeGeneratedFiles(); |
275 } |
280 } |
276 |
|
277 } |
281 } |
278 |
282 |
279 private void addClassesToGeneratedFiles() throws IOException { |
283 private void addClassesToGeneratedFiles() throws IOException { |
280 Iterable<File> generatedFiles = options.getGeneratedFiles(); |
284 Iterable<File> generatedFiles = options.getGeneratedFiles(); |
281 final List<File> trackedClassFiles = new ArrayList<File>(); |
285 final List<File> trackedClassFiles = new ArrayList<File>(); |
284 String relativeDir = DirectoryUtil.getRelativePathfromCommonBase(f.getParentFile(),options.sourceDir); |
288 String relativeDir = DirectoryUtil.getRelativePathfromCommonBase(f.getParentFile(),options.sourceDir); |
285 final String className = f.getName().substring(0,f.getName().indexOf(".java")); |
289 final String className = f.getName().substring(0,f.getName().indexOf(".java")); |
286 File classDir = new File(options.destDir,relativeDir); |
290 File classDir = new File(options.destDir,relativeDir); |
287 if(classDir.exists()) { |
291 if(classDir.exists()) { |
288 classDir.listFiles(new FilenameFilter() { |
292 classDir.listFiles(new FilenameFilter() { |
289 |
293 @Override |
290 public boolean accept(File dir, String name) { |
294 public boolean accept(File dir, String name) { |
291 if(name.equals(className+".class") || (name.startsWith(className+"$") && name.endsWith(".class"))) { |
295 if(name.equals(className+".class") || (name.startsWith(className+"$") && name.endsWith(".class"))) { |
292 trackedClassFiles.add(new File(dir,name)); |
296 trackedClassFiles.add(new File(dir,name)); |
293 return true; |
297 return true; |
294 } |
298 } |
307 File zipFile = new File(options.clientjar); |
311 File zipFile = new File(options.clientjar); |
308 if(!zipFile.isAbsolute()) { |
312 if(!zipFile.isAbsolute()) { |
309 zipFile = new File(options.destDir, options.clientjar); |
313 zipFile = new File(options.destDir, options.clientjar); |
310 } |
314 } |
311 |
315 |
312 if (zipFile.exists()) { |
316 FileOutputStream fos; |
313 //TODO |
317 if (!options.quiet) { |
314 } |
|
315 FileOutputStream fos = null; |
|
316 if( !options.quiet ) |
|
317 listener.message(WscompileMessages.WSIMPORT_ARCHIVING_ARTIFACTS(zipFile)); |
318 listener.message(WscompileMessages.WSIMPORT_ARCHIVING_ARTIFACTS(zipFile)); |
318 |
319 } |
319 |
320 |
|
321 BufferedInputStream bis = null; |
|
322 FileInputStream fi = null; |
320 fos = new FileOutputStream(zipFile); |
323 fos = new FileOutputStream(zipFile); |
321 JarOutputStream jos = new JarOutputStream(fos); |
324 JarOutputStream jos = new JarOutputStream(fos); |
322 |
325 try { |
323 String base = options.destDir.getCanonicalPath(); |
326 String base = options.destDir.getCanonicalPath(); |
324 for(File f: options.getGeneratedFiles()) { |
327 for(File f: options.getGeneratedFiles()) { |
325 //exclude packaging the java files in the jar |
328 //exclude packaging the java files in the jar |
326 if(f.getName().endsWith(".java")) { |
329 if(f.getName().endsWith(".java")) { |
327 continue; |
330 continue; |
328 } |
331 } |
329 if(options.verbose) { |
332 if(options.verbose) { |
330 listener.message(WscompileMessages.WSIMPORT_ARCHIVE_ARTIFACT(f, options.clientjar)); |
333 listener.message(WscompileMessages.WSIMPORT_ARCHIVE_ARTIFACT(f, options.clientjar)); |
331 } |
334 } |
332 String entry = f.getCanonicalPath().substring(base.length()+1); |
335 String entry = f.getCanonicalPath().substring(base.length()+1); |
333 BufferedInputStream bis = new BufferedInputStream( |
336 fi = new FileInputStream(f); |
334 new FileInputStream(f)); |
337 bis = new BufferedInputStream(fi); |
335 JarEntry jarEntry = new JarEntry(entry); |
338 JarEntry jarEntry = new JarEntry(entry); |
336 jos.putNextEntry(jarEntry); |
339 jos.putNextEntry(jarEntry); |
337 int bytesRead; |
340 int bytesRead; |
338 byte[] buffer = new byte[1024]; |
341 byte[] buffer = new byte[1024]; |
339 while ((bytesRead = bis.read(buffer)) != -1) { |
342 while ((bytesRead = bis.read(buffer)) != -1) { |
340 jos.write(buffer, 0, bytesRead); |
343 jos.write(buffer, 0, bytesRead); |
341 } |
344 } |
342 bis.close(); |
345 } |
343 |
346 } finally { |
344 } |
347 try { |
345 |
348 if (bis != null) { |
346 jos.close(); |
349 bis.close(); |
347 |
350 } |
|
351 } finally { |
|
352 if (jos != null) { |
|
353 jos.close(); |
|
354 } |
|
355 if (fi != null) { |
|
356 fi.close(); |
|
357 } |
|
358 } |
|
359 } |
348 } |
360 } |
349 |
361 |
350 protected void parseArguments(String[] args, Listener listener, |
362 protected void parseArguments(String[] args, Listener listener, |
351 Receiver receiver) throws BadCommandLineException { |
363 Receiver receiver) throws BadCommandLineException { |
352 options.parseArguments(args); |
364 options.parseArguments(args); |
354 if (options.debugMode) |
366 if (options.debugMode) |
355 listener.enableDebugging(); |
367 listener.enableDebugging(); |
356 options.parseBindings(receiver); |
368 options.parseBindings(receiver); |
357 } |
369 } |
358 |
370 |
359 protected Model buildWsdlModel(Listener listener, |
371 protected Model buildWsdlModel(Listener listener, final Receiver receiver) |
360 Receiver receiver) throws BadCommandLineException, XMLStreamException, IOException { |
372 throws BadCommandLineException, XMLStreamException, IOException { |
361 if( !options.quiet ) |
|
362 listener.message(WscompileMessages.WSIMPORT_PARSING_WSDL()); |
|
363 |
|
364 //set auth info |
373 //set auth info |
365 //if(options.authFile != null) |
374 //if(options.authFile != null) |
366 if (!options.disableAuthenticator) { |
375 if (!options.disableAuthenticator) { |
367 Authenticator.setDefault(new DefaultAuthenticator(receiver, options.authFile)); |
376 class AuthListener implements DefaultAuthenticator.Receiver { |
|
377 |
|
378 private final boolean isFatal; |
|
379 |
|
380 AuthListener(boolean isFatal) { |
|
381 this.isFatal = isFatal; |
|
382 } |
|
383 |
|
384 @Override |
|
385 public void onParsingError(String text, Locator loc) { |
|
386 error(new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(text), loc)); |
|
387 } |
|
388 |
|
389 @Override |
|
390 public void onError(Exception e, Locator loc) { |
|
391 if (e instanceof FileNotFoundException) { |
|
392 error(new SAXParseException(WscompileMessages.WSIMPORT_AUTH_FILE_NOT_FOUND( |
|
393 loc.getSystemId(), WsimportOptions.defaultAuthfile), null)); |
|
394 } else { |
|
395 error(new SAXParseException(WscompileMessages.WSIMPORT_FAILED_TO_PARSE(loc.getSystemId(),e.getMessage()), loc)); |
|
396 } |
|
397 } |
|
398 |
|
399 private void error(SAXParseException e) { |
|
400 if (isFatal) { |
|
401 receiver.error(e); |
|
402 } else { |
|
403 receiver.debug(e); |
|
404 } |
|
405 } |
|
406 } |
|
407 |
|
408 DefaultAuthenticator da = DefaultAuthenticator.getAuthenticator(); |
|
409 if (options.proxyAuth != null) { |
|
410 da.setProxyAuth(options.proxyAuth); |
|
411 } |
|
412 if (options.authFile != null) { |
|
413 da.setAuth(options.authFile, new AuthListener(true)); |
|
414 } else { |
|
415 da.setAuth(new File(WsimportOptions.defaultAuthfile), new AuthListener(false)); |
|
416 } |
|
417 } |
|
418 |
|
419 if (!options.quiet) { |
|
420 listener.message(WscompileMessages.WSIMPORT_PARSING_WSDL()); |
368 } |
421 } |
369 |
422 |
370 MetadataFinder forest = new MetadataFinder(new WSDLInternalizationLogic(), options, receiver); |
423 MetadataFinder forest = new MetadataFinder(new WSDLInternalizationLogic(), options, receiver); |
371 forest.parseWSDL(); |
424 forest.parseWSDL(); |
372 if (forest.isMexMetadata) |
425 if (forest.isMexMetadata) |
496 args[baseIndex + i] = sourceFiles.get(i); |
549 args[baseIndex + i] = sourceFiles.get(i); |
497 } |
550 } |
498 |
551 |
499 listener.message(WscompileMessages.WSIMPORT_COMPILING_CODE()); |
552 listener.message(WscompileMessages.WSIMPORT_COMPILING_CODE()); |
500 if(options.verbose){ |
553 if(options.verbose){ |
501 StringBuffer argstr = new StringBuffer(); |
554 StringBuilder argstr = new StringBuilder(); |
502 for(String arg:args){ |
555 for(String arg:args){ |
503 argstr.append(arg).append(" "); |
556 argstr.append(arg).append(" "); |
504 } |
557 } |
505 listener.message("javac "+ argstr.toString()); |
558 listener.message("javac "+ argstr.toString()); |
506 } |
559 } |