author | thartmann |
Sun, 21 Sep 2014 16:13:39 +0200 | |
changeset 26810 | ad491ed6cda8 |
parent 26658 | e04929038abf |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
12009 | 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.xml.internal.bind.util.Which; |
|
33 |
||
34 |
import javax.xml.ws.Service; |
|
35 |
import javax.xml.ws.WebServiceFeature; |
|
36 |
import javax.xml.namespace.QName; |
|
37 |
import java.io.OutputStream; |
|
38 |
import java.io.IOException; |
|
39 |
import java.lang.reflect.Constructor; |
|
40 |
import java.lang.reflect.InvocationTargetException; |
|
41 |
import java.lang.reflect.Method; |
|
42 |
import java.net.URL; |
|
43 |
import java.net.URLClassLoader; |
|
44 |
import java.util.ArrayList; |
|
45 |
import java.util.Arrays; |
|
46 |
import java.util.List; |
|
47 |
||
48 |
/** |
|
49 |
* Invokes JAX-WS tools in a special class loader that can pick up annotation processing classes, |
|
50 |
* even if it's not available in the tool launcher classpath. |
|
51 |
* |
|
52 |
* @author Kohsuke Kawaguchi |
|
53 |
*/ |
|
54 |
public final class Invoker { |
|
16791 | 55 |
|
56 |
/** |
|
57 |
* The list of package prefixes we want the |
|
58 |
* {@link MaskingClassLoader} to prevent the parent |
|
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
59 |
* class loader from loading |
16791 | 60 |
*/ |
61 |
static final String[] maskedPackages = new String[]{ |
|
62 |
"com.sun.istack.internal.tools.", |
|
63 |
"com.sun.tools.internal.jxc.", |
|
64 |
"com.sun.tools.internal.xjc.", |
|
65 |
"com.sun.tools.internal.ws.", |
|
66 |
"com.sun.codemodel.internal.", |
|
67 |
"com.sun.relaxng.", |
|
68 |
"com.sun.xml.internal.xsom.", |
|
69 |
"com.sun.xml.internal.bind.", |
|
70 |
"com.ctc.wstx.", //wsimport, wsgen ant task |
|
71 |
"org.codehaus.stax2.", //wsimport, wsgen ant task |
|
72 |
"com.sun.xml.internal.messaging.saaj.", //wsgen ant task |
|
73 |
"com.sun.xml.internal.ws.", |
|
74 |
"com.oracle.webservices.internal.api." //wsgen |
|
75 |
}; |
|
76 |
||
77 |
/** |
|
78 |
* Escape hatch to work around IBM JDK problem. |
|
79 |
* See http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?nav=false&forum=367&thread=164718&cat=10 |
|
80 |
*/ |
|
81 |
public static final boolean noSystemProxies; |
|
82 |
||
83 |
static { |
|
84 |
boolean noSysProxiesProperty = false; |
|
85 |
try { |
|
86 |
noSysProxiesProperty = Boolean.getBoolean(Invoker.class.getName()+".noSystemProxies"); |
|
87 |
} catch(SecurityException e) { |
|
88 |
// ignore |
|
89 |
} finally { |
|
90 |
noSystemProxies = noSysProxiesProperty; |
|
91 |
} |
|
92 |
} |
|
93 |
||
12009 | 94 |
static int invoke(String mainClass, String[] args) throws Throwable { |
95 |
// use the platform default proxy if available. |
|
96 |
// see sun.net.spi.DefaultProxySelector for details. |
|
97 |
if(!noSystemProxies) { |
|
98 |
try { |
|
99 |
System.setProperty("java.net.useSystemProxies","true"); |
|
100 |
} catch (SecurityException e) { |
|
101 |
// failing to set this property isn't fatal |
|
102 |
} |
|
103 |
} |
|
104 |
||
105 |
ClassLoader oldcc = Thread.currentThread().getContextClassLoader(); |
|
106 |
try { |
|
107 |
ClassLoader cl = Invoker.class.getClassLoader(); |
|
108 |
if(Arrays.asList(args).contains("-Xendorsed")) |
|
109 |
cl = createClassLoader(cl); // perform JDK6 workaround hack |
|
110 |
else { |
|
111 |
int targetArgIndex = Arrays.asList(args).indexOf("-target"); |
|
112 |
Options.Target targetVersion; |
|
113 |
if (targetArgIndex != -1) { |
|
114 |
targetVersion = Options.Target.parse(args[targetArgIndex+1]); |
|
115 |
} else { |
|
116 |
targetVersion = Options.Target.getDefault(); |
|
117 |
} |
|
118 |
Options.Target loadedVersion = Options.Target.getLoadedAPIVersion(); |
|
119 |
||
120 |
//Check if the target version is supported by the loaded API version |
|
121 |
if (!loadedVersion.isLaterThan(targetVersion)) { |
|
122 |
if (Service.class.getClassLoader() == null) |
|
123 |
System.err.println(WscompileMessages.INVOKER_NEED_ENDORSED(loadedVersion.getVersion(), targetVersion.getVersion())); |
|
124 |
else { |
|
125 |
System.err.println(WscompileMessages.WRAPPER_TASK_LOADING_INCORRECT_API(loadedVersion.getVersion(), Which.which(Service.class), targetVersion.getVersion())); |
|
126 |
} |
|
127 |
return -1; |
|
128 |
} |
|
129 |
||
130 |
} |
|
131 |
||
132 |
Thread.currentThread().setContextClassLoader(cl); |
|
133 |
||
134 |
Class compileTool = cl.loadClass(mainClass); |
|
135 |
Constructor ctor = compileTool.getConstructor(OutputStream.class); |
|
136 |
Object tool = ctor.newInstance(System.out); |
|
137 |
Method runMethod = compileTool.getMethod("run",String[].class); |
|
138 |
boolean r = (Boolean)runMethod.invoke(tool,new Object[]{args}); |
|
139 |
return r ? 0 : 1; |
|
140 |
} catch (InvocationTargetException e) { |
|
141 |
throw e.getCause(); |
|
142 |
} catch(ClassNotFoundException e){ |
|
143 |
throw e; |
|
144 |
}finally { |
|
145 |
Thread.currentThread().setContextClassLoader(oldcc); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* Returns true if the RI appears to be loading the JAX-WS 2.1 API. |
|
151 |
*/ |
|
152 |
public static boolean checkIfLoading21API() { |
|
153 |
try { |
|
154 |
Service.class.getMethod("getPort",Class.class, WebServiceFeature[].class); |
|
155 |
// yup. things look good. |
|
156 |
return true; |
|
157 |
} catch (NoSuchMethodException e) { |
|
158 |
} catch (LinkageError e) { |
|
159 |
} |
|
160 |
// nope |
|
161 |
return false; |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Returns true if the RI appears to be loading the JAX-WS 2.2 API. |
|
166 |
*/ |
|
167 |
public static boolean checkIfLoading22API() { |
|
168 |
try { |
|
169 |
Service.class.getMethod("create",java.net.URL.class, QName.class, WebServiceFeature[].class); |
|
170 |
// yup. things look good. |
|
171 |
return true; |
|
172 |
} catch (NoSuchMethodException e) { |
|
173 |
} catch (LinkageError e) { |
|
174 |
} |
|
175 |
// nope |
|
176 |
return false; |
|
177 |
} |
|
178 |
||
179 |
||
180 |
/** |
|
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
181 |
* Creates a class loader that can load JAXB/WS 2.2 API, |
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
182 |
* and then return a class loader that can RI classes, which can see all those APIs. |
12009 | 183 |
*/ |
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
184 |
public static ClassLoader createClassLoader(ClassLoader cl) throws ClassNotFoundException, IOException { |
12009 | 185 |
|
186 |
URL[] urls = findIstack22APIs(cl); |
|
187 |
if(urls.length==0) |
|
188 |
return cl; // we seem to be able to load everything already. no need for the hack |
|
189 |
||
190 |
List<String> mask = new ArrayList<String>(Arrays.asList(maskedPackages)); |
|
191 |
if(urls.length>1) { |
|
192 |
// we need to load 2.1 API from side. so add them to the mask |
|
193 |
mask.add("javax.xml.bind."); |
|
194 |
mask.add("javax.xml.ws."); |
|
195 |
} |
|
196 |
||
197 |
// first create a protected area so that we load JAXB/WS 2.1 API |
|
198 |
// and everything that depends on them inside |
|
199 |
cl = new MaskingClassLoader(cl,mask); |
|
200 |
||
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
201 |
// then this class loader loads the API |
12009 | 202 |
cl = new URLClassLoader(urls, cl); |
203 |
||
204 |
// finally load the rest of the RI. The actual class files are loaded from ancestors |
|
205 |
cl = new ParallelWorldClassLoader(cl,""); |
|
206 |
||
207 |
return cl; |
|
208 |
} |
|
209 |
||
210 |
/** |
|
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
211 |
* Creates a class loader for loading JAXB/WS 2.2 jar |
12009 | 212 |
*/ |
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
213 |
private static URL[] findIstack22APIs(ClassLoader cl) throws ClassNotFoundException, IOException { |
12009 | 214 |
List<URL> urls = new ArrayList<URL>(); |
215 |
||
216 |
if(Service.class.getClassLoader()==null) { |
|
26658
e04929038abf
8054548: JAX-WS tools need to updated to work with modular image
mkos
parents:
25871
diff
changeset
|
217 |
// JAX-WS API is loaded from bootstrap class loader |
12009 | 218 |
URL res = cl.getResource("javax/xml/ws/EndpointContext.class"); |
219 |
if(res==null) |
|
220 |
throw new ClassNotFoundException("There's no JAX-WS 2.2 API in the classpath"); |
|
221 |
urls.add(ParallelWorldClassLoader.toJarUrl(res)); |
|
222 |
res = cl.getResource("javax/xml/bind/JAXBPermission.class"); |
|
223 |
if(res==null) |
|
224 |
throw new ClassNotFoundException("There's no JAXB 2.2 API in the classpath"); |
|
225 |
urls.add(ParallelWorldClassLoader.toJarUrl(res)); |
|
226 |
} |
|
227 |
||
228 |
return urls.toArray(new URL[urls.size()]); |
|
229 |
} |
|
230 |
||
231 |
} |