src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/WsimportTool.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -25,10 +25,11 @@
package com.sun.tools.internal.ws.wscompile;
import com.sun.codemodel.internal.CodeWriter;
import com.sun.codemodel.internal.writer.ProgressCodeWriter;
+import com.sun.istack.internal.tools.DefaultAuthenticator;
import com.sun.tools.internal.ws.ToolVersion;
import com.sun.tools.internal.ws.api.TJavaGeneratorExtension;
import com.sun.tools.internal.ws.processor.generator.CustomExceptionGenerator;
import com.sun.tools.internal.ws.processor.generator.GeneratorBase;
import com.sun.tools.internal.ws.processor.generator.SeiGenerator;
@@ -53,13 +54,14 @@
import javax.xml.bind.JAXBPermission;
import javax.xml.stream.*;
import javax.xml.ws.EndpointContext;
import java.io.*;
import java.util.*;
-import java.net.Authenticator;
+import java.text.MessageFormat;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
+import org.xml.sax.Locator;
import org.xml.sax.SAXException;
/**
* @author Vivek Pandey
*/
@@ -132,15 +134,17 @@
public Receiver(Listener listener) {
super(listener);
this.listener = listener;
}
+ @Override
public void info(SAXParseException exception) {
if (options.verbose)
super.info(exception);
}
+ @Override
public void warning(SAXParseException exception) {
if (!options.quiet)
super.warning(exception);
}
@@ -177,17 +181,14 @@
WscompileMessages.WSIMPORT_FULLVERSION(ToolVersion.VERSION.toString()));
return true;
}
}
- Authenticator orig = null;
try {
parseArguments(args, listener, receiver);
try {
- orig = DefaultAuthenticator.getCurrentAuthenticator();
-
Model wsdlModel = buildWsdlModel(listener, receiver);
if (wsdlModel == null)
return false;
if (!generateCode(listener, receiver, wsdlModel, true))
@@ -233,11 +234,11 @@
usage(e.getOptions());
return false;
} finally{
deleteGeneratedFiles();
if (!options.disableAuthenticator) {
- Authenticator.setDefault(orig);
+ DefaultAuthenticator.reset();
}
}
if(receiver.hadError()) {
return false;
}
@@ -251,31 +252,34 @@
//remove all non-java artifacts as they will packaged in jar.
Iterable<File> generatedFiles = options.getGeneratedFiles();
synchronized (generatedFiles) {
for (File file : generatedFiles) {
if (!file.getName().endsWith(".java")) {
- file.delete();
+ boolean deleted = file.delete();
+ if (options.verbose && !deleted) {
+ System.out.println(MessageFormat.format("{0} could not be deleted.", file));
+ }
trackedRootPackages.add(file.getParentFile());
-
}
-
}
}
//remove empty package dirs
for(File pkg:trackedRootPackages) {
while(pkg.list() != null && pkg.list().length ==0 && !pkg.equals(options.destDir)) {
File parentPkg = pkg.getParentFile();
- pkg.delete();
+ boolean deleted = pkg.delete();
+ if (options.verbose && !deleted) {
+ System.out.println(MessageFormat.format("{0} could not be deleted.", pkg));
+ }
pkg = parentPkg;
}
}
}
if(!options.keep) {
options.removeGeneratedFiles();
}
-
}
private void addClassesToGeneratedFiles() throws IOException {
Iterable<File> generatedFiles = options.getGeneratedFiles();
final List<File> trackedClassFiles = new ArrayList<File>();
@@ -284,11 +288,11 @@
String relativeDir = DirectoryUtil.getRelativePathfromCommonBase(f.getParentFile(),options.sourceDir);
final String className = f.getName().substring(0,f.getName().indexOf(".java"));
File classDir = new File(options.destDir,relativeDir);
if(classDir.exists()) {
classDir.listFiles(new FilenameFilter() {
-
+ @Override
public boolean accept(File dir, String name) {
if(name.equals(className+".class") || (name.startsWith(className+"$") && name.endsWith(".class"))) {
trackedClassFiles.add(new File(dir,name));
return true;
}
@@ -307,46 +311,54 @@
File zipFile = new File(options.clientjar);
if(!zipFile.isAbsolute()) {
zipFile = new File(options.destDir, options.clientjar);
}
- if (zipFile.exists()) {
- //TODO
- }
- FileOutputStream fos = null;
- if( !options.quiet )
+ FileOutputStream fos;
+ if (!options.quiet) {
listener.message(WscompileMessages.WSIMPORT_ARCHIVING_ARTIFACTS(zipFile));
+ }
-
+ BufferedInputStream bis = null;
+ FileInputStream fi = null;
fos = new FileOutputStream(zipFile);
JarOutputStream jos = new JarOutputStream(fos);
-
+ try {
String base = options.destDir.getCanonicalPath();
for(File f: options.getGeneratedFiles()) {
//exclude packaging the java files in the jar
if(f.getName().endsWith(".java")) {
continue;
}
if(options.verbose) {
listener.message(WscompileMessages.WSIMPORT_ARCHIVE_ARTIFACT(f, options.clientjar));
}
String entry = f.getCanonicalPath().substring(base.length()+1);
- BufferedInputStream bis = new BufferedInputStream(
- new FileInputStream(f));
+ fi = new FileInputStream(f);
+ bis = new BufferedInputStream(fi);
JarEntry jarEntry = new JarEntry(entry);
jos.putNextEntry(jarEntry);
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) {
jos.write(buffer, 0, bytesRead);
}
+ }
+ } finally {
+ try {
+ if (bis != null) {
bis.close();
-
}
-
+ } finally {
+ if (jos != null) {
jos.close();
-
+ }
+ if (fi != null) {
+ fi.close();
+ }
+ }
+ }
}
protected void parseArguments(String[] args, Listener listener,
Receiver receiver) throws BadCommandLineException {
options.parseArguments(args);
@@ -354,19 +366,60 @@
if (options.debugMode)
listener.enableDebugging();
options.parseBindings(receiver);
}
- protected Model buildWsdlModel(Listener listener,
- Receiver receiver) throws BadCommandLineException, XMLStreamException, IOException {
- if( !options.quiet )
- listener.message(WscompileMessages.WSIMPORT_PARSING_WSDL());
-
+ protected Model buildWsdlModel(Listener listener, final Receiver receiver)
+ throws BadCommandLineException, XMLStreamException, IOException {
//set auth info
//if(options.authFile != null)
if (!options.disableAuthenticator) {
- Authenticator.setDefault(new DefaultAuthenticator(receiver, options.authFile));
+ class AuthListener implements DefaultAuthenticator.Receiver {
+
+ private final boolean isFatal;
+
+ AuthListener(boolean isFatal) {
+ this.isFatal = isFatal;
+ }
+
+ @Override
+ public void onParsingError(String text, Locator loc) {
+ error(new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(text), loc));
+ }
+
+ @Override
+ public void onError(Exception e, Locator loc) {
+ if (e instanceof FileNotFoundException) {
+ error(new SAXParseException(WscompileMessages.WSIMPORT_AUTH_FILE_NOT_FOUND(
+ loc.getSystemId(), WsimportOptions.defaultAuthfile), null));
+ } else {
+ error(new SAXParseException(WscompileMessages.WSIMPORT_FAILED_TO_PARSE(loc.getSystemId(),e.getMessage()), loc));
+ }
+ }
+
+ private void error(SAXParseException e) {
+ if (isFatal) {
+ receiver.error(e);
+ } else {
+ receiver.debug(e);
+ }
+ }
+ }
+
+ DefaultAuthenticator da = DefaultAuthenticator.getAuthenticator();
+ if (options.proxyAuth != null) {
+ da.setProxyAuth(options.proxyAuth);
+ }
+ if (options.authFile != null) {
+ da.setAuth(options.authFile, new AuthListener(true));
+ } else {
+ da.setAuth(new File(WsimportOptions.defaultAuthfile), new AuthListener(false));
+ }
+ }
+
+ if (!options.quiet) {
+ listener.message(WscompileMessages.WSIMPORT_PARSING_WSDL());
}
MetadataFinder forest = new MetadataFinder(new WSDLInternalizationLogic(), options, receiver);
forest.parseWSDL();
if (forest.isMexMetadata)
@@ -496,11 +549,11 @@
args[baseIndex + i] = sourceFiles.get(i);
}
listener.message(WscompileMessages.WSIMPORT_COMPILING_CODE());
if(options.verbose){
- StringBuffer argstr = new StringBuffer();
+ StringBuilder argstr = new StringBuilder();
for(String arg:args){
argstr.append(arg).append(" ");
}
listener.message("javac "+ argstr.toString());
}