KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > mbeanserver > MBeanInstantiatorImpl


1 /*
2  * @(#)MBeanInstantiatorImpl.java 1.32 07/07/24
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.jmx.mbeanserver;
9
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12 import java.io.*;
13
14 import javax.management.*;
15
16 import com.sun.jmx.trace.Trace;
17 import sun.reflect.misc.ReflectUtil;
18
19 /**
20  * Implements the MBeanInstantiator interface. Provides methods for
21  * instantiating objects, finding the class given its name and using
22  * different class loaders, deserializing objects in the context of a
23  * given class loader.
24  *
25  * @since 1.5
26  * @since.unbundled JMX RI 1.2
27  */

28 class MBeanInstantiatorImpl implements MBeanInstantiator {
29
30     private final ModifiableClassLoaderRepository clr;
31     // private MetaData meta = null;
32

33     /** The name of this class to be used for tracing */
34     private final static String JavaDoc dbgTag = "MBeanInstantiatorImpl";
35
36     public MBeanInstantiatorImpl(ModifiableClassLoaderRepository clr) {
37     this.clr = clr;
38     }
39
40   
41     public void testCreation(Class JavaDoc c) throws NotCompliantMBeanException {
42     Introspector.testCreation(c);
43     }
44
45     public Class JavaDoc findClassWithDefaultLoaderRepository(String JavaDoc className)
46     throws ReflectionException {
47
48     Class JavaDoc theClass;
49     if (className == null) {
50         throw new RuntimeOperationsException(new
51         IllegalArgumentException JavaDoc("The class name cannot be null"),
52                              "Exception occured during object instantiation");
53     }
54
55     try {
56         if (clr == null) throw new ClassNotFoundException JavaDoc(className);
57         theClass = clr.loadClass(className);
58     }
59     catch (ClassNotFoundException JavaDoc ee) {
60         throw new ReflectionException(ee,
61        "The MBean class could not be loaded by the default loader repository");
62     }
63     
64     return theClass;
65     }
66
67
68     public Class JavaDoc findClass(String JavaDoc className, ClassLoader JavaDoc loader)
69         throws ReflectionException {
70    
71         return loadClass(className,loader);
72     }
73
74     public Class JavaDoc findClass(String JavaDoc className, ObjectName aLoader)
75         throws ReflectionException, InstanceNotFoundException {
76     Class JavaDoc theClass = null;
77
78         if (aLoader == null)
79         throw new RuntimeOperationsException(new
80         IllegalArgumentException JavaDoc(), "Null loader passed in parameter");
81
82         // Retrieve the class loader from the repository
83
ClassLoader JavaDoc loader = null;
84         synchronized(this) {
85         if (clr!=null)
86         loader = clr.getClassLoader(aLoader);
87         }
88         if (loader == null) {
89             throw new InstanceNotFoundException("The loader named " +
90                aLoader + " is not registered in the MBeanServer");
91         }
92     return findClass(className,loader);
93     }
94
95
96     public Class JavaDoc[] findSignatureClasses(String JavaDoc signature[],
97                     ClassLoader JavaDoc loader)
98     throws ReflectionException {
99
100     if (signature == null) return null;
101     final ClassLoader JavaDoc aLoader = (ClassLoader JavaDoc) loader;
102     final int length= signature.length;
103     final Class JavaDoc tab[]=new Class JavaDoc[length];
104
105     if (length == 0) return tab;
106     try {
107         for (int i= 0; i < length; i++) {
108         // Start handling primitive types (int. boolean and so
109
// forth)
110
//
111

112         final Class JavaDoc primCla =
113             StandardMetaDataImpl.findClassForPrim(signature[i]);
114         if (primCla != null) {
115             tab[i] = primCla;
116             continue;
117         }
118
119         // Ok we do not have a primitive type ! We need to build
120
// the signature of the method
121
//
122
if (aLoader != null) {
123             // We need to load the class through the class
124
// loader of the target object.
125
//
126
tab[i] = Class.forName(signature[i], false, aLoader);
127         } else {
128             // Load through the default class loader
129
//
130
tab[i] = findClass(signature[i],
131                        this.getClass().getClassLoader());
132         }
133         }
134     } catch (ClassNotFoundException JavaDoc e) {
135         debugX("findSignatureClasses",e);
136         throw new ReflectionException(e,
137               "The parameter class could not be found");
138     } catch (RuntimeException JavaDoc e) {
139         debugX("findSignatureClasses",e);
140         throw e;
141     }
142     return tab;
143     }
144
145
146     public Object JavaDoc instantiate(Class JavaDoc theClass)
147     throws ReflectionException, MBeanException {
148         Object JavaDoc moi = null;
149
150
151     // ------------------------------
152
// ------------------------------
153
Constructor JavaDoc cons =
154         StandardMetaDataImpl.findConstructor(theClass, null);
155         if (cons == null) {
156             throw new ReflectionException(new
157         NoSuchMethodException JavaDoc("No such constructor"));
158         }
159         // Instantiate the new object
160
try {
161             ReflectUtil.checkPackageAccess(theClass);
162             moi= cons.newInstance((Object JavaDoc[]) null);
163         } catch (InvocationTargetException JavaDoc e) {
164             // Wrap the exception.
165
Throwable JavaDoc t = e.getTargetException();
166             if (t instanceof RuntimeException JavaDoc) {
167                 throw new RuntimeMBeanException((RuntimeException JavaDoc)t,
168                    "RuntimeException thrown in the MBean's empty constructor");
169             } else if (t instanceof Error JavaDoc) {
170                 throw new RuntimeErrorException((Error JavaDoc) t,
171                    "Error thrown in the MBean's empty constructor");
172             } else {
173                 throw new MBeanException((Exception JavaDoc) t,
174                    "Exception thrown in the MBean's empty constructor");
175             }
176         } catch (NoSuchMethodError JavaDoc error) {
177             throw new ReflectionException(new
178         NoSuchMethodException JavaDoc("No constructor"),
179                       "No such constructor");
180         } catch (InstantiationException JavaDoc e) {
181             throw new ReflectionException(e,
182             "Exception thrown trying to invoke the MBean's empty constructor");
183         } catch (IllegalAccessException JavaDoc e) {
184             throw new ReflectionException(e,
185             "Exception thrown trying to invoke the MBean's empty constructor");
186         } catch (IllegalArgumentException JavaDoc e) {
187             throw new ReflectionException(e,
188             "Exception thrown trying to invoke the MBean's empty constructor");
189         }
190         return moi;
191
192     }
193
194    
195
196     public Object JavaDoc instantiate(Class JavaDoc theClass, Object JavaDoc params[],
197                   String JavaDoc signature[], ClassLoader JavaDoc loader)
198         throws ReflectionException, MBeanException {
199         // Instantiate the new object
200

201     // ------------------------------
202
// ------------------------------
203
final Class JavaDoc[] tab;
204         Object JavaDoc moi= null;
205         try {
206         // Build the signature of the method
207
//
208
ClassLoader JavaDoc aLoader= (ClassLoader JavaDoc) theClass.getClassLoader();
209         // Build the signature of the method
210
//
211
tab =
212         ((signature == null)?null:
213          findSignatureClasses(signature,aLoader));
214     }
215         // Exception IllegalArgumentException raised in Jdk1.1.8
216
catch (IllegalArgumentException JavaDoc e) {
217             throw new ReflectionException(e,
218             "The constructor parameter classes could not be loaded");
219         }
220     
221         // Query the metadata service to get the right constructor
222
Constructor JavaDoc cons = null;
223         cons= StandardMetaDataImpl.findConstructor(theClass, tab);
224         
225         if (cons == null) {
226             throw new ReflectionException(new
227         NoSuchMethodException JavaDoc("No such constructor"));
228         }
229         try {
230             ReflectUtil.checkPackageAccess(theClass);
231             moi = cons.newInstance(params);
232         }
233         catch (NoSuchMethodError JavaDoc error) {
234             throw new ReflectionException(new
235         NoSuchMethodException JavaDoc("No such constructor found"),
236                       "No such constructor" );
237         }
238         catch (InstantiationException JavaDoc e) {
239             throw new ReflectionException(e,
240                 "Exception thrown trying to invoke the MBean's constructor");
241         }
242         catch (IllegalAccessException JavaDoc e) {
243             throw new ReflectionException(e,
244                 "Exception thrown trying to invoke the MBean's constructor");
245         }
246         catch (InvocationTargetException JavaDoc e) {
247             // Wrap the exception.
248
Throwable JavaDoc th = e.getTargetException();
249             if (th instanceof RuntimeException JavaDoc) {
250                 throw new RuntimeMBeanException((RuntimeException JavaDoc)th,
251               "RuntimeException thrown in the MBean's constructor");
252             } else if (th instanceof Error JavaDoc) {
253                 throw new RuntimeErrorException((Error JavaDoc) th,
254                       "Error thrown in the MBean's constructor");
255             } else {
256                 throw new MBeanException((Exception JavaDoc) th,
257                       "Exception thrown in the MBean's constructor");
258             }
259         }
260         return moi;
261     }
262
263     public ObjectInputStream deserialize(ClassLoader JavaDoc loader, byte[] data)
264     throws OperationsException {
265
266         // Check parameter validity
267
if (data == null) {
268             throw new RuntimeOperationsException(new
269         IllegalArgumentException JavaDoc(), "Null data passed in parameter");
270         }
271         if (data.length == 0) {
272         throw new RuntimeOperationsException(new
273         IllegalArgumentException JavaDoc(), "Empty data passed in parameter");
274         }
275  
276     // Object deserialization
277
ByteArrayInputStream bIn;
278         ObjectInputStream objIn;
279         String JavaDoc typeStr;
280
281         bIn = new ByteArrayInputStream(data);
282         try {
283             objIn = new ObjectInputStreamWithLoader(bIn,loader);
284         } catch (IOException e) {
285             throw new OperationsException(
286                      "An IOException occured trying to de-serialize the data");
287         }
288  
289         return objIn;
290     }
291
292     public ObjectInputStream deserialize(String JavaDoc className,
293                      ObjectName loaderName,
294                      byte[] data,
295                      ClassLoader JavaDoc loader)
296     throws InstanceNotFoundException,
297            OperationsException,
298            ReflectionException {
299
300         // Check parameter validity
301
if (data == null) {
302             throw new RuntimeOperationsException(new
303         IllegalArgumentException JavaDoc(), "Null data passed in parameter");
304         }
305         if (data.length == 0) {
306             throw new RuntimeOperationsException(new
307         IllegalArgumentException JavaDoc(), "Empty data passed in parameter");
308         }
309         if (className == null) {
310             throw new RuntimeOperationsException(new
311          IllegalArgumentException JavaDoc(), "Null className passed in parameter");
312         }
313         Class JavaDoc theClass = null;
314         if (loaderName == null) {
315             // Load the class using the agent class loader
316
theClass = findClass(className, loader);
317         
318         } else {
319             // Get the class loader MBean
320
try {
321         ClassLoader JavaDoc instance = null;
322         
323         if (clr!=null)
324             instance = clr.getClassLoader(loaderName);
325         if (instance == null)
326             throw new ClassNotFoundException JavaDoc(className);
327         theClass = Class.forName(className, false, instance);
328             }
329             catch (ClassNotFoundException JavaDoc e) {
330                 throw new ReflectionException(e,
331                                "The MBean class could not be loaded by the " +
332                        loaderName.toString() + " class loader");
333             }
334         }
335  
336         // Object deserialization
337
ByteArrayInputStream bIn;
338         ObjectInputStream objIn;
339         String JavaDoc typeStr;
340         
341         bIn = new ByteArrayInputStream(data);
342         try {
343             objIn = new ObjectInputStreamWithLoader(bIn,
344                        theClass.getClassLoader());
345         } catch (IOException e) {
346             throw new OperationsException(
347                     "An IOException occured trying to de-serialize the data");
348         }
349         
350         return objIn;
351     }
352
353
354     public Object JavaDoc instantiate(String JavaDoc className)
355     throws ReflectionException,
356     MBeanException {
357
358     return instantiate(className, (Object JavaDoc[]) null, (String JavaDoc[]) null, null);
359     }
360
361
362     public Object JavaDoc instantiate(String JavaDoc className, ObjectName loaderName,
363                   ClassLoader JavaDoc loader)
364         throws ReflectionException, MBeanException,
365            InstanceNotFoundException {
366
367     return instantiate(className, loaderName, (Object JavaDoc[]) null,
368                (String JavaDoc[]) null, loader);
369     }
370
371
372     public Object JavaDoc instantiate(String JavaDoc className,
373                   Object JavaDoc params[],
374                   String JavaDoc signature[],
375                   ClassLoader JavaDoc loader)
376         throws ReflectionException,
377     MBeanException {
378
379     Class JavaDoc theClass = findClassWithDefaultLoaderRepository(className);
380     return instantiate(theClass, params, signature, loader);
381     }
382
383
384
385     public Object JavaDoc instantiate(String JavaDoc className,
386                   ObjectName loaderName,
387                   Object JavaDoc params[],
388                   String JavaDoc signature[],
389                   ClassLoader JavaDoc loader)
390         throws ReflectionException,
391            MBeanException,
392     InstanceNotFoundException {
393
394     // ------------------------------
395
// ------------------------------
396
Class JavaDoc theClass;
397     
398     if (loaderName == null) {
399         theClass = findClass(className, loader);
400     } else {
401         theClass = findClass(className, loaderName);
402     }
403     return instantiate(theClass, params, signature, loader);
404     }
405
406
407     public ModifiableClassLoaderRepository getClassLoaderRepository() {
408     return clr;
409     }
410
411     /**
412      * Load a class with the specified loader, or with this object
413      * class loader if the specified loader is null.
414      **/

415     static Class JavaDoc loadClass(String JavaDoc className, ClassLoader JavaDoc loader)
416         throws ReflectionException {
417    
418         Class JavaDoc theClass = null;
419     if (className == null) {
420         throw new RuntimeOperationsException(new
421         IllegalArgumentException JavaDoc("The class name cannot be null"),
422                               "Exception occured during object instantiation");
423     }
424     try {
425         if (loader == null)
426         loader = MBeanInstantiatorImpl.class.getClassLoader();
427         if (loader != null) {
428         theClass = Class.forName(className, false, loader);
429         } else {
430         theClass = Class.forName(className);
431         }
432     } catch (ClassNotFoundException JavaDoc e) {
433         throw new ReflectionException(e,
434         "The MBean class could not be loaded by the context classloader");
435     }
436         return theClass;
437     }
438
439
440     
441     /**
442      * Load the classes specified in the signature with the given loader,
443      * or with this object class loader.
444      **/

445     static Class JavaDoc[] loadSignatureClasses(String JavaDoc signature[],
446                     ClassLoader JavaDoc loader)
447     throws ReflectionException {
448         
449     if (signature == null) return null;
450     final ClassLoader JavaDoc aLoader =
451        (loader==null?MBeanInstantiatorImpl.class.getClassLoader():loader);
452     final int length= signature.length;
453     final Class JavaDoc tab[]=new Class JavaDoc[length];
454
455     if (length == 0) return tab;
456     try {
457         for (int i= 0; i < length; i++) {
458         // Start handling primitive types (int. boolean and so
459
// forth)
460
//
461

462         final Class JavaDoc primCla =
463             StandardMetaDataImpl.findClassForPrim(signature[i]);
464         if (primCla != null) {
465             tab[i] = primCla;
466             continue;
467         }
468
469         // Ok we do not have a primitive type ! We need to build
470
// the signature of the method
471
//
472
// We need to load the class through the class
473
// loader of the target object.
474
//
475
tab[i] = Class.forName(signature[i], false, aLoader);
476         }
477     } catch (ClassNotFoundException JavaDoc e) {
478         debugX("findSignatureClasses",e);
479         throw new ReflectionException(e,
480               "The parameter class could not be found");
481     } catch (RuntimeException JavaDoc e) {
482         debugX("findSignatureClasses",e);
483         throw e;
484     }
485     return tab;
486     }
487
488
489     // TRACES & DEBUG
490
//---------------
491

492     private static boolean isTraceOn() {
493         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
494     }
495
496     private static void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
497         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
498     }
499
500     private static void trace(String JavaDoc func, String JavaDoc info) {
501         trace(dbgTag, func, info);
502     }
503
504     private static boolean isDebugOn() {
505         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
506     }
507
508     private static void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
509         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
510     }
511
512     private static void debug(String JavaDoc func, String JavaDoc info) {
513         debug(dbgTag, func, info);
514     }
515
516     private static void debugX(String JavaDoc func,Throwable JavaDoc e) {
517     if (isDebugOn()) {
518         final StringWriter s = new StringWriter();
519         e.printStackTrace(new PrintWriter(s));
520         final String JavaDoc stack = s.toString();
521         
522         debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
523         debug(dbgTag,func,stack);
524         
525         // java.lang.System.err.println("**** Exception caught in "+
526
// func+"(): "+e);
527
// java.lang.System.err.println(stack);
528
}
529     }
530     
531 }
532
Popular Tags