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

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

mercurial