author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 43503 | bc7f8619ab70 |
child 45663 | 4a0cbf8f2474 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
32639
339de1317e84
8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.management.remote; |
|
27 |
||
28 |
import com.sun.jmx.mbeanserver.Util; |
|
29 |
import java.io.IOException; |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
30 |
import java.io.UncheckedIOException; |
2 | 31 |
import java.net.MalformedURLException; |
32 |
import java.util.Collections; |
|
33 |
import java.util.HashMap; |
|
34 |
import java.util.Map; |
|
35 |
import java.util.ServiceLoader; |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
36 |
import java.util.ServiceLoader.Provider; |
2 | 37 |
import java.util.StringTokenizer; |
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
38 |
import java.util.function.Predicate; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
39 |
import java.util.stream.Stream; |
2 | 40 |
import java.security.AccessController; |
41 |
import java.security.PrivilegedAction; |
|
42 |
||
43 |
import com.sun.jmx.remote.util.ClassLogger; |
|
44 |
import com.sun.jmx.remote.util.EnvHelp; |
|
18190 | 45 |
import sun.reflect.misc.ReflectUtil; |
2 | 46 |
|
47 |
||
48 |
/** |
|
49 |
* <p>Factory to create JMX API connector clients. There |
|
50 |
* are no instances of this class.</p> |
|
51 |
* |
|
52 |
* <p>Connections are usually made using the {@link |
|
53 |
* #connect(JMXServiceURL) connect} method of this class. More |
|
54 |
* advanced applications can separate the creation of the connector |
|
55 |
* client, using {@link #newJMXConnector(JMXServiceURL, Map) |
|
56 |
* newJMXConnector} and the establishment of the connection itself, using |
|
57 |
* {@link JMXConnector#connect(Map)}.</p> |
|
58 |
* |
|
59 |
* <p>Each client is created by an instance of {@link |
|
60 |
* JMXConnectorProvider}. This instance is found as follows. Suppose |
|
61 |
* the given {@link JMXServiceURL} looks like |
|
62 |
* <code>"service:jmx:<em>protocol</em>:<em>remainder</em>"</code>. |
|
63 |
* Then the factory will attempt to find the appropriate {@link |
|
64 |
* JMXConnectorProvider} for <code><em>protocol</em></code>. Each |
|
65 |
* occurrence of the character <code>+</code> or <code>-</code> in |
|
66 |
* <code><em>protocol</em></code> is replaced by <code>.</code> or |
|
67 |
* <code>_</code>, respectively.</p> |
|
68 |
* |
|
69 |
* <p>A <em>provider package list</em> is searched for as follows:</p> |
|
70 |
* |
|
71 |
* <ol> |
|
72 |
* |
|
73 |
* <li>If the <code>environment</code> parameter to {@link |
|
74 |
* #newJMXConnector(JMXServiceURL, Map) newJMXConnector} contains the |
|
75 |
* key <code>jmx.remote.protocol.provider.pkgs</code> then the |
|
76 |
* associated value is the provider package list. |
|
77 |
* |
|
78 |
* <li>Otherwise, if the system property |
|
79 |
* <code>jmx.remote.protocol.provider.pkgs</code> exists, then its value |
|
80 |
* is the provider package list. |
|
81 |
* |
|
82 |
* <li>Otherwise, there is no provider package list. |
|
83 |
* |
|
84 |
* </ol> |
|
85 |
* |
|
86 |
* <p>The provider package list is a string that is interpreted as a |
|
87 |
* list of non-empty Java package names separated by vertical bars |
|
88 |
* (<code>|</code>). If the string is empty, then so is the provider |
|
89 |
* package list. If the provider package list is not a String, or if |
|
90 |
* it contains an element that is an empty string, a {@link |
|
91 |
* JMXProviderException} is thrown.</p> |
|
92 |
* |
|
93 |
* <p>If the provider package list exists and is not empty, then for |
|
94 |
* each element <code><em>pkg</em></code> of the list, the factory |
|
95 |
* will attempt to load the class |
|
96 |
* |
|
97 |
* <blockquote> |
|
98 |
* <code><em>pkg</em>.<em>protocol</em>.ClientProvider</code> |
|
99 |
* </blockquote> |
|
100 |
||
101 |
* <p>If the <code>environment</code> parameter to {@link |
|
102 |
* #newJMXConnector(JMXServiceURL, Map) newJMXConnector} contains the |
|
103 |
* key <code>jmx.remote.protocol.provider.class.loader</code> then the |
|
104 |
* associated value is the class loader to use to load the provider. |
|
105 |
* If the associated value is not an instance of {@link |
|
106 |
* java.lang.ClassLoader}, an {@link |
|
107 |
* java.lang.IllegalArgumentException} is thrown.</p> |
|
108 |
* |
|
109 |
* <p>If the <code>jmx.remote.protocol.provider.class.loader</code> |
|
110 |
* key is not present in the <code>environment</code> parameter, the |
|
111 |
* calling thread's context class loader is used.</p> |
|
112 |
* |
|
113 |
* <p>If the attempt to load this class produces a {@link |
|
114 |
* ClassNotFoundException}, the search for a handler continues with |
|
115 |
* the next element of the list.</p> |
|
116 |
* |
|
117 |
* <p>Otherwise, a problem with the provider found is signalled by a |
|
118 |
* {@link JMXProviderException} whose {@link |
|
119 |
* JMXProviderException#getCause() <em>cause</em>} indicates the underlying |
|
120 |
* exception, as follows:</p> |
|
121 |
* |
|
122 |
* <ul> |
|
123 |
* |
|
124 |
* <li>if the attempt to load the class produces an exception other |
|
125 |
* than <code>ClassNotFoundException</code>, that is the |
|
126 |
* <em>cause</em>; |
|
127 |
* |
|
128 |
* <li>if {@link Class#newInstance()} for the class produces an |
|
129 |
* exception, that is the <em>cause</em>. |
|
130 |
* |
|
131 |
* </ul> |
|
132 |
* |
|
133 |
* <p>If no provider is found by the above steps, including the |
|
134 |
* default case where there is no provider package list, then the |
|
135 |
* implementation will use its own provider for |
|
136 |
* <code><em>protocol</em></code>, or it will throw a |
|
137 |
* <code>MalformedURLException</code> if there is none. An |
|
138 |
* implementation may choose to find providers by other means. For |
|
139 |
* example, it may support the <a |
|
24368 | 140 |
* href="{@docRoot}/../technotes/guides/jar/jar.html#Service%20Provider"> |
2 | 141 |
* JAR conventions for service providers</a>, where the service |
142 |
* interface is <code>JMXConnectorProvider</code>.</p> |
|
143 |
* |
|
14917
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
144 |
* <p>Every implementation must support the RMI connector protocol with |
bf08557604f8
8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents:
5506
diff
changeset
|
145 |
* the default RMI transport, specified with string <code>rmi</code>. |
32639
339de1317e84
8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents:
25859
diff
changeset
|
146 |
* </p> |
2 | 147 |
* |
148 |
* <p>Once a provider is found, the result of the |
|
149 |
* <code>newJMXConnector</code> method is the result of calling {@link |
|
150 |
* JMXConnectorProvider#newJMXConnector(JMXServiceURL,Map) newJMXConnector} |
|
151 |
* on the provider.</p> |
|
152 |
* |
|
153 |
* <p>The <code>Map</code> parameter passed to the |
|
154 |
* <code>JMXConnectorProvider</code> is a new read-only |
|
155 |
* <code>Map</code> that contains all the entries that were in the |
|
156 |
* <code>environment</code> parameter to {@link |
|
157 |
* #newJMXConnector(JMXServiceURL,Map) |
|
158 |
* JMXConnectorFactory.newJMXConnector}, if there was one. |
|
159 |
* Additionally, if the |
|
160 |
* <code>jmx.remote.protocol.provider.class.loader</code> key is not |
|
161 |
* present in the <code>environment</code> parameter, it is added to |
|
162 |
* the new read-only <code>Map</code>. The associated value is the |
|
163 |
* calling thread's context class loader.</p> |
|
164 |
* |
|
165 |
* @since 1.5 |
|
166 |
*/ |
|
167 |
public class JMXConnectorFactory { |
|
168 |
||
169 |
/** |
|
170 |
* <p>Name of the attribute that specifies the default class |
|
171 |
* loader. This class loader is used to deserialize return values and |
|
172 |
* exceptions from remote <code>MBeanServerConnection</code> |
|
173 |
* calls. The value associated with this attribute is an instance |
|
174 |
* of {@link ClassLoader}.</p> |
|
175 |
*/ |
|
176 |
public static final String DEFAULT_CLASS_LOADER = |
|
177 |
"jmx.remote.default.class.loader"; |
|
178 |
||
179 |
/** |
|
180 |
* <p>Name of the attribute that specifies the provider packages |
|
181 |
* that are consulted when looking for the handler for a protocol. |
|
182 |
* The value associated with this attribute is a string with |
|
183 |
* package names separated by vertical bars (<code>|</code>).</p> |
|
184 |
*/ |
|
185 |
public static final String PROTOCOL_PROVIDER_PACKAGES = |
|
186 |
"jmx.remote.protocol.provider.pkgs"; |
|
187 |
||
188 |
/** |
|
189 |
* <p>Name of the attribute that specifies the class |
|
190 |
* loader for loading protocol providers. |
|
191 |
* The value associated with this attribute is an instance |
|
192 |
* of {@link ClassLoader}.</p> |
|
193 |
*/ |
|
194 |
public static final String PROTOCOL_PROVIDER_CLASS_LOADER = |
|
195 |
"jmx.remote.protocol.provider.class.loader"; |
|
196 |
||
197 |
private static final String PROTOCOL_PROVIDER_DEFAULT_PACKAGE = |
|
198 |
"com.sun.jmx.remote.protocol"; |
|
199 |
||
200 |
private static final ClassLogger logger = |
|
201 |
new ClassLogger("javax.management.remote.misc", "JMXConnectorFactory"); |
|
202 |
||
203 |
/** There are no instances of this class. */ |
|
204 |
private JMXConnectorFactory() { |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* <p>Creates a connection to the connector server at the given |
|
209 |
* address.</p> |
|
210 |
* |
|
211 |
* <p>This method is equivalent to {@link |
|
212 |
* #connect(JMXServiceURL,Map) connect(serviceURL, null)}.</p> |
|
213 |
* |
|
214 |
* @param serviceURL the address of the connector server to |
|
215 |
* connect to. |
|
216 |
* |
|
217 |
* @return a <code>JMXConnector</code> whose {@link |
|
218 |
* JMXConnector#connect connect} method has been called. |
|
219 |
* |
|
220 |
* @exception NullPointerException if <code>serviceURL</code> is null. |
|
221 |
* |
|
222 |
* @exception IOException if the connector client or the |
|
223 |
* connection cannot be made because of a communication problem. |
|
224 |
* |
|
225 |
* @exception SecurityException if the connection cannot be made |
|
226 |
* for security reasons. |
|
227 |
*/ |
|
228 |
public static JMXConnector connect(JMXServiceURL serviceURL) |
|
229 |
throws IOException { |
|
230 |
return connect(serviceURL, null); |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* <p>Creates a connection to the connector server at the given |
|
235 |
* address.</p> |
|
236 |
* |
|
237 |
* <p>This method is equivalent to:</p> |
|
238 |
* |
|
239 |
* <pre> |
|
240 |
* JMXConnector conn = JMXConnectorFactory.newJMXConnector(serviceURL, |
|
241 |
* environment); |
|
242 |
* conn.connect(environment); |
|
243 |
* </pre> |
|
244 |
* |
|
245 |
* @param serviceURL the address of the connector server to connect to. |
|
246 |
* |
|
247 |
* @param environment a set of attributes to determine how the |
|
248 |
* connection is made. This parameter can be null. Keys in this |
|
249 |
* map must be Strings. The appropriate type of each associated |
|
250 |
* value depends on the attribute. The contents of |
|
251 |
* <code>environment</code> are not changed by this call. |
|
252 |
* |
|
253 |
* @return a <code>JMXConnector</code> representing the newly-made |
|
254 |
* connection. Each successful call to this method produces a |
|
255 |
* different object. |
|
256 |
* |
|
257 |
* @exception NullPointerException if <code>serviceURL</code> is null. |
|
258 |
* |
|
259 |
* @exception IOException if the connector client or the |
|
260 |
* connection cannot be made because of a communication problem. |
|
261 |
* |
|
262 |
* @exception SecurityException if the connection cannot be made |
|
263 |
* for security reasons. |
|
264 |
*/ |
|
265 |
public static JMXConnector connect(JMXServiceURL serviceURL, |
|
266 |
Map<String,?> environment) |
|
267 |
throws IOException { |
|
268 |
if (serviceURL == null) |
|
269 |
throw new NullPointerException("Null JMXServiceURL"); |
|
270 |
JMXConnector conn = newJMXConnector(serviceURL, environment); |
|
271 |
conn.connect(environment); |
|
272 |
return conn; |
|
273 |
} |
|
274 |
||
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
275 |
private static <K,V> Map<K,V> newHashMap() { |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
276 |
return new HashMap<K,V>(); |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
277 |
} |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
278 |
|
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
279 |
private static <K> Map<K,Object> newHashMap(Map<K,?> map) { |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
280 |
return new HashMap<K,Object>(map); |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
281 |
} |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
282 |
|
2 | 283 |
/** |
284 |
* <p>Creates a connector client for the connector server at the |
|
285 |
* given address. The resultant client is not connected until its |
|
286 |
* {@link JMXConnector#connect(Map) connect} method is called.</p> |
|
287 |
* |
|
288 |
* @param serviceURL the address of the connector server to connect to. |
|
289 |
* |
|
290 |
* @param environment a set of attributes to determine how the |
|
291 |
* connection is made. This parameter can be null. Keys in this |
|
292 |
* map must be Strings. The appropriate type of each associated |
|
293 |
* value depends on the attribute. The contents of |
|
294 |
* <code>environment</code> are not changed by this call. |
|
295 |
* |
|
296 |
* @return a <code>JMXConnector</code> representing the new |
|
297 |
* connector client. Each successful call to this method produces |
|
298 |
* a different object. |
|
299 |
* |
|
300 |
* @exception NullPointerException if <code>serviceURL</code> is null. |
|
301 |
* |
|
302 |
* @exception IOException if the connector client cannot be made |
|
303 |
* because of a communication problem. |
|
304 |
* |
|
305 |
* @exception MalformedURLException if there is no provider for the |
|
306 |
* protocol in <code>serviceURL</code>. |
|
307 |
* |
|
308 |
* @exception JMXProviderException if there is a provider for the |
|
309 |
* protocol in <code>serviceURL</code> but it cannot be used for |
|
310 |
* some reason. |
|
311 |
*/ |
|
312 |
public static JMXConnector newJMXConnector(JMXServiceURL serviceURL, |
|
313 |
Map<String,?> environment) |
|
314 |
throws IOException { |
|
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
315 |
|
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
316 |
final Map<String,Object> envcopy; |
2 | 317 |
if (environment == null) |
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
318 |
envcopy = newHashMap(); |
2 | 319 |
else { |
320 |
EnvHelp.checkAttributes(environment); |
|
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
321 |
envcopy = newHashMap(environment); |
2 | 322 |
} |
323 |
||
324 |
final ClassLoader loader = resolveClassLoader(envcopy); |
|
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
325 |
final Class<JMXConnectorProvider> targetInterface = |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
326 |
JMXConnectorProvider.class; |
2 | 327 |
final String protocol = serviceURL.getProtocol(); |
328 |
final String providerClassName = "ClientProvider"; |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1510
diff
changeset
|
329 |
final JMXServiceURL providerURL = serviceURL; |
2 | 330 |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1510
diff
changeset
|
331 |
JMXConnectorProvider provider = getProvider(providerURL, envcopy, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1510
diff
changeset
|
332 |
providerClassName, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1510
diff
changeset
|
333 |
targetInterface, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1510
diff
changeset
|
334 |
loader); |
2 | 335 |
|
336 |
IOException exception = null; |
|
337 |
if (provider == null) { |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
338 |
Predicate<Provider<?>> systemProvider = |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
339 |
JMXConnectorFactory::isSystemProvider; |
2 | 340 |
// Loader is null when context class loader is set to null |
341 |
// and no loader has been provided in map. |
|
342 |
// com.sun.jmx.remote.util.Service class extracted from j2se |
|
343 |
// provider search algorithm doesn't handle well null classloader. |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
344 |
JMXConnector connection = null; |
2 | 345 |
if (loader != null) { |
346 |
try { |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
347 |
connection = getConnectorAsService(loader, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
348 |
providerURL, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
349 |
envcopy, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
350 |
systemProvider.negate()); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
351 |
if (connection != null) return connection; |
2 | 352 |
} catch (JMXProviderException e) { |
353 |
throw e; |
|
354 |
} catch (IOException e) { |
|
355 |
exception = e; |
|
356 |
} |
|
357 |
} |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
358 |
connection = getConnectorAsService( |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
359 |
JMXConnectorFactory.class.getClassLoader(), |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
360 |
providerURL, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
361 |
Collections.unmodifiableMap(envcopy), |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
362 |
systemProvider); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
363 |
if (connection != null) return connection; |
2 | 364 |
} |
365 |
||
366 |
if (provider == null) { |
|
367 |
MalformedURLException e = |
|
368 |
new MalformedURLException("Unsupported protocol: " + protocol); |
|
369 |
if (exception == null) { |
|
370 |
throw e; |
|
371 |
} else { |
|
372 |
throw EnvHelp.initCause(e, exception); |
|
373 |
} |
|
374 |
} |
|
375 |
||
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
376 |
final Map<String,Object> fixedenv = |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
377 |
Collections.unmodifiableMap(envcopy); |
2 | 378 |
|
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
379 |
return provider.newJMXConnector(serviceURL, fixedenv); |
2 | 380 |
} |
381 |
||
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1156
diff
changeset
|
382 |
private static String resolvePkgs(Map<String, ?> env) |
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1156
diff
changeset
|
383 |
throws JMXProviderException { |
2 | 384 |
|
385 |
Object pkgsObject = null; |
|
386 |
||
387 |
if (env != null) |
|
388 |
pkgsObject = env.get(PROTOCOL_PROVIDER_PACKAGES); |
|
389 |
||
390 |
if (pkgsObject == null) |
|
391 |
pkgsObject = |
|
1156
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
392 |
AccessController.doPrivileged(new PrivilegedAction<String>() { |
bbc2d15aaf7a
5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
715
diff
changeset
|
393 |
public String run() { |
2 | 394 |
return System.getProperty(PROTOCOL_PROVIDER_PACKAGES); |
395 |
} |
|
396 |
}); |
|
397 |
||
398 |
if (pkgsObject == null) |
|
399 |
return null; |
|
400 |
||
401 |
if (!(pkgsObject instanceof String)) { |
|
402 |
final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES + |
|
403 |
" parameter is not a String: " + |
|
404 |
pkgsObject.getClass().getName(); |
|
405 |
throw new JMXProviderException(msg); |
|
406 |
} |
|
407 |
||
408 |
final String pkgs = (String) pkgsObject; |
|
409 |
if (pkgs.trim().equals("")) |
|
410 |
return null; |
|
411 |
||
412 |
// pkgs may not contain an empty element |
|
413 |
if (pkgs.startsWith("|") || pkgs.endsWith("|") || |
|
414 |
pkgs.indexOf("||") >= 0) { |
|
415 |
final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES + |
|
416 |
" contains an empty element: " + pkgs; |
|
417 |
throw new JMXProviderException(msg); |
|
418 |
} |
|
419 |
||
420 |
return pkgs; |
|
421 |
} |
|
422 |
||
423 |
static <T> T getProvider(JMXServiceURL serviceURL, |
|
18190 | 424 |
final Map<String, Object> environment, |
2 | 425 |
String providerClassName, |
426 |
Class<T> targetInterface, |
|
18190 | 427 |
final ClassLoader loader) |
2 | 428 |
throws IOException { |
429 |
||
430 |
final String protocol = serviceURL.getProtocol(); |
|
431 |
||
432 |
final String pkgs = resolvePkgs(environment); |
|
433 |
||
434 |
T instance = null; |
|
435 |
||
436 |
if (pkgs != null) { |
|
437 |
instance = |
|
438 |
getProvider(protocol, pkgs, loader, providerClassName, |
|
439 |
targetInterface); |
|
18190 | 440 |
|
441 |
if (instance != null) { |
|
442 |
boolean needsWrap = (loader != instance.getClass().getClassLoader()); |
|
443 |
environment.put(PROTOCOL_PROVIDER_CLASS_LOADER, needsWrap ? wrap(loader) : loader); |
|
444 |
} |
|
2 | 445 |
} |
446 |
||
447 |
return instance; |
|
448 |
} |
|
449 |
||
18190 | 450 |
private static ClassLoader wrap(final ClassLoader parent) { |
451 |
return parent != null ? AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { |
|
452 |
@Override |
|
453 |
public ClassLoader run() { |
|
454 |
return new ClassLoader(parent) { |
|
455 |
@Override |
|
456 |
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
|
457 |
ReflectUtil.checkPackageAccess(name); |
|
458 |
return super.loadClass(name, resolve); |
|
459 |
} |
|
460 |
}; |
|
461 |
} |
|
462 |
}) : null; |
|
463 |
} |
|
464 |
||
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
465 |
/** |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
466 |
* Checks whether the given provider is our system provider for |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
467 |
* the RMI connector. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
468 |
* If providers for additional protocols are added in the future |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
469 |
* then the name of their modules may need to be added here. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
470 |
* System providers will be loaded only if no other provider is found. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
471 |
* @param provider the provider to test. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
472 |
* @return true if this provider is a default system provider. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
473 |
*/ |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
474 |
static boolean isSystemProvider(Provider<?> provider) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
475 |
Module providerModule = provider.type().getModule(); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
476 |
return providerModule.isNamed() |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
477 |
&& providerModule.getName().equals("java.management.rmi"); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
478 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
479 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
480 |
/** |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
481 |
* Creates a JMXConnector from the first JMXConnectorProvider service |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
482 |
* supporting the given url that can be loaded from the given loader. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
483 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
484 |
* Parses the list of JMXConnectorProvider services that can be loaded |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
485 |
* from the given loader, only retaining those that satisfy the given filter. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
486 |
* Then for each provider, attempts to create a new JMXConnector. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
487 |
* The first JMXConnector successfully created is returned. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
488 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
489 |
* The filter predicate is usually used to either exclude system providers |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
490 |
* or only retain system providers (see isSystemProvider(...) above). |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
491 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
492 |
* @param loader The ClassLoader to use when looking up an implementation |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
493 |
* of the service. If null, then only installed services will be |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
494 |
* considered. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
495 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
496 |
* @param url The JMXServiceURL of the connector for which a provider is |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
497 |
* requested. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
498 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
499 |
* @param filter A filter used to exclude or return provider |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
500 |
* implementations. Typically the filter will either exclude |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
501 |
* system services (system default implementations) or only |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
502 |
* retain those. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
503 |
* This can allow to first look for custom implementations (e.g. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
504 |
* deployed on the CLASSPATH with META-INF/services) and |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
505 |
* then only default to system implementations. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
506 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
507 |
* @throws IOException if no connector could not be instantiated, and |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
508 |
* at least one provider threw an exception that wasn't a |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
509 |
* {@code MalformedURLException} or a {@code JMProviderException}. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
510 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
511 |
* @throws JMXProviderException if a provider for the protocol in |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
512 |
* <code>url</code> was found, but couldn't create the connector |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
513 |
* some reason. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
514 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
515 |
* @return an instance of JMXConnector if a provider was found from |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
516 |
* which one could be instantiated, {@code null} otherwise. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
517 |
*/ |
2 | 518 |
private static JMXConnector getConnectorAsService(ClassLoader loader, |
519 |
JMXServiceURL url, |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
520 |
Map<String, ?> map, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
521 |
Predicate<Provider<?>> filter) |
2 | 522 |
throws IOException { |
523 |
||
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
524 |
final ConnectorFactory<JMXConnectorProvider, JMXConnector> factory = |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
525 |
(p) -> p.newJMXConnector(url, map); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
526 |
return getConnectorAsService(JMXConnectorProvider.class, loader, url, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
527 |
filter, factory); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
528 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
529 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
530 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
531 |
/** |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
532 |
* A factory function that can create a connector from a provider. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
533 |
* The pair (P,C) will be either one of: |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
534 |
* a. (JMXConnectorProvider, JMXConnector) or |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
535 |
* b. (JMXConnectorServerProvider, JMXConnectorServer) |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
536 |
*/ |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
537 |
@FunctionalInterface |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
538 |
static interface ConnectorFactory<P,C> { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
539 |
public C apply(P provider) throws Exception; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
540 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
541 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
542 |
/** |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
543 |
* An instance of ProviderFinder is used to traverse a |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
544 |
* {@code Stream<Provider<P>>} and find the first implementation of P |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
545 |
* that supports creating a connector C from the given JMXServiceURL. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
546 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
547 |
* The pair (P,C) will be either one of: <br> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
548 |
* a. (JMXConnectorProvider, JMXConnector) or <br> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
549 |
* b. (JMXConnectorServerProvider, JMXConnectorServer) |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
550 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
551 |
* The first connector successfully created while traversing the stream |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
552 |
* is stored in the ProviderFinder instance. After that, the |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
553 |
* ProviderFinder::test method, if called, will always return false, skipping |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
554 |
* the remaining providers. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
555 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
556 |
* An instance of ProviderFinder is always expected to be used in conjunction |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
557 |
* with Stream::findFirst, so that the stream traversal is stopped as soon |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
558 |
* as a matching provider is found. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
559 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
560 |
* At the end of the stream traversal, the ProviderFinder::get method can be |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
561 |
* used to obtain the connector instance (an instance of C) that was created. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
562 |
* If no connector could be created, and an exception was encountered while |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
563 |
* traversing the stream and attempting to create the connector, then that |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
564 |
* exception will be thrown by ProviderFinder::get, wrapped, if needed, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
565 |
* inside an IOException. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
566 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
567 |
* If any JMXProviderException is encountered while traversing the stream and |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
568 |
* attempting to create the connector, that exception will be wrapped in an |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
569 |
* UncheckedIOException and thrown immediately within the stream, thus |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
570 |
* interrupting the traversal. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
571 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
572 |
* If no matching provider was found (no provider found or attempting |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
573 |
* factory.apply always returned null or threw a MalformedURLException, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
574 |
* indicating the provider didn't support the protocol asked for by |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
575 |
* the JMXServiceURL), then ProviderFinder::get will simply return null. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
576 |
*/ |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
577 |
private static final class ProviderFinder<P,C> implements Predicate<Provider<P>> { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
578 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
579 |
final ConnectorFactory<P,C> factory; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
580 |
final JMXServiceURL url; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
581 |
private IOException exception = null; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
582 |
private C connection = null; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
583 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
584 |
ProviderFinder(ConnectorFactory<P,C> factory, JMXServiceURL url) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
585 |
this.factory = factory; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
586 |
this.url = url; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
587 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
588 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
589 |
/** |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
590 |
* Returns {@code true} for the first provider {@code sp} that can |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
591 |
* be used to obtain an instance of {@code C} from the given |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
592 |
* {@code factory}. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
593 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
594 |
* @param sp a candidate provider for instantiating {@code C}. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
595 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
596 |
* @throws UncheckedIOException if {@code sp} throws a |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
597 |
* JMXProviderException. The JMXProviderException is set as the |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
598 |
* root cause. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
599 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
600 |
* @return {@code true} for the first provider {@code sp} for which |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
601 |
* {@code C} could be instantiated, {@code false} otherwise. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
602 |
*/ |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
603 |
public boolean test(Provider<P> sp) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
604 |
if (connection == null) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
605 |
P provider = sp.get(); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
606 |
try { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
607 |
connection = factory.apply(provider); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
608 |
return connection != null; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
609 |
} catch (JMXProviderException e) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
610 |
throw new UncheckedIOException(e); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
611 |
} catch (Exception e) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
612 |
if (logger.traceOn()) |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
613 |
logger.trace("getConnectorAsService", |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
614 |
"URL[" + url + |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
615 |
"] Service provider exception: " + e); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
616 |
if (!(e instanceof MalformedURLException)) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
617 |
if (exception == null) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
618 |
if (e instanceof IOException) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
619 |
exception = (IOException) e; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
620 |
} else { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
621 |
exception = EnvHelp.initCause( |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
622 |
new IOException(e.getMessage()), e); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
623 |
} |
2 | 624 |
} |
625 |
} |
|
626 |
} |
|
43503
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
627 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
628 |
return false; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
629 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
630 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
631 |
/** |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
632 |
* Returns an instance of {@code C} if a provider was found from |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
633 |
* which {@code C} could be instantiated. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
634 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
635 |
* @throws IOException if {@code C} could not be instantiated, and |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
636 |
* at least one provider threw an exception that wasn't a |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
637 |
* {@code MalformedURLException} or a {@code JMProviderException}. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
638 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
639 |
* @return an instance of {@code C} if a provider was found from |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
640 |
* which {@code C} could be instantiated, {@code null} otherwise. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
641 |
*/ |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
642 |
C get() throws IOException { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
643 |
if (connection != null) return connection; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
644 |
else if (exception != null) throw exception; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
645 |
else return null; |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
646 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
647 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
648 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
649 |
/** |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
650 |
* Creates a connector from a provider loaded from the ServiceLoader. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
651 |
* <p> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
652 |
* The pair (P,C) will be either one of: <br> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
653 |
* a. (JMXConnectorProvider, JMXConnector) or <br> |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
654 |
* b. (JMXConnectorServerProvider, JMXConnectorServer) |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
655 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
656 |
* @param providerClass The service type for which an implementation |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
657 |
* should be looked up from the {@code ServiceLoader}. This will |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
658 |
* be either {@code JMXConnectorProvider.class} or |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
659 |
* {@code JMXConnectorServerProvider.class} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
660 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
661 |
* @param loader The ClassLoader to use when looking up an implementation |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
662 |
* of the service. If null, then only installed services will be |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
663 |
* considered. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
664 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
665 |
* @param url The JMXServiceURL of the connector for which a provider is |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
666 |
* requested. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
667 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
668 |
* @param filter A filter used to exclude or return provider |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
669 |
* implementations. Typically the filter will either exclude |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
670 |
* system services (system default implementations) or only |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
671 |
* retain those. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
672 |
* This can allow to first look for custom implementations (e.g. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
673 |
* deployed on the CLASSPATH with META-INF/services) and |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
674 |
* then only default to system implementations. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
675 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
676 |
* @param factory A functional factory that can attempt to create an |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
677 |
* instance of connector {@code C} from a provider {@code P}. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
678 |
* Typically, this is a simple wrapper over {@code |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
679 |
* JMXConnectorProvider::newJMXConnector} or {@code |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
680 |
* JMXConnectorProviderServer::newJMXConnectorServer}. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
681 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
682 |
* @throws IOException if {@code C} could not be instantiated, and |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
683 |
* at least one provider {@code P} threw an exception that wasn't a |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
684 |
* {@code MalformedURLException} or a {@code JMProviderException}. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
685 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
686 |
* @throws JMXProviderException if a provider {@code P} for the protocol in |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
687 |
* <code>url</code> was found, but couldn't create the connector |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
688 |
* {@code C} for some reason. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
689 |
* |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
690 |
* @return an instance of {@code C} if a provider {@code P} was found from |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
691 |
* which one could be instantiated, {@code null} otherwise. |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
692 |
*/ |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
693 |
static <P,C> C getConnectorAsService(Class<P> providerClass, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
694 |
ClassLoader loader, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
695 |
JMXServiceURL url, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
696 |
Predicate<Provider<?>> filter, |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
697 |
ConnectorFactory<P,C> factory) |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
698 |
throws IOException { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
699 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
700 |
// sanity check |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
701 |
if (JMXConnectorProvider.class != providerClass |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
702 |
&& JMXConnectorServerProvider.class != providerClass) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
703 |
// should never happen |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
704 |
throw new InternalError("Unsupported service interface: " |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
705 |
+ providerClass.getName()); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
706 |
} |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
707 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
708 |
ServiceLoader<P> serviceLoader = loader == null |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
709 |
? ServiceLoader.loadInstalled(providerClass) |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
710 |
: ServiceLoader.load(providerClass, loader); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
711 |
Stream<Provider<P>> stream = serviceLoader.stream().filter(filter); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
712 |
ProviderFinder<P,C> finder = new ProviderFinder<>(factory, url); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
713 |
|
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
714 |
try { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
715 |
stream.filter(finder).findFirst(); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
716 |
return finder.get(); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
717 |
} catch (UncheckedIOException e) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
718 |
if (e.getCause() instanceof JMXProviderException) { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
719 |
throw (JMXProviderException) e.getCause(); |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
720 |
} else { |
bc7f8619ab70
8173607: JMX RMI connector should be in its own module
dfuchs
parents:
37782
diff
changeset
|
721 |
throw e; |
2 | 722 |
} |
723 |
} |
|
724 |
} |
|
725 |
||
726 |
static <T> T getProvider(String protocol, |
|
727 |
String pkgs, |
|
728 |
ClassLoader loader, |
|
729 |
String providerClassName, |
|
730 |
Class<T> targetInterface) |
|
731 |
throws IOException { |
|
732 |
||
733 |
StringTokenizer tokenizer = new StringTokenizer(pkgs, "|"); |
|
734 |
||
735 |
while (tokenizer.hasMoreTokens()) { |
|
736 |
String pkg = tokenizer.nextToken(); |
|
737 |
String className = (pkg + "." + protocol2package(protocol) + |
|
738 |
"." + providerClassName); |
|
739 |
Class<?> providerClass; |
|
740 |
try { |
|
741 |
providerClass = Class.forName(className, true, loader); |
|
742 |
} catch (ClassNotFoundException e) { |
|
743 |
//Add trace. |
|
744 |
continue; |
|
745 |
} |
|
746 |
||
747 |
if (!targetInterface.isAssignableFrom(providerClass)) { |
|
748 |
final String msg = |
|
749 |
"Provider class does not implement " + |
|
750 |
targetInterface.getName() + ": " + |
|
751 |
providerClass.getName(); |
|
752 |
throw new JMXProviderException(msg); |
|
753 |
} |
|
754 |
||
755 |
// We have just proved that this cast is correct |
|
756 |
Class<? extends T> providerClassT = Util.cast(providerClass); |
|
757 |
try { |
|
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
32639
diff
changeset
|
758 |
@SuppressWarnings("deprecation") |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
32639
diff
changeset
|
759 |
T result = providerClassT.newInstance(); |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
32639
diff
changeset
|
760 |
return result; |
2 | 761 |
} catch (Exception e) { |
762 |
final String msg = |
|
763 |
"Exception when instantiating provider [" + className + |
|
764 |
"]"; |
|
765 |
throw new JMXProviderException(msg, e); |
|
766 |
} |
|
767 |
} |
|
768 |
||
769 |
return null; |
|
770 |
} |
|
771 |
||
1510
e747d3193ef2
6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents:
1156
diff
changeset
|
772 |
static ClassLoader resolveClassLoader(Map<String, ?> environment) { |
2 | 773 |
ClassLoader loader = null; |
774 |
||
775 |
if (environment != null) { |
|
776 |
try { |
|
777 |
loader = (ClassLoader) |
|
778 |
environment.get(PROTOCOL_PROVIDER_CLASS_LOADER); |
|
779 |
} catch (ClassCastException e) { |
|
780 |
final String msg = |
|
781 |
"The ClassLoader supplied in the environment map using " + |
|
782 |
"the " + PROTOCOL_PROVIDER_CLASS_LOADER + |
|
783 |
" attribute is not an instance of java.lang.ClassLoader"; |
|
784 |
throw new IllegalArgumentException(msg); |
|
785 |
} |
|
786 |
} |
|
787 |
||
18187 | 788 |
if (loader == null) { |
789 |
loader = Thread.currentThread().getContextClassLoader(); |
|
790 |
} |
|
2 | 791 |
|
792 |
return loader; |
|
793 |
} |
|
794 |
||
795 |
private static String protocol2package(String protocol) { |
|
796 |
return protocol.replace('+', '.').replace('-', '_'); |
|
797 |
} |
|
798 |
} |