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