author | sjiang |
Thu, 12 Jun 2014 10:32:19 +0200 | |
changeset 24871 | 224e298c3978 |
parent 18207 | 64dcc0ad2298 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16878 | 2 |
* Copyright (c) 2000, 2013, 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 com.sun.jmx.mbeanserver; |
|
27 |
||
28 |
||
29 |
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER; |
|
30 |
import java.io.ByteArrayInputStream; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.ObjectInputStream; |
|
33 |
import java.lang.reflect.Constructor; |
|
34 |
import java.lang.reflect.InvocationTargetException; |
|
16878 | 35 |
import java.lang.reflect.Modifier; |
18207 | 36 |
import java.security.AccessControlContext; |
37 |
import java.security.AccessController; |
|
16109 | 38 |
import java.security.Permission; |
18207 | 39 |
import java.security.Permissions; |
40 |
import java.security.PrivilegedAction; |
|
41 |
import java.security.ProtectionDomain; |
|
2 | 42 |
import java.util.Map; |
43 |
import java.util.logging.Level; |
|
44 |
||
45 |
import javax.management.InstanceNotFoundException; |
|
46 |
import javax.management.MBeanException; |
|
16109 | 47 |
import javax.management.MBeanPermission; |
2 | 48 |
import javax.management.NotCompliantMBeanException; |
49 |
import javax.management.ObjectName; |
|
50 |
import javax.management.OperationsException; |
|
51 |
import javax.management.ReflectionException; |
|
52 |
import javax.management.RuntimeErrorException; |
|
53 |
import javax.management.RuntimeMBeanException; |
|
54 |
import javax.management.RuntimeOperationsException; |
|
16109 | 55 |
import sun.reflect.misc.ConstructorUtil; |
2 | 56 |
import sun.reflect.misc.ReflectUtil; |
57 |
||
58 |
/** |
|
59 |
* Implements the MBeanInstantiator interface. Provides methods for |
|
60 |
* instantiating objects, finding the class given its name and using |
|
61 |
* different class loaders, deserializing objects in the context of a |
|
62 |
* given class loader. |
|
63 |
* |
|
64 |
* @since 1.5 |
|
65 |
*/ |
|
66 |
public class MBeanInstantiator { |
|
67 |
private final ModifiableClassLoaderRepository clr; |
|
68 |
// private MetaData meta = null; |
|
69 |
||
70 |
MBeanInstantiator(ModifiableClassLoaderRepository clr) { |
|
71 |
this.clr = clr; |
|
72 |
} |
|
73 |
||
74 |
||
75 |
/** |
|
76 |
* This methods tests if the MBean class makes it possible to |
|
77 |
* instantiate an MBean of this class in the MBeanServer. |
|
78 |
* e.g. it must have a public constructor, be a concrete class... |
|
79 |
*/ |
|
1510 | 80 |
public void testCreation(Class<?> c) throws NotCompliantMBeanException { |
2 | 81 |
Introspector.testCreation(c); |
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Loads the class with the specified name using this object's |
|
86 |
* Default Loader Repository. |
|
87 |
**/ |
|
1510 | 88 |
public Class<?> findClassWithDefaultLoaderRepository(String className) |
2 | 89 |
throws ReflectionException { |
90 |
||
1510 | 91 |
Class<?> theClass; |
2 | 92 |
if (className == null) { |
93 |
throw new RuntimeOperationsException(new |
|
94 |
IllegalArgumentException("The class name cannot be null"), |
|
95 |
"Exception occurred during object instantiation"); |
|
96 |
} |
|
97 |
||
16109 | 98 |
ReflectUtil.checkPackageAccess(className); |
2 | 99 |
try { |
100 |
if (clr == null) throw new ClassNotFoundException(className); |
|
101 |
theClass = clr.loadClass(className); |
|
102 |
} |
|
103 |
catch (ClassNotFoundException ee) { |
|
104 |
throw new ReflectionException(ee, |
|
105 |
"The MBean class could not be loaded by the default loader repository"); |
|
106 |
} |
|
107 |
||
108 |
return theClass; |
|
109 |
} |
|
110 |
||
111 |
||
112 |
/** |
|
113 |
* Gets the class for the specified class name using the MBean |
|
114 |
* Interceptor's classloader |
|
115 |
*/ |
|
1510 | 116 |
public Class<?> findClass(String className, ClassLoader loader) |
2 | 117 |
throws ReflectionException { |
118 |
||
119 |
return loadClass(className,loader); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Gets the class for the specified class name using the specified |
|
124 |
* class loader |
|
125 |
*/ |
|
1510 | 126 |
public Class<?> findClass(String className, ObjectName aLoader) |
2 | 127 |
throws ReflectionException, InstanceNotFoundException { |
128 |
||
129 |
if (aLoader == null) |
|
130 |
throw new RuntimeOperationsException(new |
|
131 |
IllegalArgumentException(), "Null loader passed in parameter"); |
|
132 |
||
133 |
// Retrieve the class loader from the repository |
|
134 |
ClassLoader loader = null; |
|
18207 | 135 |
synchronized (this) { |
136 |
loader = getClassLoader(aLoader); |
|
2 | 137 |
} |
138 |
if (loader == null) { |
|
139 |
throw new InstanceNotFoundException("The loader named " + |
|
140 |
aLoader + " is not registered in the MBeanServer"); |
|
141 |
} |
|
142 |
return findClass(className,loader); |
|
143 |
} |
|
144 |
||
145 |
||
146 |
/** |
|
147 |
* Return an array of Class corresponding to the given signature, using |
|
148 |
* the specified class loader. |
|
149 |
*/ |
|
1510 | 150 |
public Class<?>[] findSignatureClasses(String signature[], |
151 |
ClassLoader loader) |
|
152 |
throws ReflectionException { |
|
2 | 153 |
|
154 |
if (signature == null) return null; |
|
155 |
final ClassLoader aLoader = loader; |
|
156 |
final int length= signature.length; |
|
1510 | 157 |
final Class<?> tab[]=new Class<?>[length]; |
2 | 158 |
|
159 |
if (length == 0) return tab; |
|
160 |
try { |
|
161 |
for (int i= 0; i < length; i++) { |
|
162 |
// Start handling primitive types (int. boolean and so |
|
163 |
// forth) |
|
164 |
// |
|
165 |
||
1510 | 166 |
final Class<?> primCla = primitiveClasses.get(signature[i]); |
2 | 167 |
if (primCla != null) { |
168 |
tab[i] = primCla; |
|
169 |
continue; |
|
170 |
} |
|
171 |
||
16109 | 172 |
ReflectUtil.checkPackageAccess(signature[i]); |
2 | 173 |
// Ok we do not have a primitive type ! We need to build |
174 |
// the signature of the method |
|
175 |
// |
|
176 |
if (aLoader != null) { |
|
177 |
// We need to load the class through the class |
|
178 |
// loader of the target object. |
|
179 |
// |
|
180 |
tab[i] = Class.forName(signature[i], false, aLoader); |
|
181 |
} else { |
|
182 |
// Load through the default class loader |
|
183 |
// |
|
184 |
tab[i] = findClass(signature[i], |
|
185 |
this.getClass().getClassLoader()); |
|
186 |
} |
|
187 |
} |
|
188 |
} catch (ClassNotFoundException e) { |
|
189 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) { |
|
190 |
MBEANSERVER_LOGGER.logp(Level.FINEST, |
|
191 |
MBeanInstantiator.class.getName(), |
|
192 |
"findSignatureClasses", |
|
193 |
"The parameter class could not be found", e); |
|
194 |
} |
|
195 |
throw new ReflectionException(e, |
|
196 |
"The parameter class could not be found"); |
|
197 |
} catch (RuntimeException e) { |
|
198 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) { |
|
199 |
MBEANSERVER_LOGGER.logp(Level.FINEST, |
|
200 |
MBeanInstantiator.class.getName(), |
|
201 |
"findSignatureClasses", |
|
202 |
"Unexpected exception", e); |
|
203 |
} |
|
204 |
throw e; |
|
205 |
} |
|
206 |
return tab; |
|
207 |
} |
|
208 |
||
209 |
||
210 |
/** |
|
211 |
* Instantiates an object given its class, using its empty constructor. |
|
212 |
* The call returns a reference to the newly created object. |
|
213 |
*/ |
|
1510 | 214 |
public Object instantiate(Class<?> theClass) |
2 | 215 |
throws ReflectionException, MBeanException { |
16109 | 216 |
|
217 |
checkMBeanPermission(theClass, null, null, "instantiate"); |
|
218 |
||
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
219 |
Object moi; |
2 | 220 |
|
221 |
// ------------------------------ |
|
222 |
// ------------------------------ |
|
1510 | 223 |
Constructor<?> cons = findConstructor(theClass, null); |
2 | 224 |
if (cons == null) { |
225 |
throw new ReflectionException(new |
|
226 |
NoSuchMethodException("No such constructor")); |
|
227 |
} |
|
228 |
// Instantiate the new object |
|
229 |
try { |
|
230 |
ReflectUtil.checkPackageAccess(theClass); |
|
16878 | 231 |
ensureClassAccess(theClass); |
2 | 232 |
moi= cons.newInstance(); |
233 |
} catch (InvocationTargetException e) { |
|
234 |
// Wrap the exception. |
|
235 |
Throwable t = e.getTargetException(); |
|
236 |
if (t instanceof RuntimeException) { |
|
237 |
throw new RuntimeMBeanException((RuntimeException)t, |
|
238 |
"RuntimeException thrown in the MBean's empty constructor"); |
|
239 |
} else if (t instanceof Error) { |
|
240 |
throw new RuntimeErrorException((Error) t, |
|
241 |
"Error thrown in the MBean's empty constructor"); |
|
242 |
} else { |
|
243 |
throw new MBeanException((Exception) t, |
|
244 |
"Exception thrown in the MBean's empty constructor"); |
|
245 |
} |
|
246 |
} catch (NoSuchMethodError error) { |
|
247 |
throw new ReflectionException(new |
|
248 |
NoSuchMethodException("No constructor"), |
|
249 |
"No such constructor"); |
|
250 |
} catch (InstantiationException e) { |
|
251 |
throw new ReflectionException(e, |
|
252 |
"Exception thrown trying to invoke the MBean's empty constructor"); |
|
253 |
} catch (IllegalAccessException e) { |
|
254 |
throw new ReflectionException(e, |
|
255 |
"Exception thrown trying to invoke the MBean's empty constructor"); |
|
256 |
} catch (IllegalArgumentException e) { |
|
257 |
throw new ReflectionException(e, |
|
258 |
"Exception thrown trying to invoke the MBean's empty constructor"); |
|
259 |
} |
|
260 |
return moi; |
|
261 |
||
262 |
} |
|
263 |
||
264 |
||
265 |
||
266 |
/** |
|
267 |
* Instantiates an object given its class, the parameters and |
|
268 |
* signature of its constructor The call returns a reference to |
|
269 |
* the newly created object. |
|
270 |
*/ |
|
1510 | 271 |
public Object instantiate(Class<?> theClass, Object params[], |
2 | 272 |
String signature[], ClassLoader loader) |
273 |
throws ReflectionException, MBeanException { |
|
16109 | 274 |
|
275 |
checkMBeanPermission(theClass, null, null, "instantiate"); |
|
276 |
||
2 | 277 |
// Instantiate the new object |
278 |
// ------------------------------ |
|
279 |
// ------------------------------ |
|
1510 | 280 |
final Class<?>[] tab; |
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
281 |
Object moi; |
2 | 282 |
try { |
283 |
// Build the signature of the method |
|
284 |
// |
|
285 |
ClassLoader aLoader= theClass.getClassLoader(); |
|
286 |
// Build the signature of the method |
|
287 |
// |
|
288 |
tab = |
|
289 |
((signature == null)?null: |
|
290 |
findSignatureClasses(signature,aLoader)); |
|
291 |
} |
|
292 |
// Exception IllegalArgumentException raised in Jdk1.1.8 |
|
293 |
catch (IllegalArgumentException e) { |
|
294 |
throw new ReflectionException(e, |
|
295 |
"The constructor parameter classes could not be loaded"); |
|
296 |
} |
|
297 |
||
298 |
// Query the metadata service to get the right constructor |
|
1510 | 299 |
Constructor<?> cons = findConstructor(theClass, tab); |
2 | 300 |
|
301 |
if (cons == null) { |
|
302 |
throw new ReflectionException(new |
|
303 |
NoSuchMethodException("No such constructor")); |
|
304 |
} |
|
305 |
try { |
|
306 |
ReflectUtil.checkPackageAccess(theClass); |
|
16878 | 307 |
ensureClassAccess(theClass); |
2 | 308 |
moi = cons.newInstance(params); |
309 |
} |
|
310 |
catch (NoSuchMethodError error) { |
|
311 |
throw new ReflectionException(new |
|
312 |
NoSuchMethodException("No such constructor found"), |
|
313 |
"No such constructor" ); |
|
314 |
} |
|
315 |
catch (InstantiationException e) { |
|
316 |
throw new ReflectionException(e, |
|
317 |
"Exception thrown trying to invoke the MBean's constructor"); |
|
318 |
} |
|
319 |
catch (IllegalAccessException e) { |
|
320 |
throw new ReflectionException(e, |
|
321 |
"Exception thrown trying to invoke the MBean's constructor"); |
|
322 |
} |
|
323 |
catch (InvocationTargetException e) { |
|
324 |
// Wrap the exception. |
|
325 |
Throwable th = e.getTargetException(); |
|
326 |
if (th instanceof RuntimeException) { |
|
327 |
throw new RuntimeMBeanException((RuntimeException)th, |
|
328 |
"RuntimeException thrown in the MBean's constructor"); |
|
329 |
} else if (th instanceof Error) { |
|
330 |
throw new RuntimeErrorException((Error) th, |
|
331 |
"Error thrown in the MBean's constructor"); |
|
332 |
} else { |
|
333 |
throw new MBeanException((Exception) th, |
|
334 |
"Exception thrown in the MBean's constructor"); |
|
335 |
} |
|
336 |
} |
|
337 |
return moi; |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* De-serializes a byte array in the context of a classloader. |
|
342 |
* |
|
343 |
* @param loader the classloader to use for de-serialization |
|
344 |
* @param data The byte array to be de-sererialized. |
|
345 |
* |
|
346 |
* @return The de-serialized object stream. |
|
347 |
* |
|
348 |
* @exception OperationsException Any of the usual Input/Output related |
|
349 |
* exceptions. |
|
350 |
*/ |
|
351 |
public ObjectInputStream deserialize(ClassLoader loader, byte[] data) |
|
352 |
throws OperationsException { |
|
353 |
||
354 |
// Check parameter validity |
|
355 |
if (data == null) { |
|
356 |
throw new RuntimeOperationsException(new |
|
357 |
IllegalArgumentException(), "Null data passed in parameter"); |
|
358 |
} |
|
359 |
if (data.length == 0) { |
|
360 |
throw new RuntimeOperationsException(new |
|
361 |
IllegalArgumentException(), "Empty data passed in parameter"); |
|
362 |
} |
|
363 |
||
364 |
// Object deserialization |
|
365 |
ByteArrayInputStream bIn; |
|
366 |
ObjectInputStream objIn; |
|
367 |
||
368 |
bIn = new ByteArrayInputStream(data); |
|
369 |
try { |
|
370 |
objIn = new ObjectInputStreamWithLoader(bIn,loader); |
|
371 |
} catch (IOException e) { |
|
372 |
throw new OperationsException( |
|
373 |
"An IOException occurred trying to de-serialize the data"); |
|
374 |
} |
|
375 |
||
376 |
return objIn; |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* De-serializes a byte array in the context of a given MBean class loader. |
|
381 |
* <P>The class loader is the one that loaded the class with name |
|
382 |
* "className". |
|
383 |
* <P>The name of the class loader to be used for loading the specified |
|
384 |
* class is specified. If null, a default one has to be provided (for a |
|
385 |
* MBean Server, its own class loader will be used). |
|
386 |
* |
|
387 |
* @param className The name of the class whose class loader should |
|
388 |
* be used for the de-serialization. |
|
389 |
* @param data The byte array to be de-sererialized. |
|
390 |
* @param loaderName The name of the class loader to be used for loading |
|
391 |
* the specified class. If null, a default one has to be provided (for a |
|
392 |
* MBean Server, its own class loader will be used). |
|
393 |
* |
|
394 |
* @return The de-serialized object stream. |
|
395 |
* |
|
396 |
* @exception InstanceNotFoundException The specified class loader MBean is |
|
397 |
* not found. |
|
398 |
* @exception OperationsException Any of the usual Input/Output related |
|
399 |
* exceptions. |
|
400 |
* @exception ReflectionException The specified class could not be loaded |
|
401 |
* by the specified class loader. |
|
402 |
*/ |
|
403 |
public ObjectInputStream deserialize(String className, |
|
404 |
ObjectName loaderName, |
|
405 |
byte[] data, |
|
406 |
ClassLoader loader) |
|
407 |
throws InstanceNotFoundException, |
|
408 |
OperationsException, |
|
409 |
ReflectionException { |
|
410 |
||
411 |
// Check parameter validity |
|
412 |
if (data == null) { |
|
413 |
throw new RuntimeOperationsException(new |
|
414 |
IllegalArgumentException(), "Null data passed in parameter"); |
|
415 |
} |
|
416 |
if (data.length == 0) { |
|
417 |
throw new RuntimeOperationsException(new |
|
418 |
IllegalArgumentException(), "Empty data passed in parameter"); |
|
419 |
} |
|
420 |
if (className == null) { |
|
421 |
throw new RuntimeOperationsException(new |
|
422 |
IllegalArgumentException(), "Null className passed in parameter"); |
|
423 |
} |
|
16109 | 424 |
|
425 |
ReflectUtil.checkPackageAccess(className); |
|
1510 | 426 |
Class<?> theClass; |
2 | 427 |
if (loaderName == null) { |
428 |
// Load the class using the agent class loader |
|
429 |
theClass = findClass(className, loader); |
|
430 |
||
431 |
} else { |
|
432 |
// Get the class loader MBean |
|
433 |
try { |
|
434 |
ClassLoader instance = null; |
|
435 |
||
18207 | 436 |
instance = getClassLoader(loaderName); |
2 | 437 |
if (instance == null) |
438 |
throw new ClassNotFoundException(className); |
|
439 |
theClass = Class.forName(className, false, instance); |
|
440 |
} |
|
441 |
catch (ClassNotFoundException e) { |
|
442 |
throw new ReflectionException(e, |
|
443 |
"The MBean class could not be loaded by the " + |
|
444 |
loaderName.toString() + " class loader"); |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
// Object deserialization |
|
449 |
ByteArrayInputStream bIn; |
|
450 |
ObjectInputStream objIn; |
|
451 |
||
452 |
bIn = new ByteArrayInputStream(data); |
|
453 |
try { |
|
454 |
objIn = new ObjectInputStreamWithLoader(bIn, |
|
455 |
theClass.getClassLoader()); |
|
456 |
} catch (IOException e) { |
|
457 |
throw new OperationsException( |
|
458 |
"An IOException occurred trying to de-serialize the data"); |
|
459 |
} |
|
460 |
||
461 |
return objIn; |
|
462 |
} |
|
463 |
||
464 |
||
465 |
/** |
|
466 |
* Instantiates an object using the list of all class loaders registered |
|
467 |
* in the MBean Interceptor |
|
468 |
* (using its {@link javax.management.loading.ClassLoaderRepository}). |
|
469 |
* <P>The object's class should have a public constructor. |
|
470 |
* <P>It returns a reference to the newly created object. |
|
471 |
* <P>The newly created object is not registered in the MBean Interceptor. |
|
472 |
* |
|
473 |
* @param className The class name of the object to be instantiated. |
|
474 |
* |
|
475 |
* @return The newly instantiated object. |
|
476 |
* |
|
477 |
* @exception ReflectionException Wraps a |
|
478 |
* <CODE>java.lang.ClassNotFoundException</CODE> or the |
|
479 |
* <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the |
|
480 |
* object's constructor. |
|
481 |
* @exception MBeanException The constructor of the object has thrown an |
|
482 |
* exception |
|
483 |
* @exception RuntimeOperationsException Wraps a |
|
484 |
* <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in |
|
485 |
* parameter is null. |
|
486 |
*/ |
|
487 |
public Object instantiate(String className) |
|
488 |
throws ReflectionException, |
|
489 |
MBeanException { |
|
490 |
||
491 |
return instantiate(className, (Object[]) null, (String[]) null, null); |
|
492 |
} |
|
493 |
||
494 |
||
495 |
||
496 |
/** |
|
497 |
* Instantiates an object using the class Loader specified by its |
|
498 |
* <CODE>ObjectName</CODE>. |
|
499 |
* <P>If the loader name is null, a default one has to be provided (for a |
|
500 |
* MBean Server, the ClassLoader that loaded it will be used). |
|
501 |
* <P>The object's class should have a public constructor. |
|
502 |
* <P>It returns a reference to the newly created object. |
|
503 |
* <P>The newly created object is not registered in the MBean Interceptor. |
|
504 |
* |
|
505 |
* @param className The class name of the MBean to be instantiated. |
|
506 |
* @param loaderName The object name of the class loader to be used. |
|
507 |
* |
|
508 |
* @return The newly instantiated object. |
|
509 |
* |
|
510 |
* @exception ReflectionException Wraps a |
|
511 |
* <CODE>java.lang.ClassNotFoundException</CODE> or the |
|
512 |
* <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the |
|
513 |
* object's constructor. |
|
514 |
* @exception MBeanException The constructor of the object has thrown an |
|
515 |
* exception. |
|
516 |
* @exception InstanceNotFoundException The specified class loader is not |
|
517 |
* registered in the MBeanServerInterceptor. |
|
518 |
* @exception RuntimeOperationsException Wraps a |
|
519 |
* <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in |
|
520 |
* parameter is null. |
|
521 |
*/ |
|
522 |
public Object instantiate(String className, ObjectName loaderName, |
|
523 |
ClassLoader loader) |
|
524 |
throws ReflectionException, MBeanException, |
|
525 |
InstanceNotFoundException { |
|
526 |
||
527 |
return instantiate(className, loaderName, (Object[]) null, |
|
528 |
(String[]) null, loader); |
|
529 |
} |
|
530 |
||
531 |
||
532 |
/** |
|
533 |
* Instantiates an object using the list of all class loaders registered |
|
534 |
* in the MBean server |
|
535 |
* (using its {@link javax.management.loading.ClassLoaderRepository}). |
|
536 |
* <P>The object's class should have a public constructor. |
|
537 |
* <P>The call returns a reference to the newly created object. |
|
538 |
* <P>The newly created object is not registered in the MBean Interceptor. |
|
539 |
* |
|
540 |
* @param className The class name of the object to be instantiated. |
|
541 |
* @param params An array containing the parameters of the constructor to |
|
542 |
* be invoked. |
|
543 |
* @param signature An array containing the signature of the constructor to |
|
544 |
* be invoked. |
|
545 |
* |
|
546 |
* @return The newly instantiated object. |
|
547 |
* |
|
548 |
* @exception ReflectionException Wraps a |
|
549 |
* <CODE>java.lang.ClassNotFoundException</CODE> or the |
|
550 |
* <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the |
|
551 |
* object's constructor. |
|
552 |
* @exception MBeanException The constructor of the object has thrown an |
|
553 |
* exception |
|
554 |
* @exception RuntimeOperationsException Wraps a |
|
555 |
* <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in |
|
556 |
* parameter is null. |
|
557 |
*/ |
|
558 |
public Object instantiate(String className, |
|
559 |
Object params[], |
|
560 |
String signature[], |
|
561 |
ClassLoader loader) |
|
562 |
throws ReflectionException, |
|
563 |
MBeanException { |
|
564 |
||
1510 | 565 |
Class<?> theClass = findClassWithDefaultLoaderRepository(className); |
2 | 566 |
return instantiate(theClass, params, signature, loader); |
567 |
} |
|
568 |
||
569 |
||
570 |
||
571 |
/** |
|
572 |
* Instantiates an object. The class loader to be used is identified by its |
|
573 |
* object name. |
|
574 |
* <P>If the object name of the loader is null, a default has to be |
|
575 |
* provided (for example, for a MBean Server, the ClassLoader that loaded |
|
576 |
* it will be used). |
|
577 |
* <P>The object's class should have a public constructor. |
|
578 |
* <P>The call returns a reference to the newly created object. |
|
579 |
* <P>The newly created object is not registered in the MBean server. |
|
580 |
* |
|
581 |
* @param className The class name of the object to be instantiated. |
|
582 |
* @param params An array containing the parameters of the constructor to |
|
583 |
* be invoked. |
|
584 |
* @param signature An array containing the signature of the constructor to |
|
585 |
* be invoked. |
|
586 |
* @param loaderName The object name of the class loader to be used. |
|
587 |
* |
|
588 |
* @return The newly instantiated object. |
|
589 |
* |
|
590 |
* @exception ReflectionException Wraps a |
|
591 |
* <CODE>java.lang.ClassNotFoundException</CODE> or the |
|
592 |
* <CODE>java.lang.Exception</CODE> that occurred when trying to invoke the |
|
593 |
* object's constructor. |
|
594 |
* @exception MBeanException The constructor of the object has thrown an |
|
595 |
* exception |
|
596 |
* @exception InstanceNotFoundException The specified class loader is not |
|
597 |
* registered in the MBean Interceptor. |
|
598 |
* @exception RuntimeOperationsException Wraps a |
|
599 |
* <CODE>java.lang.IllegalArgumentException</CODE>: the className passed in |
|
600 |
* parameter is null. |
|
601 |
*/ |
|
602 |
public Object instantiate(String className, |
|
603 |
ObjectName loaderName, |
|
604 |
Object params[], |
|
605 |
String signature[], |
|
606 |
ClassLoader loader) |
|
607 |
throws ReflectionException, |
|
608 |
MBeanException, |
|
609 |
InstanceNotFoundException { |
|
610 |
||
611 |
// ------------------------------ |
|
612 |
// ------------------------------ |
|
1510 | 613 |
Class<?> theClass; |
2 | 614 |
|
615 |
if (loaderName == null) { |
|
616 |
theClass = findClass(className, loader); |
|
617 |
} else { |
|
618 |
theClass = findClass(className, loaderName); |
|
619 |
} |
|
620 |
return instantiate(theClass, params, signature, loader); |
|
621 |
} |
|
622 |
||
623 |
||
624 |
/** |
|
625 |
* Return the Default Loader Repository used by this instantiator object. |
|
626 |
**/ |
|
627 |
public ModifiableClassLoaderRepository getClassLoaderRepository() { |
|
18203 | 628 |
checkMBeanPermission((String)null, null, null, "getClassLoaderRepository"); |
2 | 629 |
return clr; |
630 |
} |
|
631 |
||
632 |
/** |
|
633 |
* Load a class with the specified loader, or with this object |
|
634 |
* class loader if the specified loader is null. |
|
635 |
**/ |
|
1510 | 636 |
static Class<?> loadClass(String className, ClassLoader loader) |
2 | 637 |
throws ReflectionException { |
1510 | 638 |
Class<?> theClass; |
2 | 639 |
if (className == null) { |
640 |
throw new RuntimeOperationsException(new |
|
641 |
IllegalArgumentException("The class name cannot be null"), |
|
642 |
"Exception occurred during object instantiation"); |
|
643 |
} |
|
16109 | 644 |
ReflectUtil.checkPackageAccess(className); |
2 | 645 |
try { |
646 |
if (loader == null) |
|
647 |
loader = MBeanInstantiator.class.getClassLoader(); |
|
648 |
if (loader != null) { |
|
649 |
theClass = Class.forName(className, false, loader); |
|
650 |
} else { |
|
651 |
theClass = Class.forName(className); |
|
652 |
} |
|
653 |
} catch (ClassNotFoundException e) { |
|
654 |
throw new ReflectionException(e, |
|
1705
08644ea24ce6
6780803: Wrong parameter name in description of EventClient::addListeners()
emcmanus
parents:
1702
diff
changeset
|
655 |
"The MBean class could not be loaded"); |
2 | 656 |
} |
657 |
return theClass; |
|
658 |
} |
|
659 |
||
660 |
||
661 |
||
662 |
/** |
|
663 |
* Load the classes specified in the signature with the given loader, |
|
664 |
* or with this object class loader. |
|
665 |
**/ |
|
1510 | 666 |
static Class<?>[] loadSignatureClasses(String signature[], |
667 |
ClassLoader loader) |
|
2 | 668 |
throws ReflectionException { |
669 |
||
670 |
if (signature == null) return null; |
|
671 |
final ClassLoader aLoader = |
|
672 |
(loader==null?MBeanInstantiator.class.getClassLoader():loader); |
|
673 |
final int length= signature.length; |
|
1510 | 674 |
final Class<?> tab[]=new Class<?>[length]; |
2 | 675 |
|
676 |
if (length == 0) return tab; |
|
677 |
try { |
|
678 |
for (int i= 0; i < length; i++) { |
|
679 |
// Start handling primitive types (int. boolean and so |
|
680 |
// forth) |
|
681 |
// |
|
682 |
||
1510 | 683 |
final Class<?> primCla = primitiveClasses.get(signature[i]); |
2 | 684 |
if (primCla != null) { |
685 |
tab[i] = primCla; |
|
686 |
continue; |
|
687 |
} |
|
688 |
||
689 |
// Ok we do not have a primitive type ! We need to build |
|
690 |
// the signature of the method |
|
691 |
// |
|
692 |
// We need to load the class through the class |
|
693 |
// loader of the target object. |
|
694 |
// |
|
16109 | 695 |
ReflectUtil.checkPackageAccess(signature[i]); |
2 | 696 |
tab[i] = Class.forName(signature[i], false, aLoader); |
697 |
} |
|
698 |
} catch (ClassNotFoundException e) { |
|
699 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) { |
|
700 |
MBEANSERVER_LOGGER.logp(Level.FINEST, |
|
701 |
MBeanInstantiator.class.getName(), |
|
702 |
"findSignatureClasses", |
|
703 |
"The parameter class could not be found", e); |
|
704 |
} |
|
705 |
throw new ReflectionException(e, |
|
706 |
"The parameter class could not be found"); |
|
707 |
} catch (RuntimeException e) { |
|
708 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) { |
|
709 |
MBEANSERVER_LOGGER.logp(Level.FINEST, |
|
710 |
MBeanInstantiator.class.getName(), |
|
711 |
"findSignatureClasses", |
|
712 |
"Unexpected exception", e); |
|
713 |
} |
|
714 |
throw e; |
|
715 |
} |
|
716 |
return tab; |
|
717 |
} |
|
718 |
||
719 |
private Constructor<?> findConstructor(Class<?> c, Class<?>[] params) { |
|
720 |
try { |
|
16109 | 721 |
return ConstructorUtil.getConstructor(c, params); |
2 | 722 |
} catch (Exception e) { |
723 |
return null; |
|
724 |
} |
|
725 |
} |
|
726 |
||
727 |
||
728 |
private static final Map<String, Class<?>> primitiveClasses = Util.newMap(); |
|
729 |
static { |
|
1510 | 730 |
for (Class<?> c : new Class<?>[] {byte.class, short.class, int.class, |
731 |
long.class, float.class, double.class, |
|
732 |
char.class, boolean.class}) |
|
2 | 733 |
primitiveClasses.put(c.getName(), c); |
734 |
} |
|
16109 | 735 |
|
736 |
private static void checkMBeanPermission(Class<?> clazz, |
|
737 |
String member, |
|
738 |
ObjectName objectName, |
|
739 |
String actions) { |
|
18203 | 740 |
if (clazz != null) { |
741 |
checkMBeanPermission(clazz.getName(), member, objectName, actions); |
|
742 |
} |
|
743 |
} |
|
744 |
||
745 |
private static void checkMBeanPermission(String classname, |
|
746 |
String member, |
|
747 |
ObjectName objectName, |
|
748 |
String actions) |
|
749 |
throws SecurityException { |
|
16109 | 750 |
SecurityManager sm = System.getSecurityManager(); |
18203 | 751 |
if (sm != null) { |
752 |
Permission perm = new MBeanPermission(classname, |
|
16109 | 753 |
member, |
754 |
objectName, |
|
755 |
actions); |
|
756 |
sm.checkPermission(perm); |
|
757 |
} |
|
758 |
} |
|
16878 | 759 |
|
24871
224e298c3978
8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents:
18207
diff
changeset
|
760 |
private static void ensureClassAccess(Class<?> clazz) |
16878 | 761 |
throws IllegalAccessException |
762 |
{ |
|
763 |
int mod = clazz.getModifiers(); |
|
764 |
if (!Modifier.isPublic(mod)) { |
|
765 |
throw new IllegalAccessException("Class is not public and can't be instantiated"); |
|
766 |
} |
|
767 |
} |
|
18207 | 768 |
|
769 |
private ClassLoader getClassLoader(final ObjectName name) { |
|
770 |
if(clr == null){ |
|
771 |
return null; |
|
772 |
} |
|
773 |
// Restrict to getClassLoader permission only |
|
774 |
Permissions permissions = new Permissions(); |
|
775 |
permissions.add(new MBeanPermission("*", null, name, "getClassLoader")); |
|
776 |
ProtectionDomain protectionDomain = new ProtectionDomain(null, permissions); |
|
777 |
ProtectionDomain[] domains = {protectionDomain}; |
|
778 |
AccessControlContext ctx = new AccessControlContext(domains); |
|
779 |
ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { |
|
780 |
public ClassLoader run() { |
|
781 |
return clr.getClassLoader(name); |
|
782 |
} |
|
783 |
}, ctx); |
|
784 |
return loader; |
|
785 |
} |
|
2 | 786 |
} |