8054548: JAX-WS tools need to updated to work with modular image

Removing java reflection API to get JavaCompiler; using standard javax.tools API instead

Reviewed-by: alanb
This commit is contained in:
Miroslav Kos 2014-09-12 17:20:37 +02:00
parent 8c15cc3983
commit 78ea4569e2
8 changed files with 32 additions and 327 deletions

View File

@ -1,152 +0,0 @@
/*
* Copyright (c) 1997, 2012, 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
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import com.sun.istack.internal.Nullable;
/**
* {@link ClassLoader} that loads Annotation Processing and specified classes
* both into the same classloader, so that they can reference each other.
*
* @author Bhakti Mehta
* @since 2.0 beta
*/
public final class ApClassLoader extends URLClassLoader {
/**
* List of package prefixes we want to mask the
* parent classLoader from loading
*/
private final String[] packagePrefixes;
/**
*
* @param packagePrefixes
* The package prefixes that are forced to resolve within this class loader.
* @param parent
* The parent class loader to delegate to. Null to indicate bootstrap classloader.
*/
public ApClassLoader(@Nullable ClassLoader parent, String[] packagePrefixes) throws ToolsJarNotFoundException {
super(getToolsJar(parent),parent);
if(getURLs().length==0)
// if tools.jar was found in our classloader, no need to create
// a parallel classes
this.packagePrefixes = new String[0];
else
this.packagePrefixes = packagePrefixes;
}
public Class loadClass(String className) throws ClassNotFoundException {
for( String prefix : packagePrefixes ) {
if (className.startsWith(prefix) ) {
// we need to load those classes in this class loader
// without delegation.
return findClass(className);
}
}
return super.loadClass(className);
}
protected Class findClass(String name) throws ClassNotFoundException {
StringBuilder sb = new StringBuilder(name.length() + 6);
sb.append(name.replace('.','/')).append(".class");
InputStream is = getResourceAsStream(sb.toString());
if (is==null)
throw new ClassNotFoundException("Class not found" + sb);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while((len=is.read(buf))>=0)
baos.write(buf,0,len);
buf = baos.toByteArray();
// define package if not defined yet
int i = name.lastIndexOf('.');
if (i != -1) {
String pkgname = name.substring(0, i);
Package pkg = getPackage(pkgname);
if(pkg==null)
definePackage(pkgname, null, null, null, null, null, null, null);
}
return defineClass(name,buf,0,buf.length);
} catch (IOException e) {
throw new ClassNotFoundException(name,e);
} finally {
try {
is.close();
} catch (IOException ioe) {
//ignore
}
}
}
/**
* Returns a class loader that can load classes from JDK tools.jar.
* @param parent
*/
private static URL[] getToolsJar(@Nullable ClassLoader parent) throws ToolsJarNotFoundException {
try {
Class.forName("com.sun.tools.javac.Main", false, parent);
return new URL[0];
// we can already load them in the parent class loader.
// so no need to look for tools.jar.
// this happens when we are run inside IDE/Ant, or
// in Mac OS.
} catch (ClassNotFoundException e) {
// otherwise try to find tools.jar
}
File jreHome = new File(System.getProperty("java.home"));
File toolsJar = new File( jreHome.getParent(), "lib/tools.jar" );
if (!toolsJar.exists()) {
throw new ToolsJarNotFoundException(toolsJar);
}
try {
return new URL[]{toolsJar.toURL()};
} catch (MalformedURLException e) {
// impossible
throw new AssertionError(e);
}
}
}

View File

