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. package java.lang.management;
  8. import javax.management.NotificationEmitter;
  9. import javax.management.MBeanServer;
  10. import javax.management.MBeanServerFactory;
  11. import javax.management.MBeanServerConnection;
  12. import javax.management.MBeanServerPermission;
  13. import javax.management.ObjectName;
  14. import javax.management.InstanceNotFoundException;
  15. import javax.management.MalformedObjectNameException;
  16. import javax.management.openmbean.CompositeData;
  17. import javax.management.openmbean.CompositeType;
  18. import javax.management.openmbean.TabularData;
  19. import java.util.List;
  20. import java.lang.reflect.Proxy;
  21. import java.lang.reflect.InvocationHandler;
  22. import java.security.AccessController;
  23. import java.security.Permission;
  24. import java.security.PrivilegedAction;
  25. import sun.management.PlatformMXBeanInvocationHandler;
  26. /**
  27. * The <tt>ManagementFactory</tt> class is a factory class for getting
  28. * managed beans for the Java platform.
  29. * This class consists of static methods each of which returns
  30. * one or more <a href="#MXBean">platform MXBean(s)</a> representing
  31. * the management interface of a component of the Java virtual
  32. * machine.
  33. *
  34. * <p>
  35. * An application can access a platform MXBean in the following ways:
  36. * <ul>
  37. * <li><i>Direct access to an MXBean interface</i>
  38. * <ol type="a">
  39. * <li>Get the MXBean instance through the static factory method
  40. * and access the MXBean locally of the running
  41. * virtual machine.
  42. * </li>
  43. * <li>Construct an MXBean proxy instance that forwards the
  44. * method calls to a given {@link MBeanServer MBeanServer} by calling
  45. * {@link #newPlatformMXBeanProxy newPlatfromMXBeanProxy}.
  46. * A proxy is typically constructed to remotely access
  47. * an MXBean of another running virtual machine.
  48. * </li>
  49. * </ol></li>
  50. * <li><i>Indirect access to an MXBean interface via MBeanServer</i>
  51. * <ol type="a">
  52. * <li>Go through the {@link #getPlatformMBeanServer
  53. * platform MBeanServer} to access MXBeans locally or
  54. * a specific <tt>MBeanServerConnection</tt> to access
  55. * MXBeans remotely.
  56. * The attributes and operations of an MXBean use only
  57. * <em>JMX open types</em> which include basic
  58. * data types, {@link CompositeData CompositeData},
  59. * and {@link TabularData TabularData} defined in
  60. * {@link javax.management.openmbean.OpenType OpenType}.
  61. * The mapping is specified below.
  62. * </li>
  63. * </ol></li>
  64. * </ul>
  65. *
  66. * <h4><a name="MXBean">Platform MXBeans</a></h4>
  67. * A platform MXBean is a <i>managed bean</i> that conforms to
  68. * the JMX Instrumentation Specification and only uses
  69. * a set of basic data types described below.
  70. * A JMX management application and the platform <tt>MBeanServer</tt>
  71. * can interoperate without requiring classes for MXBean specific
  72. * data types.
  73. * The data types being transmitted between the JMX connector
  74. * server and the connector client are
  75. * {@link javax.management.openmbean.OpenType open types}
  76. * and this allows interoperation across versions.
  77. * <p>
  78. * The platform MXBean interfaces use only the following data types:
  79. * <ul>
  80. * <li>Primitive types such as <tt>int</tt>, <tt>long</tt>,
  81. * <tt>boolean</tt>, etc</li>
  82. * <li>Wrapper classes for primitive types such as
  83. * {@link java.lang.Integer Integer}, {@link java.lang.Long Long},
  84. * {@link java.lang.Boolean Boolean}, etc and
  85. * {@link java.lang.String String}</li>
  86. * <li>{@link java.lang.Enum Enum} classes</li>
  87. * <li>Classes that define only getter methods and define a static
  88. * <tt>from</tt> method with a {@link CompositeData CompositeData}
  89. * argument to convert from an input <tt>CompositeData</tt> to
  90. * an instance of that class
  91. * </li>
  92. * <li>{@link java.util.List List<E>}
  93. * where <tt>E</tt> is a primitive type, a wrapper class,
  94. * an enum class, or a class supporting conversion from a
  95. * <tt>CompositeData</tt> to its class
  96. * </li>
  97. * <li>{@link java.util.Map Map<K,V>}
  98. * where <tt>K</tt> and <tt>V</tt> are
  99. * a primitive type, a wrapper class,
  100. * an enum class, or a class supporting conversion from a
  101. * <tt>CompositeData</tt> to its class
  102. * </li>
  103. * </ul>
  104. *
  105. * <p>
  106. * When an attribute or operation of a platform MXBean
  107. * is accessed via an <tt>MBeanServer</tt>, the data types are mapped
  108. * as follows:
  109. * <ul>
  110. * <li>A primitive type or a wrapper class is mapped
  111. * to the same type.
  112. * </li>
  113. * <li>An {@link Enum} is mapped to
  114. * <tt>String</tt> whose value is the name of the enum constant.
  115. * <li>A class that defines only getter methods and a static
  116. * <tt>from</tt> method with a {@link CompositeData CompositeData}
  117. * argument is mapped to
  118. * {@link javax.management.openmbean.CompositeData CompositeData}.
  119. * </li>
  120. * <li><tt>Map<K,V></tt> is mapped to
  121. * {@link TabularData TabularData}
  122. * whose row type is a {@link CompositeType CompositeType} with
  123. * two items whose names are <i>"key"</i> and <i>"value"</i>
  124. * and the item types are
  125. * the corresponding mapped type of <tt>K</tt> and <tt>V</tt>
  126. * respectively and the <i>"key"</i> is the index.
  127. * </li>
  128. * <li><tt>List<E></tt> is mapped to an array with the mapped
  129. * type of <tt>E</tt> as the element type.
  130. * </li>
  131. * <li>An array of element type <tt>E</tt> is mapped to
  132. * an array of the same dimenions with the mapped type of <tt>E</tt>
  133. * as the element type.</li>
  134. * </ul>
  135. *
  136. * The {@link javax.management.MBeanInfo MBeanInfo}
  137. * for a platform MXBean
  138. * describes the data types of the attributes and operations
  139. * as primitive or open types mapped as specified above.
  140. *
  141. * <p>
  142. * For example, the {@link MemoryMXBean}
  143. * interface has the following <i>getter</i> and <i>setter</i> methods:
  144. *
  145. * <blockquote><pre>
  146. * public MemoryUsage getHeapMemoryUsage();
  147. * public boolean isVerbose();
  148. * public void setVerbose(boolean value);
  149. * </pre></blockquote>
  150. *
  151. * These attributes in the <tt>MBeanInfo</tt>
  152. * of the <tt>MemoryMXBean</tt> have the following names and types:
  153. *
  154. * <blockquote>
  155. * <table border>
  156. * <tr>
  157. * <th>Attribute Name</th>
  158. * <th>Type</th>
  159. * </tr>
  160. * <tr>
  161. * <td><tt>HeapMemoryUsage</tt></td>
  162. * <td>{@link MemoryUsage#from
  163. * CompositeData representing MemoryUsage}</td>
  164. * </tr>
  165. * <tr>
  166. * <td><tt>Verbose</tt></td>
  167. * <td><tt>boolean</tt></td>
  168. * </tr>
  169. * </table>
  170. * </blockquote>
  171. *
  172. * <p>
  173. * <b>Implementation Note:</b><p>
  174. * The mapping specified above could be done in the implementation
  175. * of each platform MXBean. One good implementation choice is to
  176. * implement a MXBean as a {@link javax.management.DynamicMBean dynamic
  177. * MBean}.
  178. *
  179. *
  180. * <h4><a name="MXBeanNames">MXBean Names</a></h4>
  181. * Each platform MXBean for a Java virtual machine has a unique
  182. * {@link javax.management.ObjectName ObjectName} for
  183. * registration in the platform <tt>MBeanServer</tt>.
  184. * A Java virtual machine has a single instance of the following management
  185. * interfaces:
  186. *
  187. * <blockquote>
  188. * <table border>
  189. * <tr>
  190. * <th>Management Interface</th>
  191. * <th>ObjectName</th>
  192. * </tr>
  193. * <tr>
  194. * <td> {@link ClassLoadingMXBean} </td>
  195. * <td> {@link #CLASS_LOADING_MXBEAN_NAME
  196. * <tt>java.lang:type=ClassLoading</tt>}</td>
  197. * </tr>
  198. * <tr>
  199. * <td> {@link MemoryMXBean} </td>
  200. * <td> {@link #MEMORY_MXBEAN_NAME
  201. * <tt>java.lang:type=Memory</tt>}</td>
  202. * </tr>
  203. * <tr>
  204. * <td> {@link ThreadMXBean} </td>
  205. * <td> {@link #THREAD_MXBEAN_NAME
  206. * <tt>java.lang:type=Threading</tt>}</td>
  207. * </tr>
  208. * <tr>
  209. * <td> {@link RuntimeMXBean} </td>
  210. * <td> {@link #RUNTIME_MXBEAN_NAME
  211. * <tt>java.lang:type=Runtime</tt>}</td>
  212. * </tr>
  213. * <tr>
  214. * <td> {@link OperatingSystemMXBean} </td>
  215. * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME
  216. * <tt>java.lang:type=OperatingSystem</tt>}</td>
  217. * </tr>
  218. * </table>
  219. * </blockquote>
  220. *
  221. * <p>
  222. * A Java virtual machine has zero or a single instance of
  223. * the following management interfaces.
  224. *
  225. * <blockquote>
  226. * <table border>
  227. * <tr>
  228. * <th>Management Interface</th>
  229. * <th>ObjectName</th>
  230. * </tr>
  231. * <tr>
  232. * <td> {@link CompilationMXBean} </td>
  233. * <td> {@link #COMPILATION_MXBEAN_NAME
  234. * <tt>java.lang:type=Compilation</tt>}</td>
  235. * </tr>
  236. * </table>
  237. * </blockquote>
  238. *
  239. * <p>
  240. * A Java virtual machine may have one or more instances of the following
  241. * management interfaces.
  242. * <blockquote>
  243. * <table border>
  244. * <tr>
  245. * <th>Management Interface</th>
  246. * <th>ObjectName</th>
  247. * </tr>
  248. * <tr>
  249. * <td> {@link GarbageCollectorMXBean} </td>
  250. * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
  251. * <tt>java.lang:type=GarbageCollector</tt>}<tt>,name=</tt><i>collector's name</i></td>
  252. * </tr>
  253. * <tr>
  254. * <td> {@link MemoryManagerMXBean} </td>
  255. * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
  256. * <tt>java.lang:type=MemoryManager</tt>}<tt>,name=</tt><i>manager's name</i></td>
  257. * </tr>
  258. * <tr>
  259. * <td> {@link MemoryPoolMXBean} </td>
  260. * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
  261. * <tt>java.lang:type=MemoryPool</tt>}<tt>,name=</tt><i>pool's name</i></td>
  262. * </tr>
  263. * </table>
  264. * </blockquote>
  265. *
  266. * @see <a href="../../../javax/management/package-summary.html">
  267. * JMX Specification.</a>
  268. * @see <a href="package-summary.html#examples">
  269. * Ways to Access Management Metrics</a>
  270. * @see java.util.logging.LoggingMXBean
  271. *
  272. * @author Mandy Chung
  273. * @version 1.19, 05/17/04
  274. * @since 1.5
  275. */
  276. public class ManagementFactory {
  277. // A class with only static fields and methods.
  278. private ManagementFactory() {};
  279. /**
  280. * String representation of the
  281. * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
  282. */
  283. public final static String CLASS_LOADING_MXBEAN_NAME =
  284. "java.lang:type=ClassLoading";
  285. /**
  286. * String representation of the
  287. * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
  288. */
  289. public final static String COMPILATION_MXBEAN_NAME =
  290. "java.lang:type=Compilation";
  291. /**
  292. * String representation of the
  293. * <tt>ObjectName</tt> for the {@link MemoryMXBean}.
  294. */
  295. public final static String MEMORY_MXBEAN_NAME =
  296. "java.lang:type=Memory";
  297. /**
  298. * String representation of the
  299. * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
  300. */
  301. public final static String OPERATING_SYSTEM_MXBEAN_NAME =
  302. "java.lang:type=OperatingSystem";
  303. /**
  304. * String representation of the
  305. * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
  306. */
  307. public final static String RUNTIME_MXBEAN_NAME =
  308. "java.lang:type=Runtime";
  309. /**
  310. * String representation of the
  311. * <tt>ObjectName</tt> for the {@link ThreadMXBean}.
  312. */
  313. public final static String THREAD_MXBEAN_NAME =
  314. "java.lang:type=Threading";
  315. /**
  316. * The domain name and the type key property in
  317. * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
  318. * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
  319. * can be formed by appending this string with
  320. * "<tt>,name=</tt><i>collector's name</i>".
  321. */
  322. public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
  323. "java.lang:type=GarbageCollector";
  324. /**
  325. * The domain name and the type key property in
  326. * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
  327. * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
  328. * can be formed by appending this string with
  329. * "<tt>,name=</tt><i>manager's name</i>".
  330. */
  331. public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
  332. "java.lang:type=MemoryManager";
  333. /**
  334. * The domain name and the type key property in
  335. * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
  336. * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
  337. * can be formed by appending this string with
  338. * <tt>,name=</tt><i>pool's name</i>.
  339. */
  340. public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
  341. "java.lang:type=MemoryPool";
  342. /**
  343. * Returns the managed bean for the class loading system of
  344. * the Java virtual machine.
  345. *
  346. * @return a {@link ClassLoadingMXBean} object for
  347. * the Java virtual machine.
  348. */
  349. public static ClassLoadingMXBean getClassLoadingMXBean() {
  350. return sun.management.ManagementFactory.getClassLoadingMXBean();
  351. }
  352. /**
  353. * Returns the managed bean for the memory system of
  354. * the Java virtual machine.
  355. *
  356. * @return a {@link MemoryMXBean} object for the Java virtual machine.
  357. */
  358. public static MemoryMXBean getMemoryMXBean() {
  359. return sun.management.ManagementFactory.getMemoryMXBean();
  360. }
  361. /**
  362. * Returns the managed bean for the thread system of
  363. * the Java virtual machine.
  364. *
  365. * @return a {@link ThreadMXBean} object for the Java virtual machine.
  366. */
  367. public static ThreadMXBean getThreadMXBean() {
  368. return sun.management.ManagementFactory.getThreadMXBean();
  369. }
  370. /**
  371. * Returns the managed bean for the runtime system of
  372. * the Java virtual machine.
  373. *
  374. * @return a {@link RuntimeMXBean} object for the Java virtual machine.
  375. */
  376. public static RuntimeMXBean getRuntimeMXBean() {
  377. return sun.management.ManagementFactory.getRuntimeMXBean();
  378. }
  379. /**
  380. * Returns the managed bean for the compilation system of
  381. * the Java virtual machine. This method returns <tt>null</tt>
  382. * if the Java virtual machine has no compilation system.
  383. *
  384. * @return a {@link CompilationMXBean} object for the Java virtual
  385. * machine or <tt>null</tt> if the Java virtual machine has
  386. * no compilation system.
  387. */
  388. public static CompilationMXBean getCompilationMXBean() {
  389. return sun.management.ManagementFactory.getCompilationMXBean();
  390. }
  391. /**
  392. * Returns the managed bean for the operating system on which
  393. * the Java virtual machine is running.
  394. *
  395. * @return an {@link OperatingSystemMXBean} object for
  396. * the Java virtual machine.
  397. */
  398. public static OperatingSystemMXBean getOperatingSystemMXBean() {
  399. return sun.management.ManagementFactory.getOperatingSystemMXBean();
  400. }
  401. /**
  402. * Returns a list of {@link MemoryPoolMXBean} objects in the
  403. * Java virtual machine.
  404. * The Java virtual machine can have one or more memory pools.
  405. * It may add or remove memory pools during execution.
  406. *
  407. * @return a list of <tt>MemoryPoolMXBean</tt> objects.
  408. *
  409. */
  410. public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
  411. return sun.management.ManagementFactory.getMemoryPoolMXBeans();
  412. }
  413. /**
  414. * Returns a list of {@link MemoryManagerMXBean} objects
  415. * in the Java virtual machine.
  416. * The Java virtual machine can have one or more memory managers.
  417. * It may add or remove memory managers during execution.
  418. *
  419. * @return a list of <tt>MemoryManagerMXBean</tt> objects.
  420. *
  421. */
  422. public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
  423. return sun.management.ManagementFactory.getMemoryManagerMXBeans();
  424. }
  425. /**
  426. * Returns a list of {@link GarbageCollectorMXBean} objects
  427. * in the Java virtual machine.
  428. * The Java virtual machine may have one or more
  429. * <tt>GarbageCollectorMXBean</tt> objects.
  430. * It may add or remove <tt>GarbageCollectorMXBean</tt>
  431. * during execution.
  432. *
  433. * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
  434. *
  435. */
  436. public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
  437. return sun.management.ManagementFactory.getGarbageCollectorMXBeans();
  438. }
  439. private static MBeanServer platformMBeanServer;
  440. /**
  441. * Returns the platform {@link javax.management.MBeanServer MBeanServer}.
  442. * On the first call to this method, it first creates the platform
  443. * <tt>MBeanServer</tt> by calling the
  444. * {@link MBeanServerFactory#createMBeanServer}
  445. * method and registers the platform MXBeans in this platform
  446. * <tt>MBeanServer</tt> using the <a href="#MXBeanNames">MXBean names</a>
  447. * defined in the class description.
  448. * This method, in subsequent calls, will simply return the
  449. * initially created platform <tt>MBeanServer</tt>.
  450. * <p>
  451. * MXBeans that get created and destroyed dynamically, for example,
  452. * memory {@link MemoryPoolMXBean pools} and
  453. * {@link MemoryManagerMXBean managers},
  454. * will automatically be registered and deregistered into the platform
  455. * <tt>MBeanServer</tt>.
  456. * <p>
  457. * If the system property <tt>javax.management.builder.initial</tt>
  458. * is set, the platform <tt>MBeanServer</tt> creation will be done
  459. * by the specified {@link javax.management.MBeanServerBuilder}.
  460. * <p>
  461. * It is recommended that this platform MBeanServer also be used
  462. * to register other application managed beans
  463. * besides the platform MXBeans.
  464. * This will allow all MBeans to be published through the same
  465. * <tt>MBeanServer</tt> and hence allow for easier network publishing
  466. * and discovery.
  467. * Name conflicts with the platform MXBeans should be avoided.
  468. *
  469. * @return the platform <tt>MBeanServer</tt> the platform
  470. * MXBeans are registered into the platform <tt>MBeanServer</tt>
  471. * at the first time this method is called.
  472. *
  473. * @exception SecurityException if there is a security manager
  474. * and the caller does not have the permission required by
  475. * {@link javax.management.MBeanServerFactory#createMBeanServer}.
  476. *
  477. * @see javax.management.MBeanServerFactory
  478. * @see javax.management.MBeanServerFactory#createMBeanServer
  479. */
  480. public static synchronized MBeanServer getPlatformMBeanServer() {
  481. SecurityManager sm = System.getSecurityManager();
  482. if (sm != null) {
  483. Permission perm = new MBeanServerPermission("createMBeanServer");
  484. sm.checkPermission(perm);
  485. }
  486. if (platformMBeanServer == null) {
  487. platformMBeanServer =
  488. sun.management.ManagementFactory.createPlatformMBeanServer();
  489. }
  490. return platformMBeanServer;
  491. }
  492. /**
  493. * Returns a proxy for a platform MXBean interface of a
  494. * given <a href="#MXBeanNames">MXBean name</a>
  495. * that forwards its method calls through the given
  496. * <tt>MBeanServerConnection</tt>.
  497. *
  498. * <p>This method is equivalent to:
  499. * <blockquote>
  500. * {@link java.lang.reflect.Proxy#newProxyInstance
  501. * Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
  502. * new Class[] { mxbeanInterface }, handler)</tt>
  503. * </blockquote>
  504. *
  505. * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
  506. * InvocationHandler} to which method invocations to the MXBean interface
  507. * are dispatched. This <tt>handler</tt> converts an input parameter
  508. * from an MXBean data type to its mapped open type before forwarding
  509. * to the <tt>MBeanServer</tt> and converts a return value from
  510. * an MXBean method call through the <tt>MBeanServer</tt>
  511. * from an open type to the corresponding return type declared in
  512. * the MXBean interface.
  513. *
  514. * <p>
  515. * If the MXBean is a notification emitter (i.e.,
  516. * it implements {@link NotificationEmitter NotificationEmitter}),
  517. * both the <tt>mxbeanInterface</tt> and <tt>NotificationEmitter</tt>
  518. * will be implemented by this proxy.
  519. *
  520. * <p>
  521. * <b>Notes:</b>
  522. * <ol>
  523. * <li>Using an MXBean proxy is a convenience remote access to
  524. * a platform MXBean of a running virtual machine. All method
  525. * calls to the MXBean proxy are forwarded to an
  526. * <tt>MBeanServerConnection</tt> where
  527. * {@link java.io.IOException IOException} may be thrown
  528. * when the communication problem occurs with the connector server.
  529. * An application remotely accesses the platform MXBeans using
  530. * proxy should prepare to catch <tt>IOException</tt> as if
  531. * accessing with the <tt>MBeanServerConnector</tt> interface.</li>
  532. *
  533. * <li>When a client application is designed to remotely access MXBeans
  534. * for a running virtual machine whose version is different than
  535. * the version on which the application is running,
  536. * it should prepare to catch
  537. * {@link java.io.InvalidObjectException InvalidObjectException}
  538. * which is thrown when an MXBean proxy receives a name of an
  539. * enum constant which is missing in the enum class loaded in
  540. * the client application. </li>
  541. *
  542. * <li>{@link javax.management.MBeanServerInvocationHandler
  543. * MBeanServerInvocationHandler} or its
  544. * {@link javax.management.MBeanServerInvocationHandler#newProxyInstance
  545. * newProxyInstance} method cannot be used to create
  546. * a proxy for a platform MXBean. The proxy object created
  547. * by <tt>MBeanServerInvocationHandler</tt> does not handle
  548. * the properties of the platform MXBeans described in
  549. * the <a href="#MXBean">class specification</a>.
  550. *</li>
  551. * </ol>
  552. *
  553. * @param connection the <tt>MBeanServerConnection</tt> to forward to.
  554. * @param mxbeanName the name of a platform MXBean within
  555. * <tt>connection</tt> to forward to. <tt>mxbeanName</tt> must be
  556. * in the format of {@link ObjectName ObjectName}.
  557. * @param mxbeanInterface the MXBean interface to be implemented
  558. * by the proxy.
  559. *
  560. * @throws IllegalArgumentException if
  561. * <ul>
  562. * <li><tt>mxbeanName</tt> is not with a valid
  563. * {@link ObjectName ObjectName} format, or</li>
  564. * <li>the named MXBean in the <tt>connection</tt> is
  565. * not a MXBean provided by the platform, or</li>
  566. * <li>the named MXBean is not registered in the
  567. * <tt>MBeanServerConnection</tt>, or</li>
  568. * <li>the named MXBean is not an instance of the given
  569. * <tt>mxbeanInterface</tt></li>
  570. * </ul>
  571. *
  572. * @throws java.io.IOException if a communication problem
  573. * occurred when accessing the <tt>MBeanServerConnection</tt>.
  574. */
  575. public static <T> T
  576. newPlatformMXBeanProxy(MBeanServerConnection connection,
  577. String mxbeanName,
  578. Class<T> mxbeanInterface)
  579. throws java.io.IOException {
  580. final Class interfaceClass = mxbeanInterface;
  581. // Only allow MXBean interfaces from rt.jar loaded by the
  582. // bootstrap class loader
  583. final ClassLoader loader = (ClassLoader)
  584. AccessController.doPrivileged(new PrivilegedAction() {
  585. public Object run() {
  586. return interfaceClass.getClassLoader();
  587. }
  588. });
  589. if (loader != null) {
  590. throw new IllegalArgumentException(mxbeanName +
  591. " is not a platform MXBean");
  592. }
  593. try {
  594. final ObjectName objName = new ObjectName(mxbeanName);
  595. if (!connection.isInstanceOf(objName, interfaceClass.getName())) {
  596. throw new IllegalArgumentException(mxbeanName +
  597. " is not an instance of " + interfaceClass);
  598. }
  599. final Class[] interfaces;
  600. // check if the registered MBean is a notification emitter
  601. if (connection.isInstanceOf(objName,
  602. NOTIF_EMITTER)) {
  603. interfaces = new Class[] {
  604. interfaceClass,
  605. NotificationEmitter.class
  606. };
  607. } else {
  608. interfaces = new Class[] {interfaceClass};
  609. }
  610. // create a MXBean proxy
  611. InvocationHandler handler =
  612. new PlatformMXBeanInvocationHandler(connection,
  613. objName,
  614. interfaceClass);
  615. return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
  616. interfaces,
  617. handler);
  618. } catch (InstanceNotFoundException e) {
  619. final IllegalArgumentException iae =
  620. new IllegalArgumentException(mxbeanName +
  621. " not found in the connection.");
  622. iae.initCause(e);
  623. throw iae;
  624. } catch (MalformedObjectNameException e) {
  625. final IllegalArgumentException iae =
  626. new IllegalArgumentException(mxbeanName +
  627. " is not a valid ObjectName format.");
  628. iae.initCause(e);
  629. throw iae;
  630. }
  631. }
  632. private static final String NOTIF_EMITTER =
  633. "javax.management.NotificationEmitter";
  634. }