aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.ws; aoqi@0: aoqi@0: import com.sun.istack.internal.tools.MaskingClassLoader; aoqi@0: import com.sun.istack.internal.tools.ParallelWorldClassLoader; aoqi@0: import com.sun.tools.internal.ws.resources.WscompileMessages; aoqi@0: import com.sun.tools.internal.ws.wscompile.Options; aoqi@0: import com.sun.tools.internal.xjc.api.util.ToolsJarNotFoundException; aoqi@0: import com.sun.xml.internal.bind.util.Which; aoqi@0: aoqi@0: import javax.xml.ws.Service; aoqi@0: import javax.xml.ws.WebServiceFeature; aoqi@0: import javax.xml.namespace.QName; aoqi@0: import java.io.File; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.lang.reflect.Constructor; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.net.MalformedURLException; aoqi@0: import java.net.URL; aoqi@0: import java.net.URLClassLoader; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.List; aoqi@0: aoqi@0: /** aoqi@0: * Invokes JAX-WS tools in a special class loader that can pick up annotation processing classes, aoqi@0: * even if it's not available in the tool launcher classpath. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public final class Invoker { aoqi@0: aoqi@0: /** aoqi@0: * The list of package prefixes we want the aoqi@0: * {@link MaskingClassLoader} to prevent the parent aoqi@0: * classLoader from loading aoqi@0: */ aoqi@0: static final String[] maskedPackages = new String[]{ aoqi@0: "com.sun.istack.internal.tools.", aoqi@0: "com.sun.tools.internal.jxc.", aoqi@0: "com.sun.tools.internal.xjc.", aoqi@0: "com.sun.tools.internal.ws.", aoqi@0: "com.sun.codemodel.internal.", aoqi@0: "com.sun.relaxng.", aoqi@0: "com.sun.xml.internal.xsom.", aoqi@0: "com.sun.xml.internal.bind.", aoqi@0: "com.ctc.wstx.", //wsimport, wsgen ant task aoqi@0: "org.codehaus.stax2.", //wsimport, wsgen ant task aoqi@0: "com.sun.xml.internal.messaging.saaj.", //wsgen ant task aoqi@0: "com.sun.xml.internal.ws.", aoqi@0: "com.oracle.webservices.internal.api." //wsgen aoqi@0: }; aoqi@0: aoqi@0: /** aoqi@0: * Escape hatch to work around IBM JDK problem. aoqi@0: * See http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?nav=false&forum=367&thread=164718&cat=10 aoqi@0: */ aoqi@0: public static final boolean noSystemProxies; aoqi@0: aoqi@0: static { aoqi@0: boolean noSysProxiesProperty = false; aoqi@0: try { aoqi@0: noSysProxiesProperty = Boolean.getBoolean(Invoker.class.getName()+".noSystemProxies"); aoqi@0: } catch(SecurityException e) { aoqi@0: // ignore aoqi@0: } finally { aoqi@0: noSystemProxies = noSysProxiesProperty; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static int invoke(String mainClass, String[] args) throws Throwable { aoqi@0: // use the platform default proxy if available. aoqi@0: // see sun.net.spi.DefaultProxySelector for details. aoqi@0: if(!noSystemProxies) { aoqi@0: try { aoqi@0: System.setProperty("java.net.useSystemProxies","true"); aoqi@0: } catch (SecurityException e) { aoqi@0: // failing to set this property isn't fatal aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ClassLoader oldcc = Thread.currentThread().getContextClassLoader(); aoqi@0: try { aoqi@0: ClassLoader cl = Invoker.class.getClassLoader(); aoqi@0: if(Arrays.asList(args).contains("-Xendorsed")) aoqi@0: cl = createClassLoader(cl); // perform JDK6 workaround hack aoqi@0: else { aoqi@0: int targetArgIndex = Arrays.asList(args).indexOf("-target"); aoqi@0: Options.Target targetVersion; aoqi@0: if (targetArgIndex != -1) { aoqi@0: targetVersion = Options.Target.parse(args[targetArgIndex+1]); aoqi@0: } else { aoqi@0: targetVersion = Options.Target.getDefault(); aoqi@0: } aoqi@0: Options.Target loadedVersion = Options.Target.getLoadedAPIVersion(); aoqi@0: aoqi@0: //Check if the target version is supported by the loaded API version aoqi@0: if (!loadedVersion.isLaterThan(targetVersion)) { aoqi@0: if (Service.class.getClassLoader() == null) aoqi@0: System.err.println(WscompileMessages.INVOKER_NEED_ENDORSED(loadedVersion.getVersion(), targetVersion.getVersion())); aoqi@0: else { aoqi@0: System.err.println(WscompileMessages.WRAPPER_TASK_LOADING_INCORRECT_API(loadedVersion.getVersion(), Which.which(Service.class), targetVersion.getVersion())); aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: //find and load tools.jar aoqi@0: List urls = new ArrayList(); aoqi@0: findToolsJar(cl, urls); aoqi@0: aoqi@0: if(urls.size() > 0){ aoqi@0: List mask = new ArrayList(Arrays.asList(maskedPackages)); aoqi@0: aoqi@0: // first create a protected area so that we load JAXB/WS 2.1 API aoqi@0: // and everything that depends on them inside aoqi@0: cl = new MaskingClassLoader(cl,mask); aoqi@0: aoqi@0: // then this classloader loads the API and tools.jar aoqi@0: cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), cl); aoqi@0: aoqi@0: // finally load the rest of the RI. The actual class files are loaded from ancestors aoqi@0: cl = new ParallelWorldClassLoader(cl,""); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: Thread.currentThread().setContextClassLoader(cl); aoqi@0: aoqi@0: Class compileTool = cl.loadClass(mainClass); aoqi@0: Constructor ctor = compileTool.getConstructor(OutputStream.class); aoqi@0: Object tool = ctor.newInstance(System.out); aoqi@0: Method runMethod = compileTool.getMethod("run",String[].class); aoqi@0: boolean r = (Boolean)runMethod.invoke(tool,new Object[]{args}); aoqi@0: return r ? 0 : 1; aoqi@0: } catch (ToolsJarNotFoundException e) { aoqi@0: System.err.println(e.getMessage()); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: throw e.getCause(); aoqi@0: } catch(ClassNotFoundException e){ aoqi@0: throw e; aoqi@0: }finally { aoqi@0: Thread.currentThread().setContextClassLoader(oldcc); aoqi@0: } aoqi@0: aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns true if the RI appears to be loading the JAX-WS 2.1 API. aoqi@0: */ aoqi@0: public static boolean checkIfLoading21API() { aoqi@0: try { aoqi@0: Service.class.getMethod("getPort",Class.class, WebServiceFeature[].class); aoqi@0: // yup. things look good. aoqi@0: return true; aoqi@0: } catch (NoSuchMethodException e) { aoqi@0: } catch (LinkageError e) { aoqi@0: } aoqi@0: // nope aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns true if the RI appears to be loading the JAX-WS 2.2 API. aoqi@0: */ aoqi@0: public static boolean checkIfLoading22API() { aoqi@0: try { aoqi@0: Service.class.getMethod("create",java.net.URL.class, QName.class, WebServiceFeature[].class); aoqi@0: // yup. things look good. aoqi@0: return true; aoqi@0: } catch (NoSuchMethodException e) { aoqi@0: } catch (LinkageError e) { aoqi@0: } aoqi@0: // nope aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Creates a classloader that can load JAXB/WS 2.2 API and tools.jar, aoqi@0: * and then return a classloader that can RI classes, which can see all those APIs and tools.jar. aoqi@0: */ aoqi@0: public static ClassLoader createClassLoader(ClassLoader cl) throws ClassNotFoundException, IOException, ToolsJarNotFoundException { aoqi@0: aoqi@0: URL[] urls = findIstack22APIs(cl); aoqi@0: if(urls.length==0) aoqi@0: return cl; // we seem to be able to load everything already. no need for the hack aoqi@0: aoqi@0: List mask = new ArrayList(Arrays.asList(maskedPackages)); aoqi@0: if(urls.length>1) { aoqi@0: // we need to load 2.1 API from side. so add them to the mask aoqi@0: mask.add("javax.xml.bind."); aoqi@0: mask.add("javax.xml.ws."); aoqi@0: } aoqi@0: aoqi@0: // first create a protected area so that we load JAXB/WS 2.1 API aoqi@0: // and everything that depends on them inside aoqi@0: cl = new MaskingClassLoader(cl,mask); aoqi@0: aoqi@0: // then this classloader loads the API and tools.jar aoqi@0: cl = new URLClassLoader(urls, cl); aoqi@0: aoqi@0: // finally load the rest of the RI. The actual class files are loaded from ancestors aoqi@0: cl = new ParallelWorldClassLoader(cl,""); aoqi@0: aoqi@0: return cl; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a classloader for loading JAXB/WS 2.2 jar and tools.jar aoqi@0: */ aoqi@0: private static URL[] findIstack22APIs(ClassLoader cl) throws ClassNotFoundException, IOException, ToolsJarNotFoundException { aoqi@0: List urls = new ArrayList(); aoqi@0: aoqi@0: if(Service.class.getClassLoader()==null) { aoqi@0: // JAX-WS API is loaded from bootstrap classloader aoqi@0: URL res = cl.getResource("javax/xml/ws/EndpointContext.class"); aoqi@0: if(res==null) aoqi@0: throw new ClassNotFoundException("There's no JAX-WS 2.2 API in the classpath"); aoqi@0: urls.add(ParallelWorldClassLoader.toJarUrl(res)); aoqi@0: res = cl.getResource("javax/xml/bind/JAXBPermission.class"); aoqi@0: if(res==null) aoqi@0: throw new ClassNotFoundException("There's no JAXB 2.2 API in the classpath"); aoqi@0: urls.add(ParallelWorldClassLoader.toJarUrl(res)); aoqi@0: } aoqi@0: aoqi@0: findToolsJar(cl, urls); aoqi@0: aoqi@0: return urls.toArray(new URL[urls.size()]); aoqi@0: } aoqi@0: aoqi@0: private static void findToolsJar(ClassLoader cl, List urls) throws ToolsJarNotFoundException, MalformedURLException { aoqi@0: try { aoqi@0: Class.forName("com.sun.tools.javac.Main",false,cl); aoqi@0: // we can already load them in the parent class loader. aoqi@0: // so no need to look for tools.jar. aoqi@0: // this happens when we are run inside IDE/Ant, or aoqi@0: // in Mac OS. aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: // otherwise try to find tools.jar aoqi@0: File jreHome = new File(System.getProperty("java.home")); aoqi@0: File toolsJar = new File( jreHome.getParent(), "lib/tools.jar" ); aoqi@0: aoqi@0: if (!toolsJar.exists()) { aoqi@0: throw new ToolsJarNotFoundException(toolsJar); aoqi@0: } aoqi@0: urls.add(toolsJar.toURL()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }