KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > management > ManagementFactory


1 /*
2  * @(#)ManagementFactory.java 1.19 04/05/17
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.lang.management;
9
10 import javax.management.NotificationEmitter JavaDoc;
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.MBeanServerConnection JavaDoc;
14 import javax.management.MBeanServerPermission JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.management.InstanceNotFoundException JavaDoc;
17 import javax.management.MalformedObjectNameException JavaDoc;
18 import javax.management.openmbean.CompositeData JavaDoc;
19 import javax.management.openmbean.CompositeType JavaDoc;
20 import javax.management.openmbean.TabularData JavaDoc;
21 import java.util.List JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23 import java.lang.reflect.InvocationHandler JavaDoc;
24 import java.security.AccessController JavaDoc;
25 import java.security.Permission JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import sun.management.PlatformMXBeanInvocationHandler;
28
29 /**
30  * The <tt>ManagementFactory</tt> class is a factory class for getting
31  * managed beans for the Java platform.
32  * This class consists of static methods each of which returns
33  * one or more <a HREF="#MXBean">platform MXBean(s)</a> representing
34  * the management interface of a component of the Java virtual
35  * machine.
36  *
37  * <p>
38  * An application can access a platform MXBean in the following ways:
39  * <ul>
40  * <li><i>Direct access to an MXBean interface</i>
41  * <ol type="a">
42  * <li>Get the MXBean instance through the static factory method
43  * and access the MXBean locally of the running
44  * virtual machine.
45  * </li>
46  * <li>Construct an MXBean proxy instance that forwards the
47  * method calls to a given {@link MBeanServer MBeanServer} by calling
48  * {@link #newPlatformMXBeanProxy newPlatfromMXBeanProxy}.
49  * A proxy is typically constructed to remotely access
50  * an MXBean of another running virtual machine.
51  * </li>
52  * </ol></li>
53  * <li><i>Indirect access to an MXBean interface via MBeanServer</i>
54  * <ol type="a">
55  * <li>Go through the {@link #getPlatformMBeanServer
56  * platform MBeanServer} to access MXBeans locally or
57  * a specific <tt>MBeanServerConnection</tt> to access
58  * MXBeans remotely.
59  * The attributes and operations of an MXBean use only
60  * <em>JMX open types</em> which include basic
61  * data types, {@link CompositeData CompositeData},
62  * and {@link TabularData TabularData} defined in
63  * {@link javax.management.openmbean.OpenType OpenType}.
64  * The mapping is specified below.
65  * </li>
66  * </ol></li>
67  * </ul>
68  *
69  * <h4><a name="MXBean">Platform MXBeans</a></h4>
70  * A platform MXBean is a <i>managed bean</i> that conforms to
71  * the JMX Instrumentation Specification and only uses
72  * a set of basic data types described below.
73  * A JMX management application and the platform <tt>MBeanServer</tt>
74  * can interoperate without requiring classes for MXBean specific
75  * data types.
76  * The data types being transmitted between the JMX connector
77  * server and the connector client are
78  * {@link javax.management.openmbean.OpenType open types}
79  * and this allows interoperation across versions.
80  * <p>
81  * The platform MXBean interfaces use only the following data types:
82  * <ul>
83  * <li>Primitive types such as <tt>int</tt>, <tt>long</tt>,
84  * <tt>boolean</tt>, etc</li>
85  * <li>Wrapper classes for primitive types such as
86  * {@link java.lang.Integer Integer}, {@link java.lang.Long Long},
87  * {@link java.lang.Boolean Boolean}, etc and
88  * {@link java.lang.String String}</li>
89  * <li>{@link java.lang.Enum Enum} classes</li>
90  * <li>Classes that define only getter methods and define a static
91  * <tt>from</tt> method with a {@link CompositeData CompositeData}
92  * argument to convert from an input <tt>CompositeData</tt> to
93  * an instance of that class
94  * </li>
95  * <li>{@link java.util.List List&lt;E&gt;}
96  * where <tt>E</tt> is a primitive type, a wrapper class,
97  * an enum class, or a class supporting conversion from a
98  * <tt>CompositeData</tt> to its class
99  * </li>
100  * <li>{@link java.util.Map Map&lt;K,V&gt;}
101  * where <tt>K</tt> and <tt>V</tt> are
102  * a primitive type, a wrapper class,
103  * an enum class, or a class supporting conversion from a
104  * <tt>CompositeData</tt> to its class
105  * </li>
106  * </ul>
107  *
108  * <p>
109  * When an attribute or operation of a platform MXBean
110  * is accessed via an <tt>MBeanServer</tt>, the data types are mapped
111  * as follows:
112  * <ul>
113  * <li>A primitive type or a wrapper class is mapped
114  * to the same type.
115  * </li>
116  * <li>An {@link Enum} is mapped to
117  * <tt>String</tt> whose value is the name of the enum constant.
118  * <li>A class that defines only getter methods and a static
119  * <tt>from</tt> method with a {@link CompositeData CompositeData}
120  * argument is mapped to
121  * {@link javax.management.openmbean.CompositeData CompositeData}.
122  * </li>
123  * <li><tt>Map&lt;K,V&gt;</tt> is mapped to
124  * {@link TabularData TabularData}
125  * whose row type is a {@link CompositeType CompositeType} with
126  * two items whose names are <i>"key"</i> and <i>"value"</i>
127  * and the item types are
128  * the corresponding mapped type of <tt>K</tt> and <tt>V</tt>
129  * respectively and the <i>"key"</i> is the index.
130  * </li>
131  * <li><tt>List&lt;E&gt;</tt> is mapped to an array with the mapped
132  * type of <tt>E</tt> as the element type.
133  * </li>
134  * <li>An array of element type <tt>E</tt> is mapped to
135  * an array of the same dimenions with the mapped type of <tt>E</tt>
136  * as the element type.</li>
137  * </ul>
138  *
139  * The {@link javax.management.MBeanInfo MBeanInfo}
140  * for a platform MXBean
141  * describes the data types of the attributes and operations
142  * as primitive or open types mapped as specified above.
143  *
144  * <p>
145  * For example, the {@link MemoryMXBean}
146  * interface has the following <i>getter</i> and <i>setter</i> methods:
147  *
148  * <blockquote><pre>
149  * public MemoryUsage getHeapMemoryUsage();
150  * public boolean isVerbose();
151  * public void setVerbose(boolean value);
152  * </pre></blockquote>
153  *
154  * These attributes in the <tt>MBeanInfo</tt>
155  * of the <tt>MemoryMXBean</tt> have the following names and types:
156  *
157  * <blockquote>
158  * <table border>
159  * <tr>
160  * <th>Attribute Name</th>
161  * <th>Type</th>
162  * </tr>
163  * <tr>
164  * <td><tt>HeapMemoryUsage</tt></td>
165  * <td>{@link MemoryUsage#from
166  * CompositeData representing MemoryUsage}</td>
167  * </tr>
168  * <tr>
169  * <td><tt>Verbose</tt></td>
170  * <td><tt>boolean</tt></td>
171  * </tr>
172  * </table>
173  * </blockquote>
174  *
175  * <p>
176  * <b>Implementation Note:</b><p>
177  * The mapping specified above could be done in the implementation
178  * of each platform MXBean. One good implementation choice is to
179  * implement a MXBean as a {@link javax.management.DynamicMBean dynamic
180  * MBean}.
181  *
182  *
183  * <h4><a name="MXBeanNames">MXBean Names</a></h4>
184  * Each platform MXBean for a Java virtual machine has a unique
185  * {@link javax.management.ObjectName ObjectName} for
186  * registration in the platform <tt>MBeanServer</tt>.
187  * A Java virtual machine has a single instance of the following management
188  * interfaces:
189  *
190  * <blockquote>
191  * <table border>
192  * <tr>
193  * <th>Management Interface</th>
194  * <th>ObjectName</th>
195  * </tr>
196  * <tr>
197  * <td> {@link ClassLoadingMXBean} </td>
198  * <td> {@link #CLASS_LOADING_MXBEAN_NAME
199  * <tt>java.lang:type=ClassLoading</tt>}</td>
200  * </tr>
201  * <tr>
202  * <td> {@link MemoryMXBean} </td>
203  * <td> {@link #MEMORY_MXBEAN_NAME
204  * <tt>java.lang:type=Memory</tt>}</td>
205  * </tr>
206  * <tr>
207  * <td> {@link ThreadMXBean} </td>
208  * <td> {@link #THREAD_MXBEAN_NAME
209  * <tt>java.lang:type=Threading</tt>}</td>
210  * </tr>
211  * <tr>
212  * <td> {@link RuntimeMXBean} </td>
213  * <td> {@link #RUNTIME_MXBEAN_NAME
214  * <tt>java.lang:type=Runtime</tt>}</td>
215  * </tr>
216  * <tr>
217  * <td> {@link OperatingSystemMXBean} </td>
218  * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME
219  * <tt>java.lang:type=OperatingSystem</tt>}</td>
220  * </tr>
221  * </table>
222  * </blockquote>
223  *
224  * <p>
225  * A Java virtual machine has zero or a single instance of
226  * the following management interfaces.
227  *
228  * <blockquote>
229  * <table border>
230  * <tr>
231  * <th>Management Interface</th>
232  * <th>ObjectName</th>
233  * </tr>
234  * <tr>
235  * <td> {@link CompilationMXBean} </td>
236  * <td> {@link #COMPILATION_MXBEAN_NAME
237  * <tt>java.lang:type=Compilation</tt>}</td>
238  * </tr>
239  * </table>
240  * </blockquote>
241  *
242  * <p>
243  * A Java virtual machine may have one or more instances of the following
244  * management interfaces.
245  * <blockquote>
246  * <table border>
247  * <tr>
248  * <th>Management Interface</th>
249  * <th>ObjectName</th>
250  * </tr>
251  * <tr>
252  * <td> {@link GarbageCollectorMXBean} </td>
253  * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
254  * <tt>java.lang:type=GarbageCollector</tt>}<tt>,name=</tt><i>collector's name</i></td>
255  * </tr>
256  * <tr>
257  * <td> {@link MemoryManagerMXBean} </td>
258  * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
259  * <tt>java.lang:type=MemoryManager</tt>}<tt>,name=</tt><i>manager's name</i></td>
260  * </tr>
261  * <tr>
262  * <td> {@link MemoryPoolMXBean} </td>
263  * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
264  * <tt>java.lang:type=MemoryPool</tt>}<tt>,name=</tt><i>pool's name</i></td>
265  * </tr>
266  * </table>
267  * </blockquote>
268  *
269  * @see <a HREF="../../../javax/management/package-summary.html">
270  * JMX Specification.</a>
271  * @see <a HREF="package-summary.html#examples">
272  * Ways to Access Management Metrics</a>
273  * @see java.util.logging.LoggingMXBean
274  *
275  * @author Mandy Chung
276  * @version 1.19, 05/17/04
277  * @since 1.5
278  */