@ -1,51 +0,0 @@
/*
* Copyright (c) 1997, 2012, 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
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.util;
import java.io.File;
/**
* Signals an error when tools.jar was not found.
*
* Simply print out the message obtained by {@link #getMessage()}.
*
* @author Kohsuke Kawaguchi
*/
public final class ToolsJarNotFoundException extends Exception {
/**
* Location where we expected to find tools.jar
*/
public final File toolsJar;
public ToolsJarNotFoundException(File toolsJar) {
super(calcMessage(toolsJar));
this.toolsJar = toolsJar;
}
private static String calcMessage(File toolsJar) {
return Messages.TOOLS_JAR_NOT_FOUND.format(toolsJar.getPath());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, 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
@ -29,19 +29,16 @@ import com.sun.istack.internal.tools.MaskingClassLoader;
import com.sun.istack.internal.tools.ParallelWorldClassLoader;
import com.sun.tools.internal.ws.resources.WscompileMessages;
import com.sun.tools.internal.ws.wscompile.Options;
import com.sun.tools.internal.xjc.api.util.ToolsJarNotFoundException;
import com.sun.xml.internal.bind.util.Which;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceFeature;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
@ -59,7 +56,7 @@ public final class Invoker {
/**
* The list of package prefixes we want the
* {@link MaskingClassLoader} to prevent the parent
* classLoader from loading
* class loader from loading
*/
static final String[] maskedPackages = new String[]{
"com.sun.istack.internal.tools.",
@ -130,24 +127,6 @@ public final class Invoker {
return -1;
}
//find and load tools.jar
List<URL> urls = new ArrayList<URL>();
findToolsJar(cl, urls);
if(urls.size() > 0){
List<String> mask = new ArrayList<String>(Arrays.asList(maskedPackages));
// first create a protected area so that we load JAXB/WS 2.1 API
// and everything that depends on them inside
cl = new MaskingClassLoader(cl,mask);
// then this classloader loads the API and tools.jar
cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), cl);
// finally load the rest of the RI. The actual class files are loaded from ancestors
cl = new ParallelWorldClassLoader(cl,"");
}
}
Thread.currentThread().setContextClassLoader(cl);
@ -158,8 +137,6 @@ public final class Invoker {
Method runMethod = compileTool.getMethod("run",String[].class);
boolean r = (Boolean)runMethod.invoke(tool,new Object[]{args});
return r ? 0 : 1;
} catch (ToolsJarNotFoundException e) {
System.err.println(e.getMessage());
} catch (InvocationTargetException e) {
throw e.getCause();
} catch(ClassNotFoundException e){
@ -167,8 +144,6 @@ public final class Invoker {
}finally {
Thread.currentThread().setContextClassLoader(oldcc);
}
return -1;
}
/**
@ -203,10 +178,10 @@ public final class Invoker {
/**
* Creates a classloader that can load JAXB/WS 2.2 API and tools.jar,
* and then return a classloader that can RI classes, which can see all those APIs and tools.jar.
* Creates a class loader that can load JAXB/WS 2.2 API,
* and then return a class loader that can RI classes, which can see all those APIs.
*/
public static ClassLoader createClassLoader(ClassLoader cl) throws ClassNotFoundException, IOException, ToolsJarNotFoundException {
public static ClassLoader createClassLoader(ClassLoader cl) throws ClassNotFoundException, IOException {
URL[] urls = findIstack22APIs(cl);
if(urls.length==0)
@ -223,7 +198,7 @@ public final class Invoker {
// and everything that depends on them inside
cl = new MaskingClassLoader(cl,mask);
// then this classloader loads the API and tools.jar
// then this class loader loads the API
cl = new URLClassLoader(urls, cl);
// finally load the rest of the RI. The actual class files are loaded from ancestors
@ -233,13 +208,13 @@ public final class Invoker {
}
/**
* Creates a classloader for loading JAXB/WS 2.2 jar and tools.jar
* Creates a class loader for loading JAXB/WS 2.2 jar
*/
private static URL[] findIstack22APIs(ClassLoader cl) throws ClassNotFoundException, IOException, ToolsJarNotFoundException {
private static URL[] findIstack22APIs(ClassLoader cl) throws ClassNotFoundException, IOException {
List<URL> urls = new ArrayList<URL>();
if(Service.class.getClassLoader()==null) {
// JAX-WS API is loaded from bootstrap classloader
// JAX-WS API is loaded from bootstrap class loader
URL res = cl.getResource("javax/xml/ws/EndpointContext.class");
if(res==null)
throw new ClassNotFoundException("There's no JAX-WS 2.2 API in the classpath");
@ -250,28 +225,7 @@ public final class Invoker {
urls.add(ParallelWorldClassLoader.toJarUrl(res));
}
findToolsJar(cl, urls);
return urls.toArray(new URL[urls.size()]);
}
private static void findToolsJar(ClassLoader cl, List<URL> urls) throws ToolsJarNotFoundException, MalformedURLException {
try {
Class.forName("com.sun.tools.javac.Main",false,cl);
// we can already load them in the parent class loader.
// so no need to look for tools.jar.
// this happens when we are run inside IDE/Ant, or
// in Mac OS.
} catch (ClassNotFoundException e) {
// otherwise try to find tools.jar
File jreHome = new File(System.getProperty("java.home"));
File toolsJar = new File( jreHome.getParent(), "lib/tools.jar" );
if (!toolsJar.exists()) {
throw new ToolsJarNotFoundException(toolsJar);
}
urls.add(toolsJar.toURL());
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, 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
@ -35,8 +35,7 @@ import com.sun.tools.internal.ws.wscompile.WsgenTool;
*/
public class WsGen {
/**
* CLI entry point. Use {@link Invoker} to
* load tools.jar
* CLI entry point. Use {@link Invoker} to load proper API version
*/
public static void main(String[] args) throws Throwable {
System.exit(Invoker.invoke("com.sun.tools.internal.ws.wscompile.WsgenTool", args));
@ -50,7 +49,7 @@ public class WsGen {
* it doesn't invoke {@link System#exit(int)}. This method
* also doesn't play with classloaders. It's the caller's
* responsibility to set up the classloader to load all jars
* needed to run the tool, including <tt>$JAVA_HOME/lib/tools.jar</tt>
* needed to run the tool.
*
* @return
* 0 if the tool runs successfully.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, 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
@ -35,8 +35,7 @@ import com.sun.tools.internal.ws.wscompile.WsimportTool;
*/
public class WsImport {
/**
* CLI entry point. Use {@link Invoker} to
* load tools.jar
* CLI entry point. Use {@link Invoker} to load proper API version
*/
public static void main(String[] args) throws Throwable {
System.exit(Invoker.invoke("com.sun.tools.internal.ws.wscompile.WsimportTool", args));
@ -50,7 +49,7 @@ public class WsImport {
* it doesn't invoke {@link System#exit(int)}. This method
* also doesn't play with classloaders. It's the caller's
* responsibility to set up the classloader to load all jars
* needed to run the tool, including <tt>$JAVA_HOME/lib/tools.jar</tt>
* needed to run the tool.
*
* @return
* 0 if the tool runs successfully.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, 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
@ -39,40 +39,16 @@ public final class JavacompilerMessages {
private final static LocalizableMessageFactory messageFactory = new LocalizableMessageFactory("com.sun.tools.internal.ws.resources.javacompiler");
private final static Localizer localizer = new Localizer();
public static Localizable localizableJAVACOMPILER_CLASSPATH_ERROR(Object arg0) {
return messageFactory.getMessage("javacompiler.classpath.error", arg0);
public static Localizable localizableNO_JAVACOMPILER_ERROR() {
return messageFactory.getMessage("no.javacompiler.error");
}
/**
* {0} is not available in the classpath, requires Sun's JDK version 5.0 or latter.
* No system compiler found, check your jdk.
*
*/
public static String JAVACOMPILER_CLASSPATH_ERROR(Object arg0) {
return localizer.localize(localizableJAVACOMPILER_CLASSPATH_ERROR(arg0));
}
public static Localizable localizableJAVACOMPILER_NOSUCHMETHOD_ERROR(Object arg0) {
return messageFactory.getMessage("javacompiler.nosuchmethod.error", arg0);
}
/**
* There is no such method {0} available, requires Sun's JDK version 5.0 or latter.
*
*/
public static String JAVACOMPILER_NOSUCHMETHOD_ERROR(Object arg0) {
return localizer.localize(localizableJAVACOMPILER_NOSUCHMETHOD_ERROR(arg0));
}
public static Localizable localizableJAVACOMPILER_ERROR(Object arg0) {
return messageFactory.getMessage("javacompiler.error", arg0);
}
/**
* error : {0}.
*
*/
public static String JAVACOMPILER_ERROR(Object arg0) {
return localizer.localize(localizableJAVACOMPILER_ERROR(arg0));
public static String NO_JAVACOMPILER_ERROR() {
return localizer.localize(localizableNO_JAVACOMPILER_ERROR());
}
}

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2005, 2014, 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
@ -26,6 +26,4 @@
#
# Generic Messages
#
javacompiler.classpath.error={0} is not available in the classpath, requires Sun's JDK version 5.0 or latter.
javacompiler.nosuchmethod.error=There is no such method {0} available, requires Sun's JDK version 5.0 or latter.
javacompiler.error=error : {0}.
no.javacompiler.error=No system compiler found, check your jdk.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, 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
@ -28,14 +28,13 @@ package com.sun.tools.internal.ws.wscompile;
import com.sun.istack.internal.tools.ParallelWorldClassLoader;
import com.sun.tools.internal.ws.resources.JavacompilerMessages;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URISyntaxException;
import java.net.URL;
/**
* A helper class to invoke javac.
@ -61,34 +60,17 @@ class JavaCompilerHelper{
}
static boolean compile(String[] args, OutputStream out, ErrorReceiver receiver){
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
/* try to use the new compiler */
Class comSunToolsJavacMainClass =
cl.loadClass("com.sun.tools.javac.Main");
try {
Method compileMethod =
comSunToolsJavacMainClass.getMethod(
"compile",
compileMethodSignature);
Object result =
compileMethod.invoke(
null, args, new PrintWriter(out));
return result instanceof Integer && (Integer) result == 0;
} catch (NoSuchMethodException e2) {
receiver.error(JavacompilerMessages.JAVACOMPILER_NOSUCHMETHOD_ERROR("getMethod(\"compile\", Class[])"), e2);
} catch (IllegalAccessException e) {
receiver.error(e);
} catch (InvocationTargetException e) {
receiver.error(e);
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
if (comp == null) {
receiver.error(JavacompilerMessages.NO_JAVACOMPILER_ERROR(), null);
return false;
}
} catch (ClassNotFoundException e) {
receiver.error(JavacompilerMessages.JAVACOMPILER_CLASSPATH_ERROR("com.sun.tools.javac.Main"), e);
return 0 == comp.run(null, out, out, args);
} catch (SecurityException e) {
receiver.error(e);
}
return false;
}
private static final Class[] compileMethodSignature = {String[].class, PrintWriter.class};
}