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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial