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