001 /*
002 * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.lang.management;
027
028 import javax.management.MBeanServer;
029 import javax.management.MBeanServerConnection;
030 import javax.management.MBeanServerPermission;
031 import javax.management.ObjectName;
032 import javax.management.InstanceNotFoundException;
033 import javax.management.MalformedObjectNameException;
034 import java.util.List;
035 import java.security.AccessController;
036 import java.security.Permission;
037 import java.security.PrivilegedAction;
038 import javax.management.JMX;
039
040 /**
041 * The <tt>ManagementFactory</tt> class is a factory class for getting
042 * managed beans for the Java platform.
043 * This class consists of static methods each of which returns
044 * one or more <a href="#MXBean">platform MXBean(s)</a> representing
045 * the management interface of a component of the Java virtual
046 * machine.
047 *
048 * <p>
049 * An application can access a platform MXBean in the following ways:
050 * <ul>
051 * <li><i>Direct access to an MXBean interface</i>
052 * <ol type="a">
053 * <li>Get the MXBean instance through the static factory method
054 * and access the MXBean locally of the running
055 * virtual machine.
056 * </li>
057 * <li>Construct an MXBean proxy instance that forwards the
058 * method calls to a given {@link MBeanServer MBeanServer} by calling
059 * {@link #newPlatformMXBeanProxy newPlatfromMXBeanProxy}.
060 * A proxy is typically constructed to remotely access
061 * an MXBean of another running virtual machine.
062 * </li>
063 * </ol></li>
064 * <li><i>Indirect access to an MXBean interface via MBeanServer</i>
065 * <ol type="a">
066 * <li>Go through the {@link #getPlatformMBeanServer
067 * platform MBeanServer} to access MXBeans locally or
068 * a specific <tt>MBeanServerConnection</tt> to access
069 * MXBeans remotely.
070 * The attributes and operations of an MXBean use only
071 * <em>JMX open types</em> which include basic data types,
072 * {@link javax.management.openmbean.CompositeData CompositeData},
073 * and {@link javax.management.openmbean.TabularData TabularData}
074 * defined in
075 * {@link javax.management.openmbean.OpenType OpenType}.
076 * The mapping is specified below.
077 * </li>
078 * </ol></li>
079 * </ul>
080 *
081 * <h4><a name="MXBean">Platform MXBeans</a></h4>
082 * A platform MXBean is a <i>managed bean</i> that conforms to
083 * the JMX Instrumentation Specification and only uses
084 * a set of basic data types described below.
085 * See <a href="../../../javax/management/MXBean.html#MXBean-spec">
086 * the specification of MXBeans</a> for details.
087 * A JMX management application and the platform <tt>MBeanServer</tt>
088 * can interoperate without requiring classes for MXBean specific
089 * data types.
090 * The data types being transmitted between the JMX connector
091 * server and the connector client are
092 * {@linkplain javax.management.openmbean.OpenType open types}
093 * and this allows interoperation across versions.
094 * <p>
095 * The platform MXBean interfaces use only the following data types:
096 * <ul>
097 * <li>Primitive types such as <tt>int</tt>, <tt>long</tt>,
098 * <tt>boolean</tt>, etc</li>
099 * <li>Wrapper classes for primitive types such as
100 * {@link java.lang.Integer Integer}, {@link java.lang.Long Long},
101 * {@link java.lang.Boolean Boolean}, etc and
102 * {@link java.lang.String String}</li>
103 * <li>{@link java.lang.Enum Enum} classes</li>
104 * <li>Classes that define only getter methods and define a static
105 * <tt>from</tt> method with a
106 * {@link javax.management.openmbean.CompositeData CompositeData}
107 * argument to convert from an input <tt>CompositeData</tt> to
108 * an instance of that class
109 * </li>
110 * <li>{@link java.util.List List<E>}
111 * where <tt>E</tt> is a primitive type, a wrapper class,
112 * an enum class, or a class supporting conversion from a
113 * <tt>CompositeData</tt> to its class
114 * </li>
115 * <li>{@link java.util.Map Map<K,V>}
116 * where <tt>K</tt> and <tt>V</tt> are
117 * a primitive type, a wrapper class,
118 * an enum class, or a class supporting conversion from a
119 * <tt>CompositeData</tt> to its class
120 * </li>
121 * </ul>
122 *
123 * <p>
124 * When an attribute or operation of a platform MXBean
125 * is accessed via an <tt>MBeanServer</tt>, the data types are mapped
126 * as follows:
127 * <ul>
128 * <li>A primitive type or a wrapper class is mapped
129 * to the same type.
130 * </li>
131 * <li>An {@link Enum} is mapped to
132 * <tt>String</tt> whose value is the name of the enum constant.
133 * <li>A class that defines only getter methods and a static
134 * <tt>from</tt> method with a
135 * {@link javax.management.openmbean.CompositeData CompositeData}
136 * argument is mapped to
137 * {@link javax.management.openmbean.CompositeData CompositeData}.
138 * </li>
139 * <li><tt>Map<K,V></tt> is mapped to
140 * {@link javax.management.openmbean.TabularData TabularData}
141 * whose row type is a
142 * {@link javax.management.openmbean.CompositeType CompositeType} with
143 * two items whose names are <i>"key"</i> and <i>"value"</i>
144 * and the item types are
145 * the corresponding mapped type of <tt>K</tt> and <tt>V</tt>
146 * respectively and the <i>"key"</i> is the index.
147 * </li>
148 * <li><tt>List<E></tt> is mapped to an array with the mapped
149 * type of <tt>E</tt> as the element type.
150 * </li>
151 * <li>An array of element type <tt>E</tt> is mapped to
152 * an array of the same dimenions with the mapped type of <tt>E</tt>
153 * as the element type.</li>
154 * </ul>
155 *
156 * The {@link javax.management.MBeanInfo MBeanInfo}
157 * for a platform MXBean
158 * describes the data types of the attributes and operations
159 * as primitive or open types mapped as specified above.
160 *
161 * <p>
162 * For example, the {@link MemoryMXBean}
163 * interface has the following <i>getter</i> and <i>setter</i> methods:
164 *
165 * <blockquote><pre>
166 * public MemoryUsage getHeapMemoryUsage();
167 * public boolean isVerbose();
168 * public void setVerbose(boolean value);
169 * </pre></blockquote>
170 *
171 * These attributes in the <tt>MBeanInfo</tt>
172 * of the <tt>MemoryMXBean</tt> have the following names and types:
173 *
174 * <blockquote>
175 * <table border>
176 * <tr>
177 * <th>Attribute Name</th>
178 * <th>Type</th>
179 * </tr>
180 * <tr>
181 * <td><tt>HeapMemoryUsage</tt></td>
182 * <td>{@link MemoryUsage#from
183 * CompositeData representing MemoryUsage}</td>
184 * </tr>
185 * <tr>
186 * <td><tt>Verbose</tt></td>
187 * <td><tt>boolean</tt></td>
188 * </tr>
189 * </table>
190 * </blockquote>
191 *
192 * <h4><a name="MXBeanNames">MXBean Names</a></h4>
193 * Each platform MXBean for a Java virtual machine has a unique
194 * {@link javax.management.ObjectName ObjectName} for
195 * registration in the platform <tt>MBeanServer</tt>.
196 * A Java virtual machine has a single instance of the following management
197 * interfaces:
198 *
199 * <blockquote>
200 * <table border>
201 * <tr>
202 * <th>Management Interface</th>
203 * <th>ObjectName</th>
204 * </tr>
205 * <tr>
206 * <td> {@link ClassLoadingMXBean} </td>
207 * <td> {@link #CLASS_LOADING_MXBEAN_NAME
208 * <tt>java.lang:type=ClassLoading</tt>}</td>
209 * </tr>
210 * <tr>
211 * <td> {@link MemoryMXBean} </td>
212 * <td> {@link #MEMORY_MXBEAN_NAME
213 * <tt>java.lang:type=Memory</tt>}</td>
214 * </tr>
215 * <tr>
216 * <td> {@link ThreadMXBean} </td>
217 * <td> {@link #THREAD_MXBEAN_NAME
218 * <tt>java.lang:type=Threading</tt>}</td>
219 * </tr>
220 * <tr>
221 * <td> {@link RuntimeMXBean} </td>
222 * <td> {@link #RUNTIME_MXBEAN_NAME
223 * <tt>java.lang:type=Runtime</tt>}</td>
224 * </tr>
225 * <tr>
226 * <td> {@link OperatingSystemMXBean} </td>
227 * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME
228 * <tt>java.lang:type=OperatingSystem</tt>}</td>
229 * </tr>
230 * </table>
231 * </blockquote>
232 *
233 * <p>
234 * A Java virtual machine has zero or a single instance of
235 * the following management interfaces.
236 *
237 * <blockquote>
238 * <table border>
239 * <tr>
240 * <th>Management Interface</th>
241 * <th>ObjectName</th>
242 * </tr>
243 * <tr>
244 * <td> {@link CompilationMXBean} </td>
245 * <td> {@link #COMPILATION_MXBEAN_NAME
246 * <tt>java.lang:type=Compilation</tt>}</td>
247 * </tr>
248 * </table>
249 * </blockquote>
250 *
251 * <p>
252 * A Java virtual machine may have one or more instances of the following
253 * management interfaces.
254 * <blockquote>
255 * <table border>
256 * <tr>
257 * <th>Management Interface</th>
258 * <th>ObjectName</th>
259 * </tr>
260 * <tr>
261 * <td> {@link GarbageCollectorMXBean} </td>
262 * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
263 * <tt>java.lang:type=GarbageCollector</tt>}<tt>,name=</tt><i>collector's name</i></td>
264 * </tr>
265 * <tr>
266 * <td> {@link MemoryManagerMXBean} </td>
267 * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
268 * <tt>java.lang:type=MemoryManager</tt>}<tt>,name=</tt><i>manager's name</i></td>
269 * </tr>
270 * <tr>
271 * <td> {@link MemoryPoolMXBean} </td>
272 * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
273 * <tt>java.lang:type=MemoryPool</tt>}<tt>,name=</tt><i>pool's name</i></td>
274 * </tr>
275 * </table>
276 * </blockquote>
277 *
278 * @see <a href="../../../javax/management/package-summary.html">
279 * JMX Specification.</a>
280 * @see <a href="package-summary.html#examples">
281 * Ways to Access Management Metrics</a>
282 * @see java.util.logging.LoggingMXBean
283 * @see javax.management.MXBean
284 *
285 * @author Mandy Chung
286 * @version 1.31, 05/09/07
287 * @since 1.5
288 */
289 public class ManagementFactory {
290 // A class with only static fields and methods.
291 private ManagementFactory() {
292 };
293
294 /**
295 * String representation of the
296 * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
297 */
298 public final static String CLASS_LOADING_MXBEAN_NAME = "java.lang:type=ClassLoading";
299
300 /**
301 * String representation of the
302 * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
303 */
304 public final static String COMPILATION_MXBEAN_NAME = "java.lang:type=Compilation";
305
306 /**
307 * String representation of the
308 * <tt>ObjectName</tt> for the {@link MemoryMXBean}.
309 */
310 public final static String MEMORY_MXBEAN_NAME = "java.lang:type=Memory";
311
312 /**
313 * String representation of the
314 * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
315 */
316 public final static String OPERATING_SYSTEM_MXBEAN_NAME = "java.lang:type=OperatingSystem";
317
318 /**
319 * String representation of the
320 * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
321 */
322 public final static String RUNTIME_MXBEAN_NAME = "java.lang:type=Runtime";
323
324 /**
325 * String representation of the
326 * <tt>ObjectName</tt> for the {@link ThreadMXBean}.
327 */
328 public final static String THREAD_MXBEAN_NAME = "java.lang:type=Threading";
329
330 /**
331 * The domain name and the type key property in
332 * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
333 * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
334 * can be formed by appending this string with
335 * "<tt>,name=</tt><i>collector's name</i>".
336 */
337 public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE = "java.lang:type=GarbageCollector";
338
339 /**
340 * The domain name and the type key property in
341 * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
342 * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
343 * can be formed by appending this string with
344 * "<tt>,name=</tt><i>manager's name</i>".
345 */
346 public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE = "java.lang:type=MemoryManager";
347
348 /**
349 * The domain name and the type key property in
350 * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
351 * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
352 * can be formed by appending this string with
353 * <tt>,name=</tt><i>pool's name</i>.
354 */
355 public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE = "java.lang:type=MemoryPool";
356
357 /**
358 * Returns the managed bean for the class loading system of
359 * the Java virtual machine.
360 *
361 * @return a {@link ClassLoadingMXBean} object for
362 * the Java virtual machine.
363 */
364 public static ClassLoadingMXBean getClassLoadingMXBean() {
365 return sun.management.ManagementFactory.getClassLoadingMXBean();
366 }
367
368 /**
369 * Returns the managed bean for the memory system of
370 * the Java virtual machine.
371 *
372 * @return a {@link MemoryMXBean} object for the Java virtual machine.
373 */
374 public static MemoryMXBean getMemoryMXBean() {
375 return sun.management.ManagementFactory.getMemoryMXBean();
376 }
377
378 /**
379 * Returns the managed bean for the thread system of
380 * the Java virtual machine.
381 *
382 * @return a {@link ThreadMXBean} object for the Java virtual machine.
383 */
384 public static ThreadMXBean getThreadMXBean() {
385 return sun.management.ManagementFactory.getThreadMXBean();
386 }
387
388 /**
389 * Returns the managed bean for the runtime system of
390 * the Java virtual machine.
391 *
392 * @return a {@link RuntimeMXBean} object for the Java virtual machine.
393
394 */
395 public static RuntimeMXBean getRuntimeMXBean() {
396 return sun.management.ManagementFactory.getRuntimeMXBean();
397 }
398
399 /**
400 * Returns the managed bean for the compilation system of
401 * the Java virtual machine. This method returns <tt>null</tt>
402 * if the Java virtual machine has no compilation system.
403 *
404 * @return a {@link CompilationMXBean} object for the Java virtual
405 * machine or <tt>null</tt> if the Java virtual machine has
406 * no compilation system.
407 */
408 public static CompilationMXBean getCompilationMXBean() {
409 return sun.management.ManagementFactory.getCompilationMXBean();
410 }
411
412 /**
413 * Returns the managed bean for the operating system on which
414 * the Java virtual machine is running.
415 *
416 * @return an {@link OperatingSystemMXBean} object for
417 * the Java virtual machine.
418 */
419 public static OperatingSystemMXBean getOperatingSystemMXBean() {
420 return sun.management.ManagementFactory
421 .getOperatingSystemMXBean();
422 }
423
424 /**
425 * Returns a list of {@link MemoryPoolMXBean} objects in the
426 * Java virtual machine.
427 * The Java virtual machine can have one or more memory pools.
428 * It may add or remove memory pools during execution.
429 *
430 * @return a list of <tt>MemoryPoolMXBean</tt> objects.
431 *
432 */
433 public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
434 return sun.management.ManagementFactory.getMemoryPoolMXBeans();
435 }
436
437 /**
438 * Returns a list of {@link MemoryManagerMXBean} objects
439 * in the Java virtual machine.
440 * The Java virtual machine can have one or more memory managers.
441 * It may add or remove memory managers during execution.
442 *
443 * @return a list of <tt>MemoryManagerMXBean</tt> objects.
444 *
445 */
446 public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
447 return sun.management.ManagementFactory
448 .getMemoryManagerMXBeans();
449 }
450
451 /**
452 * Returns a list of {@link GarbageCollectorMXBean} objects
453 * in the Java virtual machine.
454 * The Java virtual machine may have one or more
455 * <tt>GarbageCollectorMXBean</tt> objects.
456 * It may add or remove <tt>GarbageCollectorMXBean</tt>
457 * during execution.
458 *
459 * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
460 *
461 */
462 public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
463 return sun.management.ManagementFactory
464 .getGarbageCollectorMXBeans();
465 }
466
467 private static MBeanServer platformMBeanServer;
468
469 /**
470 * Returns the platform {@link javax.management.MBeanServer MBeanServer}.
471 * On the first call to this method, it first creates the platform
472 * <tt>MBeanServer</tt> by calling the
473 * {@link javax.management.MBeanServerFactory#createMBeanServer
474 * MBeanServerFactory.createMBeanServer}
475 * method and registers the platform MXBeans in this platform
476 * <tt>MBeanServer</tt> using the <a href="#MXBeanNames">MXBean names</a>
477 * defined in the class description.
478 * This method, in subsequent calls, will simply return the
479 * initially created platform <tt>MBeanServer</tt>.
480 * <p>
481 * MXBeans that get created and destroyed dynamically, for example,
482 * memory {@link MemoryPoolMXBean pools} and
483 * {@link MemoryManagerMXBean managers},
484 * will automatically be registered and deregistered into the platform
485 * <tt>MBeanServer</tt>.
486 * <p>
487 * If the system property <tt>javax.management.builder.initial</tt>
488 * is set, the platform <tt>MBeanServer</tt> creation will be done
489 * by the specified {@link javax.management.MBeanServerBuilder}.
490 * <p>
491 * It is recommended that this platform MBeanServer also be used
492 * to register other application managed beans
493 * besides the platform MXBeans.
494 * This will allow all MBeans to be published through the same
495 * <tt>MBeanServer</tt> and hence allow for easier network publishing
496 * and discovery.
497 * Name conflicts with the platform MXBeans should be avoided.
498 *
499 * @return the platform <tt>MBeanServer</tt>; the platform
500 * MXBeans are registered into the platform <tt>MBeanServer</tt>
501 * at the first time this method is called.
502 *
503 * @exception SecurityException if there is a security manager
504 * and the caller does not have the permission required by
505 * {@link javax.management.MBeanServerFactory#createMBeanServer}.
506 *
507 * @see javax.management.MBeanServerFactory
508 * @see javax.management.MBeanServerFactory#createMBeanServer
509 */
510 public static synchronized MBeanServer getPlatformMBeanServer() {
511 SecurityManager sm = System.getSecurityManager();
512 if (sm != null) {
513 Permission perm = new MBeanServerPermission(
514 "createMBeanServer");
515 sm.checkPermission(perm);
516 }
517
518 if (platformMBeanServer == null) {
519 platformMBeanServer = sun.management.ManagementFactory
520 .createPlatformMBeanServer();
521 }
522 return platformMBeanServer;
523 }
524
525 /**
526 * Returns a proxy for a platform MXBean interface of a
527 * given <a href="#MXBeanNames">MXBean name</a>
528 * that forwards its method calls through the given
529 * <tt>MBeanServerConnection</tt>.
530 *
531 * <p>This method is equivalent to:
532 * <blockquote>
533 * {@link java.lang.reflect.Proxy#newProxyInstance
534 * Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
535 * new Class[] { mxbeanInterface }, handler)</tt>
536 * </blockquote>
537 *
538 * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
539 * InvocationHandler} to which method invocations to the MXBean interface
540 * are dispatched. This <tt>handler</tt> converts an input parameter
541 * from an MXBean data type to its mapped open type before forwarding
542 * to the <tt>MBeanServer</tt> and converts a return value from
543 * an MXBean method call through the <tt>MBeanServer</tt>
544 * from an open type to the corresponding return type declared in
545 * the MXBean interface.
546 *
547 * <p>
548 * If the MXBean is a notification emitter (i.e.,
549 * it implements
550 * {@link javax.management.NotificationEmitter NotificationEmitter}),
551 * both the <tt>mxbeanInterface</tt> and <tt>NotificationEmitter</tt>
552 * will be implemented by this proxy.
553 *
554 * <p>
555 * <b>Notes:</b>
556 * <ol>
557 * <li>Using an MXBean proxy is a convenience remote access to
558 * a platform MXBean of a running virtual machine. All method
559 * calls to the MXBean proxy are forwarded to an
560 * <tt>MBeanServerConnection</tt> where
561 * {@link java.io.IOException IOException} may be thrown
562 * when the communication problem occurs with the connector server.
563 * An application remotely accesses the platform MXBeans using
564 * proxy should prepare to catch <tt>IOException</tt> as if
565 * accessing with the <tt>MBeanServerConnector</tt> interface.</li>
566 *
567 * <li>When a client application is designed to remotely access MXBeans
568 * for a running virtual machine whose version is different than
569 * the version on which the application is running,
570 * it should prepare to catch
571 * {@link java.io.InvalidObjectException InvalidObjectException}
572 * which is thrown when an MXBean proxy receives a name of an
573 * enum constant which is missing in the enum class loaded in
574 * the client application. </li>
575 *
576 * <li>{@link javax.management.MBeanServerInvocationHandler
577 * MBeanServerInvocationHandler} or its
578 * {@link javax.management.MBeanServerInvocationHandler#newProxyInstance
579 * newProxyInstance} method cannot be used to create
580 * a proxy for a platform MXBean. The proxy object created
581 * by <tt>MBeanServerInvocationHandler</tt> does not handle
582 * the properties of the platform MXBeans described in
583 * the <a href="#MXBean">class specification</a>.
584 *</li>
585 * </ol>
586 *
587 * @param connection the <tt>MBeanServerConnection</tt> to forward to.
588 * @param mxbeanName the name of a platform MXBean within
589 * <tt>connection</tt> to forward to. <tt>mxbeanName</tt> must be
590 * in the format of {@link ObjectName ObjectName}.
591 * @param mxbeanInterface the MXBean interface to be implemented
592 * by the proxy.
593 *
594 * @throws IllegalArgumentException if
595 * <ul>
596 * <li><tt>mxbeanName</tt> is not with a valid
597 * {@link ObjectName ObjectName} format, or</li>
598 * <li>the named MXBean in the <tt>connection</tt> is
599 * not a MXBean provided by the platform, or</li>
600 * <li>the named MXBean is not registered in the
601 * <tt>MBeanServerConnection</tt>, or</li>
602 * <li>the named MXBean is not an instance of the given
603 * <tt>mxbeanInterface</tt></li>
604 * </ul>
605 *
606 * @throws java.io.IOException if a communication problem
607 * occurred when accessing the <tt>MBeanServerConnection</tt>.
608 */
609 public static <T> T newPlatformMXBeanProxy(
610 MBeanServerConnection connection, String mxbeanName,
611 Class<T> mxbeanInterface) throws java.io.IOException {
612
613 final Class interfaceClass = mxbeanInterface;
614 // Only allow MXBean interfaces from rt.jar loaded by the
615 // bootstrap class loader
616 final ClassLoader loader = AccessController
617 .doPrivileged(new PrivilegedAction<ClassLoader>() {
618 public ClassLoader run() {
619 return interfaceClass.getClassLoader();
620 }
621 });
622 if (loader != null) {
623 throw new IllegalArgumentException(mxbeanName
624 + " is not a platform MXBean");
625 }
626
627 try {
628 final ObjectName objName = new ObjectName(mxbeanName);
629 if (!connection.isInstanceOf(objName, interfaceClass
630 .getName())) {
631 throw new IllegalArgumentException(mxbeanName
632 + " is not an instance of " + interfaceClass);
633 }
634
635 final Class[] interfaces;
636 // check if the registered MBean is a notification emitter
637 boolean emitter = connection.isInstanceOf(objName,
638 NOTIF_EMITTER);
639
640 // create an MXBean proxy
641 return JMX.newMXBeanProxy(connection, objName,
642 mxbeanInterface, emitter);
643 } catch (InstanceNotFoundException e) {
644 final IllegalArgumentException iae = new IllegalArgumentException(
645 mxbeanName + " not found in the connection.");
646 iae.initCause(e);
647 throw iae;
648 } catch (MalformedObjectNameException e) {
649 final IllegalArgumentException iae = new IllegalArgumentException(
650 mxbeanName + " is not a valid ObjectName format.");
651 iae.initCause(e);
652 throw iae;
653 }
654 }
655
656 private static final String NOTIF_EMITTER = "javax.management.NotificationEmitter";
657 }
|