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