src/share/jaxws_classes/com/sun/tools/internal/ws/Invoker.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.ws;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.tools.MaskingClassLoader;
aoqi@0 29 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
aoqi@0 30 import com.sun.tools.internal.ws.resources.WscompileMessages;
aoqi@0 31 import com.sun.tools.internal.ws.wscompile.Options;
aoqi@0 32 import com.sun.tools.internal.xjc.api.util.ToolsJarNotFoundException;
aoqi@0 33 import com.sun.xml.internal.bind.util.Which;
aoqi@0 34
aoqi@0 35 import javax.xml.ws.Service;
aoqi@0 36 import javax.xml.ws.WebServiceFeature;
aoqi@0 37 import javax.xml.namespace.QName;
aoqi@0 38 import java.io.File;
aoqi@0 39 import java.io.OutputStream;
aoqi@0 40 import java.io.IOException;
aoqi@0 41 import java.lang.reflect.Constructor;
aoqi@0 42 import java.lang.reflect.InvocationTargetException;
aoqi@0 43 import java.lang.reflect.Method;
aoqi@0 44 import java.net.MalformedURLException;
aoqi@0 45 import java.net.URL;
aoqi@0 46 import java.net.URLClassLoader;
aoqi@0 47 import java.util.ArrayList;
aoqi@0 48 import java.util.Arrays;
aoqi@0 49 import java.util.List;
aoqi@0 50
aoqi@0 51 /**
aoqi@0 52 * Invokes JAX-WS tools in a special class loader that can pick up annotation processing classes,
aoqi@0 53 * even if it's not available in the tool launcher classpath.
aoqi@0 54 *
aoqi@0 55 * @author Kohsuke Kawaguchi
aoqi@0 56 */
aoqi@0 57 public final class Invoker {
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * The list of package prefixes we want the
aoqi@0 61 * {@link MaskingClassLoader} to prevent the parent
aoqi@0 62 * classLoader from loading
aoqi@0 63 */
aoqi@0 64 static final String[] maskedPackages = new String[]{
aoqi@0 65 "com.sun.istack.internal.tools.",
aoqi@0 66 "com.sun.tools.internal.jxc.",
aoqi@0 67 "com.sun.tools.internal.xjc.",
aoqi@0 68 "com.sun.tools.internal.ws.",
aoqi@0 69 "com.sun.codemodel.internal.",
aoqi@0 70 "com.sun.relaxng.",
aoqi@0 71 "com.sun.xml.internal.xsom.",
aoqi@0 72 "com.sun.xml.internal.bind.",
aoqi@0 73 "com.ctc.wstx.", //wsimport, wsgen ant task
aoqi@0 74 "org.codehaus.stax2.", //wsimport, wsgen ant task
aoqi@0 75 "com.sun.xml.internal.messaging.saaj.", //wsgen ant task
aoqi@0 76 "com.sun.xml.internal.ws.",
aoqi@0 77 "com.oracle.webservices.internal.api." //wsgen
aoqi@0 78 };
aoqi@0 79
aoqi@0 80 /**
aoqi@0 81 * Escape hatch to work around IBM JDK problem.
aoqi@0 82 * See http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?nav=false&forum=367&thread=164718&cat=10
aoqi@0 83 */
aoqi@0 84 public static final boolean noSystemProxies;
aoqi@0 85
aoqi@0 86 static {
aoqi@0 87 boolean noSysProxiesProperty = false;
aoqi@0 88 try {
aoqi@0 89 noSysProxiesProperty = Boolean.getBoolean(Invoker.class.getName()+".noSystemProxies");
aoqi@0 90 } catch(SecurityException e) {
aoqi@0 91 // ignore
aoqi@0 92 } finally {
aoqi@0 93 noSystemProxies = noSysProxiesProperty;
aoqi@0 94 }
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 static int invoke(String mainClass, String[] args) throws Throwable {
aoqi@0 98 // use the platform default proxy if available.
aoqi@0 99 // see sun.net.spi.DefaultProxySelector for details.
aoqi@0 100 if(!noSystemProxies) {
aoqi@0 101 try {
aoqi@0 102 System.setProperty("java.net.useSystemProxies","true");
aoqi@0 103 } catch (SecurityException e) {
aoqi@0 104 // failing to set this property isn't fatal
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 ClassLoader oldcc = Thread.currentThread().getContextClassLoader();
aoqi@0 109 try {
aoqi@0 110 ClassLoader cl = Invoker.class.getClassLoader();
aoqi@0 111 if(Arrays.asList(args).contains("-Xendorsed"))
aoqi@0 112 cl = createClassLoader(cl); // perform JDK6 workaround hack
aoqi@0 113 else {
aoqi@0 114 int targetArgIndex = Arrays.asList(args).indexOf("-target");
aoqi@0 115 Options.Target targetVersion;
aoqi@0 116 if (targetArgIndex != -1) {
aoqi@0 117 targetVersion = Options.Target.parse(args[targetArgIndex+1]);
aoqi@0 118 } else {
aoqi@0 119 targetVersion = Options.Target.getDefault();
aoqi@0 120 }
aoqi@0 121 Options.Target loadedVersion = Options.Target.getLoadedAPIVersion();
aoqi@0 122
aoqi@0 123 //Check if the target version is supported by the loaded API version
aoqi@0 124 if (!loadedVersion.isLaterThan(targetVersion)) {
aoqi@0 125 if (Service.class.getClassLoader() == null)
aoqi@0 126 System.err.println(WscompileMessages.INVOKER_NEED_ENDORSED(loadedVersion.getVersion(), targetVersion.getVersion()));
aoqi@0 127 else {
aoqi@0 128 System.err.println(WscompileMessages.WRAPPER_TASK_LOADING_INCORRECT_API(loadedVersion.getVersion(), Which.which(Service.class), targetVersion.getVersion()));
aoqi@0 129 }
aoqi@0 130 return -1;
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 //find and load tools.jar
aoqi@0 134 List<URL> urls = new ArrayList<URL>();
aoqi@0 135 findToolsJar(cl, urls);
aoqi@0 136
aoqi@0 137 if(urls.size() > 0){
aoqi@0 138 List<String> mask = new ArrayList<String>(Arrays.asList(maskedPackages));
aoqi@0 139
aoqi@0 140 // first create a protected area so that we load JAXB/WS 2.1 API
aoqi@0 141 // and everything that depends on them inside
aoqi@0 142 cl = new MaskingClassLoader(cl,mask);
aoqi@0 143
aoqi@0 144 // then this classloader loads the API and tools.jar
aoqi@0 145 cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), cl);
aoqi@0 146
aoqi@0 147 // finally load the rest of the RI. The actual class files are loaded from ancestors
aoqi@0 148 cl = new ParallelWorldClassLoader(cl,"");
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 Thread.currentThread().setContextClassLoader(cl);
aoqi@0 154
aoqi@0 155 Class compileTool = cl.loadClass(mainClass);
aoqi@0 156 Constructor ctor = compileTool.getConstructor(OutputStream.class);
aoqi@0 157 Object tool = ctor.newInstance(System.out);
aoqi@0 158 Method runMethod = compileTool.getMethod("run",String[].class);
aoqi@0 159 boolean r = (Boolean)runMethod.invoke(tool,new Object[]{args});
aoqi@0 160 return r ? 0 : 1;
aoqi@0 161 } catch (ToolsJarNotFoundException e) {
aoqi@0 162 System.err.println(e.getMessage());
aoqi@0 163 } catch (InvocationTargetException e) {
aoqi@0 164 throw e.getCause();
aoqi@0 165 } catch(ClassNotFoundException e){
aoqi@0 166 throw e;
aoqi@0 167 }finally {
aoqi@0 168 Thread.currentThread().setContextClassLoader(oldcc);
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 return -1;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 /**
aoqi@0 175 * Returns true if the RI appears to be loading the JAX-WS 2.1 API.
aoqi@0 176 */
aoqi@0 177 public static boolean checkIfLoading21API() {
aoqi@0 178 try {
aoqi@0 179 Service.class.getMethod("getPort",Class.class, WebServiceFeature[].class);
aoqi@0 180 // yup. things look good.
aoqi@0 181 return true;
aoqi@0 182 } catch (NoSuchMethodException e) {
aoqi@0 183 } catch (LinkageError e) {
aoqi@0 184 }
aoqi@0 185 // nope
aoqi@0 186 return false;
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 /**
aoqi@0 190 * Returns true if the RI appears to be loading the JAX-WS 2.2 API.
aoqi@0 191 */
aoqi@0 192 public static boolean checkIfLoading22API() {
aoqi@0 193 try {
aoqi@0 194 Service.class.getMethod("create",java.net.URL.class, QName.class, WebServiceFeature[].class);
aoqi@0 195 // yup. things look good.
aoqi@0 196 return true;
aoqi@0 197 } catch (NoSuchMethodException e) {
aoqi@0 198 } catch (LinkageError e) {
aoqi@0 199 }
aoqi@0 200 // nope
aoqi@0 201 return false;
aoqi@0 202 }
aoqi@0 203
aoqi@0 204
aoqi@0 205 /**
aoqi@0 206 * Creates a classloader that can load JAXB/WS 2.2 API and tools.jar,
aoqi@0 207 * and then return a classloader that can RI classes, which can see all those APIs and tools.jar.
aoqi@0 208 */
aoqi@0 209 public static ClassLoader createClassLoader(ClassLoader cl) throws ClassNotFoundException, IOException, ToolsJarNotFoundException {
aoqi@0 210
aoqi@0 211 URL[] urls = findIstack22APIs(cl);
aoqi@0 212 if(urls.length==0)
aoqi@0 213 return cl; // we seem to be able to load everything already. no need for the hack
aoqi@0 214
aoqi@0 215 List<String> mask = new ArrayList<String>(Arrays.asList(maskedPackages));
aoqi@0 216 if(urls.length>1) {
aoqi@0 217 // we need to load 2.1 API from side. so add them to the mask
aoqi@0 218 mask.add("javax.xml.bind.");
aoqi@0 219 mask.add("javax.xml.ws.");
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 // first create a protected area so that we load JAXB/WS 2.1 API
aoqi@0 223 // and everything that depends on them inside
aoqi@0 224 cl = new MaskingClassLoader(cl,mask);
aoqi@0 225
aoqi@0 226 // then this classloader loads the API and tools.jar
aoqi@0 227 cl = new URLClassLoader(urls, cl);
aoqi@0 228
aoqi@0 229 // finally load the rest of the RI. The actual class files are loaded from ancestors
aoqi@0 230 cl = new ParallelWorldClassLoader(cl,"");
aoqi@0 231
aoqi@0 232 return cl;
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 /**
aoqi@0 236 * Creates a classloader for loading JAXB/WS 2.2 jar and tools.jar
aoqi@0 237 */
aoqi@0 238 private static URL[] findIstack22APIs(ClassLoader cl) throws ClassNotFoundException, IOException, ToolsJarNotFoundException {
aoqi@0 239 List<URL> urls = new ArrayList<URL>();
aoqi@0 240
aoqi@0 241 if(Service.class.getClassLoader()==null) {
aoqi@0 242 // JAX-WS API is loaded from bootstrap classloader
aoqi@0 243 URL res = cl.getResource("javax/xml/ws/EndpointContext.class");
aoqi@0 244 if(res==null)
aoqi@0 245 throw new ClassNotFoundException("There's no JAX-WS 2.2 API in the classpath");
aoqi@0 246 urls.add(ParallelWorldClassLoader.toJarUrl(res));
aoqi@0 247 res = cl.getResource("javax/xml/bind/JAXBPermission.class");
aoqi@0 248 if(res==null)
aoqi@0 249 throw new ClassNotFoundException("There's no JAXB 2.2 API in the classpath");
aoqi@0 250 urls.add(ParallelWorldClassLoader.toJarUrl(res));
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 findToolsJar(cl, urls);
aoqi@0 254
aoqi@0 255 return urls.toArray(new URL[urls.size()]);
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 private static void findToolsJar(ClassLoader cl, List<URL> urls) throws ToolsJarNotFoundException, MalformedURLException {
aoqi@0 259 try {
aoqi@0 260 Class.forName("com.sun.tools.javac.Main",false,cl);
aoqi@0 261 // we can already load them in the parent class loader.
aoqi@0 262 // so no need to look for tools.jar.
aoqi@0 263 // this happens when we are run inside IDE/Ant, or
aoqi@0 264 // in Mac OS.
aoqi@0 265 } catch (ClassNotFoundException e) {
aoqi@0 266 // otherwise try to find tools.jar
aoqi@0 267 File jreHome = new File(System.getProperty("java.home"));
aoqi@0 268 File toolsJar = new File( jreHome.getParent(), "lib/tools.jar" );
aoqi@0 269
aoqi@0 270 if (!toolsJar.exists()) {
aoqi@0 271 throw new ToolsJarNotFoundException(toolsJar);
aoqi@0 272 }
aoqi@0 273 urls.add(toolsJar.toURL());
aoqi@0 274 }
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 }

mercurial