279 public class ManagementFactory {
280     // A class with only static fields and methods.
281
private ManagementFactory() {};
282
283     /**
284      * String representation of the
285      * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
286      */

287     public final static String JavaDoc CLASS_LOADING_MXBEAN_NAME =
288         "java.lang:type=ClassLoading";
289
290     /**
291      * String representation of the
292      * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
293      */

294     public final static String JavaDoc COMPILATION_MXBEAN_NAME =
295         "java.lang:type=Compilation";
296
297     /**
298      * String representation of the
299      * <tt>ObjectName</tt> for the {@link MemoryMXBean}.
300      */

301     public final static String JavaDoc MEMORY_MXBEAN_NAME =
302         "java.lang:type=Memory";
303
304     /**
305      * String representation of the
306      * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
307      */

308     public final static String JavaDoc OPERATING_SYSTEM_MXBEAN_NAME =
309         "java.lang:type=OperatingSystem";
310
311     /**
312      * String representation of the
313      * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
314      */

315     public final static String JavaDoc RUNTIME_MXBEAN_NAME =
316         "java.lang:type=Runtime";
317
318     /**
319      * String representation of the
320      * <tt>ObjectName</tt> for the {@link ThreadMXBean}.
321      */

322     public final static String JavaDoc THREAD_MXBEAN_NAME =
323         "java.lang:type=Threading";
324
325     /**
326      * The domain name and the type key property in
327      * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
328      * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
329      * can be formed by appending this string with
330      * "<tt>,name=</tt><i>collector's name</i>".
331      */

332     public final static String JavaDoc GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
333         "java.lang:type=GarbageCollector";
334
335     /**
336      * The domain name and the type key property in
337      * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
338      * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
339      * can be formed by appending this string with
340      * "<tt>,name=</tt><i>manager's name</i>".
341      */

342     public final static String JavaDoc MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
343         "java.lang:type=MemoryManager";
344
345     /**
346      * The domain name and the type key property in
347      * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
348      * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
349      * can be formed by appending this string with
350      * <tt>,name=</tt><i>pool's name</i>.
351      */

352     public final static String JavaDoc MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
353         "java.lang:type=MemoryPool";
354
355     /**
356      * Returns the managed bean for the class loading system of
357      * the Java virtual machine.
358      *
359      * @return a {@link ClassLoadingMXBean} object for
360      * the Java virtual machine.
361      */

362     public static ClassLoadingMXBean JavaDoc getClassLoadingMXBean() {
363         return sun.management.ManagementFactory.getClassLoadingMXBean();
364     }
365
366     /**
367      * Returns the managed bean for the memory system of
368      * the Java virtual machine.
369      *
370      * @return a {@link MemoryMXBean} object for the Java virtual machine.
371      */

372     public static MemoryMXBean JavaDoc getMemoryMXBean() {
373         return sun.management.ManagementFactory.getMemoryMXBean();
374     }
375
376     /**
377      * Returns the managed bean for the thread system of
378      * the Java virtual machine.
379      *
380      * @return a {@link ThreadMXBean} object for the Java virtual machine.
381      */

382     public static ThreadMXBean JavaDoc getThreadMXBean() {
383         return sun.management.ManagementFactory.getThreadMXBean();
384     }
385
386     /**
387      * Returns the managed bean for the runtime system of
388      * the Java virtual machine.
389      *
390      * @return a {@link RuntimeMXBean} object for the Java virtual machine.
391
392      */

393     public static RuntimeMXBean JavaDoc getRuntimeMXBean() {
394         return sun.management.ManagementFactory.getRuntimeMXBean();
395     }
396
397     /**
398      * Returns the managed bean for the compilation system of
399      * the Java virtual machine. This method returns <tt>null</tt>
400      * if the Java virtual machine has no compilation system.
401      *
402      * @return a {@link CompilationMXBean} object for the Java virtual
403      * machine or <tt>null</tt> if the Java virtual machine has
404      * no compilation system.
405      */

406     public static CompilationMXBean JavaDoc getCompilationMXBean() {
407         return sun.management.ManagementFactory.getCompilationMXBean();
408     }
409
410     /**
411      * Returns the managed bean for the operating system on which
412      * the Java virtual machine is running.
413      *
414      * @return an {@link OperatingSystemMXBean} object for
415      * the Java virtual machine.
416      */

417     public static OperatingSystemMXBean JavaDoc getOperatingSystemMXBean() {
418         return sun.management.ManagementFactory.getOperatingSystemMXBean();
419     }
420
421     /**
422      * Returns a list of {@link MemoryPoolMXBean} objects in the
423      * Java virtual machine.
424      * The Java virtual machine can have one or more memory pools.
425      * It may add or remove memory pools during execution.
426      *
427      * @return a list of <tt>MemoryPoolMXBean</tt> objects.
428      *
429      */

430     public static List JavaDoc<MemoryPoolMXBean JavaDoc> getMemoryPoolMXBeans() {
431         return sun.management.ManagementFactory.getMemoryPoolMXBeans();
432     }
433
434     /**
435      * Returns a list of {@link MemoryManagerMXBean} objects
436      * in the Java virtual machine.
437      * The Java virtual machine can have one or more memory managers.
438      * It may add or remove memory managers during execution.
439      *
440      * @return a list of <tt>MemoryManagerMXBean</tt> objects.
441      *
442      */

443     public static List JavaDoc<MemoryManagerMXBean JavaDoc> getMemoryManagerMXBeans() {
444         return sun.management.ManagementFactory.getMemoryManagerMXBeans();
445     }
446
447
448     /**
449      * Returns a list of {@link GarbageCollectorMXBean} objects
450      * in the Java virtual machine.
451      * The Java virtual machine may have one or more
452      * <tt>GarbageCollectorMXBean</tt> objects.
453      * It may add or remove <tt>GarbageCollectorMXBean</tt>
454      * during execution.
455      *
456      * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
457      *
458      */

459     public static List JavaDoc<GarbageCollectorMXBean JavaDoc> getGarbageCollectorMXBeans() {
460         return sun.management.ManagementFactory.getGarbageCollectorMXBeans();
461     }
462
463     private static MBeanServer JavaDoc platformMBeanServer;
464     /**
465      * Returns the platform {@link javax.management.MBeanServer MBeanServer}.
466      * On the first call to this method, it first creates the platform
467      * <tt>MBeanServer</tt> by calling the
468      * {@link MBeanServerFactory#createMBeanServer}
469      * method and registers the platform MXBeans in this platform
470      * <tt>MBeanServer</tt> using the <a HREF="#MXBeanNames">MXBean names</a>
471      * defined in the class description.
472      * This method, in subsequent calls, will simply return the
473      * initially created platform <tt>MBeanServer</tt>.
474      * <p>
475      * MXBeans that get created and destroyed dynamically, for example,
476      * memory {@link MemoryPoolMXBean pools} and
477      * {@link MemoryManagerMXBean managers},
478      * will automatically be registered and deregistered into the platform
479      * <tt>MBeanServer</tt>.
480      * <p>
481      * If the system property <tt>javax.management.builder.initial</tt>
482      * is set, the platform <tt>MBeanServer</tt> creation will be done
483      * by the specified {@link javax.management.MBeanServerBuilder}.
484      * <p>
485      * It is recommended that this platform MBeanServer also be used
486      * to register other application managed beans
487      * besides the platform MXBeans.
488      * This will allow all MBeans to be published through the same
489      * <tt>MBeanServer</tt> and hence allow for easier network publishing
490      * and discovery.
491      * Name conflicts with the platform MXBeans should be avoided.
492      *
493      * @return the platform <tt>MBeanServer</tt>; the platform
494      * MXBeans are registered into the platform <tt>MBeanServer</tt>
495      * at the first time this method is called.
496      *
497      * @exception SecurityException if there is a security manager
498      * and the caller does not have the permission required by
499      * {@link javax.management.MBeanServerFactory#createMBeanServer}.
500      *
501      * @see javax.management.MBeanServerFactory
502      * @see javax.management.MBeanServerFactory#createMBeanServer
503      */

504     public static synchronized MBeanServer JavaDoc getPlatformMBeanServer() {
505         SecurityManager JavaDoc sm = System.getSecurityManager();
506         if (sm != null) {
507             Permission JavaDoc perm = new MBeanServerPermission JavaDoc("createMBeanServer");
508             sm.checkPermission(perm);
509         }
510
511         if (platformMBeanServer == null) {
512             platformMBeanServer =
513                 sun.management.ManagementFactory.createPlatformMBeanServer();
514         }
515         return platformMBeanServer;
516     }
517
518     /**
519      * Returns a proxy for a platform MXBean interface of a
520      * given <a HREF="#MXBeanNames">MXBean name</a>
521      * that forwards its method calls through the given
522      * <tt>MBeanServerConnection</tt>.
523      *
524      * <p>This method is equivalent to:
525      * <blockquote>
526      * {@link java.lang.reflect.Proxy#newProxyInstance
527      * Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
528      * new Class[] { mxbeanInterface }, handler)</tt>
529      * </blockquote>
530      *
531      * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
532      * InvocationHandler} to which method invocations to the MXBean interface
533      * are dispatched. This <tt>handler</tt> converts an input parameter
534      * from an MXBean data type to its mapped open type before forwarding
535      * to the <tt>MBeanServer</tt> and converts a return value from
536      * an MXBean method call through the <tt>MBeanServer</tt>
537      * from an open type to the corresponding return type declared in
538      * the MXBean interface.
539      *
540      * <p>
541      * If the MXBean is a notification emitter (i.e.,
542      * it implements {@link NotificationEmitter NotificationEmitter}),
543      * both the <tt>mxbeanInterface</tt> and <tt>NotificationEmitter</tt>
544      * will be implemented by this proxy.
545      *
546      * <p>
547      * <b>Notes:</b>
548      * <ol>
549      * <li>Using an MXBean proxy is a convenience remote access to
550      * a platform MXBean of a running virtual machine. All method
551      * calls to the MXBean proxy are forwarded to an
552      * <tt>MBeanServerConnection</tt> where
553      * {@link java.io.IOException IOException} may be thrown
554      * when the communication problem occurs with the connector server.
555      * An application remotely accesses the platform MXBeans using
556      * proxy should prepare to catch <tt>IOException</tt> as if
557      * accessing with the <tt>MBeanServerConnector</tt> interface.</li>
558      *
559      * <li>When a client application is designed to remotely access MXBeans
560      * for a running virtual machine whose version is different than
561      * the version on which the application is running,
562      * it should prepare to catch
563      * {@link java.io.InvalidObjectException InvalidObjectException}
564      * which is thrown when an MXBean proxy receives a name of an
565      * enum constant which is missing in the enum class loaded in
566      * the client application. </li>
567      *
568      * <li>{@link javax.management.MBeanServerInvocationHandler
569      * MBeanServerInvocationHandler} or its
570      * {@link javax.management.MBeanServerInvocationHandler#newProxyInstance
571      * newProxyInstance} method cannot be used to create
572      * a proxy for a platform MXBean. The proxy object created
573      * by <tt>MBeanServerInvocationHandler</tt> does not handle
574      * the properties of the platform MXBeans described in
575      * the <a HREF="#MXBean">class specification</a>.
576      *</li>
577      * </ol>
578      *
579      * @param connection the <tt>MBeanServerConnection</tt> to forward to.
580      * @param mxbeanName the name of a platform MXBean within
581      * <tt>connection</tt> to forward to. <tt>mxbeanName</tt> must be
582      * in the format of {@link ObjectName ObjectName}.
583      * @param mxbeanInterface the MXBean interface to be implemented
584      * by the proxy.
585      *
586      * @throws IllegalArgumentException if
587      * <ul>
588      * <li><tt>mxbeanName</tt> is not with a valid
589      * {@link ObjectName ObjectName} format, or</li>
590      * <li>the named MXBean in the <tt>connection</tt> is
591      * not a MXBean provided by the platform, or</li>
592      * <li>the named MXBean is not registered in the
593      * <tt>MBeanServerConnection</tt>, or</li>
594      * <li>the named MXBean is not an instance of the given
595      * <tt>mxbeanInterface</tt></li>
596      * </ul>
597      *
598      * @throws java.io.IOException if a communication problem
599      * occurred when accessing the <tt>MBeanServerConnection</tt>.
600      */

601     public static <T> T
602         newPlatformMXBeanProxy(MBeanServerConnection JavaDoc connection,
603                                String JavaDoc mxbeanName,
604                                Class JavaDoc<T> mxbeanInterface)
605             throws java.io.IOException JavaDoc {
606
607         final Class JavaDoc interfaceClass = mxbeanInterface;
608         // Only allow MXBean interfaces from rt.jar loaded by the
609
// bootstrap class loader
610
final ClassLoader JavaDoc loader = (ClassLoader JavaDoc)
611             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
612                 public Object JavaDoc run() {
613                     return interfaceClass.getClassLoader();
614                 }
615             });
616         if (loader != null) {
617             throw new IllegalArgumentException JavaDoc(mxbeanName +
618                 " is not a platform MXBean");
619         }
620
621         try {
622             final ObjectName JavaDoc objName = new ObjectName JavaDoc(mxbeanName);
623             if (!connection.isInstanceOf(objName, interfaceClass.getName())) {
624                 throw new IllegalArgumentException JavaDoc(mxbeanName +
625                     " is not an instance of " + interfaceClass);
626             }
627
628             final Class JavaDoc[] interfaces;
629             // check if the registered MBean is a notification emitter
630
if (connection.isInstanceOf(objName,
631                                         NOTIF_EMITTER)) {
632                 interfaces = new Class JavaDoc[] {
633                                  interfaceClass,
634                                  NotificationEmitter JavaDoc.class
635                              };
636             } else {
637                 interfaces = new Class JavaDoc[] {interfaceClass};
638             }
639             
640             // create a MXBean proxy
641
InvocationHandler JavaDoc handler =
642                 new PlatformMXBeanInvocationHandler(connection,
643                                                     objName,
644                                                     interfaceClass);
645             return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
646                                               interfaces,
647                                               handler);
648         } catch (InstanceNotFoundException JavaDoc e) {
649             final IllegalArgumentException JavaDoc iae =
650                 new IllegalArgumentException JavaDoc(mxbeanName +
651                     " not found in the connection.");
652             iae.initCause(e);
653             throw iae;
654         } catch (MalformedObjectNameException JavaDoc e) {
655             final IllegalArgumentException JavaDoc iae =
656                 new IllegalArgumentException JavaDoc(mxbeanName +
657                     " is not a valid ObjectName format.");
658             iae.initCause(e);
659             throw iae;
660         }
661     }
662
663     private static final String JavaDoc NOTIF_EMITTER =
664         "javax.management.NotificationEmitter";
665 }
666
Popular Tags