Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/lang/management/ManagementFactory.java
+++ new/src/share/classes/java/lang/management/ManagementFactory.java
1 1 /*
2 - * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package java.lang.management;
27 27 import javax.management.DynamicMBean;
28 28 import javax.management.MBeanServer;
29 29 import javax.management.MBeanServerConnection;
30 30 import javax.management.MBeanServerFactory;
31 31 import javax.management.MBeanServerPermission;
32 +import javax.management.MXBean;
32 33 import javax.management.NotificationEmitter;
33 34 import javax.management.ObjectInstance;
34 35 import javax.management.ObjectName;
35 36 import javax.management.InstanceAlreadyExistsException;
36 37 import javax.management.InstanceNotFoundException;
37 38 import javax.management.MalformedObjectNameException;
38 39 import javax.management.MBeanRegistrationException;
39 40 import javax.management.NotCompliantMBeanException;
40 41 import javax.management.StandardEmitterMBean;
41 42 import javax.management.StandardMBean;
42 43 import java.util.Collections;
43 44 import java.util.List;
44 45 import java.util.Set;
46 +import java.util.HashMap;
45 47 import java.util.HashSet;
46 48 import java.security.AccessController;
47 49 import java.security.Permission;
48 50 import java.security.PrivilegedAction;
49 51 import java.security.PrivilegedActionException;
50 52 import java.security.PrivilegedExceptionAction;
51 53 import javax.management.JMX;
52 54 import sun.management.ManagementFactoryHelper;
53 55
54 56 /**
55 57 * The {@code ManagementFactory} class is a factory class for getting
56 58 * managed beans for the Java platform.
57 59 * This class consists of static methods each of which returns
58 60 * one or more <i>platform MXBeans</i> representing
59 61 * the management interface of a component of the Java virtual
60 62 * machine.
61 63 * <p>
62 64 * <h4><a name="MXBean">Platform MXBeans</a></h4>
63 65 * <p>
64 66 * A platform MXBean is a <i>managed bean</i> that
65 67 * conforms to the <a href="../../../javax/management/package-summary.html">JMX</a>
66 68 * Instrumentation Specification and only uses a set of basic data types.
67 69 * A JMX management application and the {@linkplain
68 70 * #getPlatformMBeanServer platform MBeanServer}
69 71 * can interoperate without requiring classes for MXBean specific
70 72 * data types.
71 73 * The data types being transmitted between the JMX connector
72 74 * server and the connector client are
73 75 * {@linkplain javax.management.openmbean.OpenType open types}
74 76 * and this allows interoperation across versions.
75 77 * See <a href="../../../javax/management/MXBean.html#MXBean-spec">
76 78 * the specification of MXBeans</a> for details.
77 79 *
78 80 * <a name="MXBeanNames"></a>
79 81 * <p>Each platform MXBean is a {@link PlatformManagedObject}
80 82 * and it has a unique
81 83 * {@link javax.management.ObjectName ObjectName} for
82 84 * registration in the platform {@code MBeanServer} as returned by
83 85 * by the {@link PlatformManagedObject#getObjectName getObjectName}
84 86 * method.
85 87 *
86 88 * <p>
87 89 * An application can access a platform MXBean in the following ways:
88 90 * <h5>1. Direct access to an MXBean interface</h5>
89 91 * <blockquote>
90 92 * <ul>
91 93 * <li>Get an MXBean instance by calling the
92 94 * {@link #getPlatformMXBean(Class) getPlatformMXBean} or
93 95 * {@link #getPlatformMXBeans(Class) getPlatformMXBeans} method
94 96 * and access the MXBean locally in the running
95 97 * virtual machine.
96 98 * </li>
97 99 * <li>Construct an MXBean proxy instance that forwards the
98 100 * method calls to a given {@link MBeanServer MBeanServer} by calling
99 101 * the {@link #getPlatformMXBean(MBeanServerConnection, Class)} or
100 102 * {@link #getPlatformMXBeans(MBeanServerConnection, Class)} method.
101 103 * The {@link #newPlatformMXBeanProxy newPlatformMXBeanProxy} method
102 104 * can also be used to construct an MXBean proxy instance of
103 105 * a given {@code ObjectName}.
104 106 * A proxy is typically constructed to remotely access
105 107 * an MXBean of another running virtual machine.
106 108 * </li>
107 109 * </ul>
108 110 * <h5>2. Indirect access to an MXBean interface via MBeanServer</h5>
109 111 * <ul>
110 112 * <li>Go through the platform {@code MBeanServer} to access MXBeans
111 113 * locally or a specific <tt>MBeanServerConnection</tt> to access
112 114 * MXBeans remotely.
113 115 * The attributes and operations of an MXBean use only
114 116 * <em>JMX open types</em> which include basic data types,
115 117 * {@link javax.management.openmbean.CompositeData CompositeData},
116 118 * and {@link javax.management.openmbean.TabularData TabularData}
117 119 * defined in
118 120 * {@link javax.management.openmbean.OpenType OpenType}.
119 121 * The mapping is specified in
120 122 * the {@linkplain javax.management.MXBean MXBean} specification
121 123 * for details.
122 124 * </li>
123 125 * </ul>
124 126 * </blockquote>
125 127 *
126 128 * <p>
127 129 * The {@link #getPlatformManagementInterfaces getPlatformManagementInterfaces}
128 130 * method returns all management interfaces supported in the Java virtual machine
129 131 * including the standard management interfaces listed in the tables
130 132 * below as well as the management interfaces extended by the JDK implementation.
131 133 * <p>
132 134 * A Java virtual machine has a single instance of the following management
133 135 * interfaces:
134 136 *
135 137 * <blockquote>
136 138 * <table border>
137 139 * <tr>
138 140 * <th>Management Interface</th>
139 141 * <th>ObjectName</th>
140 142 * </tr>
141 143 * <tr>
142 144 * <td> {@link ClassLoadingMXBean} </td>
143 145 * <td> {@link #CLASS_LOADING_MXBEAN_NAME
144 146 * java.lang:type=ClassLoading}</td>
145 147 * </tr>
146 148 * <tr>
147 149 * <td> {@link MemoryMXBean} </td>
148 150 * <td> {@link #MEMORY_MXBEAN_NAME
149 151 * java.lang:type=Memory}</td>
150 152 * </tr>
151 153 * <tr>
152 154 * <td> {@link ThreadMXBean} </td>
153 155 * <td> {@link #THREAD_MXBEAN_NAME
154 156 * java.lang:type=Threading}</td>
155 157 * </tr>
156 158 * <tr>
157 159 * <td> {@link RuntimeMXBean} </td>
158 160 * <td> {@link #RUNTIME_MXBEAN_NAME
159 161 * java.lang:type=Runtime}</td>
160 162 * </tr>
161 163 * <tr>
162 164 * <td> {@link OperatingSystemMXBean} </td>
163 165 * <td> {@link #OPERATING_SYSTEM_MXBEAN_NAME
164 166 * java.lang:type=OperatingSystem}</td>
165 167 * </tr>
166 168 * <tr>
167 169 * <td> {@link PlatformLoggingMXBean} </td>
168 170 * <td> {@link java.util.logging.LogManager#LOGGING_MXBEAN_NAME
169 171 * java.util.logging:type=Logging}</td>
170 172 * </tr>
171 173 * </table>
172 174 * </blockquote>
173 175 *
174 176 * <p>
175 177 * A Java virtual machine has zero or a single instance of
176 178 * the following management interfaces.
177 179 *
178 180 * <blockquote>
179 181 * <table border>
180 182 * <tr>
181 183 * <th>Management Interface</th>
182 184 * <th>ObjectName</th>
183 185 * </tr>
184 186 * <tr>
185 187 * <td> {@link CompilationMXBean} </td>
186 188 * <td> {@link #COMPILATION_MXBEAN_NAME
187 189 * java.lang:type=Compilation}</td>
188 190 * </tr>
189 191 * </table>
190 192 * </blockquote>
191 193 *
192 194 * <p>
193 195 * A Java virtual machine may have one or more instances of the following
194 196 * management interfaces.
195 197 * <blockquote>
196 198 * <table border>
197 199 * <tr>
198 200 * <th>Management Interface</th>
199 201 * <th>ObjectName</th>
200 202 * </tr>
201 203 * <tr>
202 204 * <td> {@link GarbageCollectorMXBean} </td>
203 205 * <td> {@link #GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
204 206 * java.lang:type=GarbageCollector}<tt>,name=</tt><i>collector's name</i></td>
205 207 * </tr>
206 208 * <tr>
207 209 * <td> {@link MemoryManagerMXBean} </td>
208 210 * <td> {@link #MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
209 211 * java.lang:type=MemoryManager}<tt>,name=</tt><i>manager's name</i></td>
210 212 * </tr>
211 213 * <tr>
212 214 * <td> {@link MemoryPoolMXBean} </td>
213 215 * <td> {@link #MEMORY_POOL_MXBEAN_DOMAIN_TYPE
214 216 * java.lang:type=MemoryPool}<tt>,name=</tt><i>pool's name</i></td>
215 217 * </tr>
216 218 * <tr>
217 219 * <td> {@link BufferPoolMXBean} </td>
218 220 * <td> {@code java.nio:type=BufferPool,name=}<i>pool name</i></td>
219 221 * </tr>
220 222 * </table>
221 223 * </blockquote>
222 224 *
223 225 * @see <a href="../../../javax/management/package-summary.html">
224 226 * JMX Specification</a>
225 227 * @see <a href="package-summary.html#examples">
226 228 * Ways to Access Management Metrics</a>
227 229 * @see javax.management.MXBean
228 230 *
229 231 * @author Mandy Chung
230 232 * @since 1.5
231 233 */
232 234 public class ManagementFactory {
233 235 // A class with only static fields and methods.
234 236 private ManagementFactory() {};
235 237
236 238 /**
237 239 * String representation of the
238 240 * <tt>ObjectName</tt> for the {@link ClassLoadingMXBean}.
239 241 */
240 242 public final static String CLASS_LOADING_MXBEAN_NAME =
241 243 "java.lang:type=ClassLoading";
242 244
243 245 /**
244 246 * String representation of the
245 247 * <tt>ObjectName</tt> for the {@link CompilationMXBean}.
246 248 */
247 249 public final static String COMPILATION_MXBEAN_NAME =
248 250 "java.lang:type=Compilation";
249 251
250 252 /**
251 253 * String representation of the
252 254 * <tt>ObjectName</tt> for the {@link MemoryMXBean}.
253 255 */
254 256 public final static String MEMORY_MXBEAN_NAME =
255 257 "java.lang:type=Memory";
256 258
257 259 /**
258 260 * String representation of the
259 261 * <tt>ObjectName</tt> for the {@link OperatingSystemMXBean}.
260 262 */
261 263 public final static String OPERATING_SYSTEM_MXBEAN_NAME =
262 264 "java.lang:type=OperatingSystem";
263 265
264 266 /**
265 267 * String representation of the
266 268 * <tt>ObjectName</tt> for the {@link RuntimeMXBean}.
267 269 */
268 270 public final static String RUNTIME_MXBEAN_NAME =
269 271 "java.lang:type=Runtime";
270 272
271 273 /**
272 274 * String representation of the
273 275 * <tt>ObjectName</tt> for the {@link ThreadMXBean}.
274 276 */
275 277 public final static String THREAD_MXBEAN_NAME =
276 278 "java.lang:type=Threading";
277 279
278 280 /**
279 281 * The domain name and the type key property in
280 282 * the <tt>ObjectName</tt> for a {@link GarbageCollectorMXBean}.
281 283 * The unique <tt>ObjectName</tt> for a <tt>GarbageCollectorMXBean</tt>
282 284 * can be formed by appending this string with
283 285 * "<tt>,name=</tt><i>collector's name</i>".
284 286 */
285 287 public final static String GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE =
286 288 "java.lang:type=GarbageCollector";
287 289
288 290 /**
289 291 * The domain name and the type key property in
290 292 * the <tt>ObjectName</tt> for a {@link MemoryManagerMXBean}.
291 293 * The unique <tt>ObjectName</tt> for a <tt>MemoryManagerMXBean</tt>
292 294 * can be formed by appending this string with
293 295 * "<tt>,name=</tt><i>manager's name</i>".
294 296 */
295 297 public final static String MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE=
296 298 "java.lang:type=MemoryManager";
297 299
298 300 /**
299 301 * The domain name and the type key property in
300 302 * the <tt>ObjectName</tt> for a {@link MemoryPoolMXBean}.
301 303 * The unique <tt>ObjectName</tt> for a <tt>MemoryPoolMXBean</tt>
302 304 * can be formed by appending this string with
303 305 * <tt>,name=</tt><i>pool's name</i>.
304 306 */
305 307 public final static String MEMORY_POOL_MXBEAN_DOMAIN_TYPE=
306 308 "java.lang:type=MemoryPool";
307 309
308 310 /**
309 311 * Returns the managed bean for the class loading system of
310 312 * the Java virtual machine.
311 313 *
312 314 * @return a {@link ClassLoadingMXBean} object for
313 315 * the Java virtual machine.
314 316 */
315 317 public static ClassLoadingMXBean getClassLoadingMXBean() {
316 318 return ManagementFactoryHelper.getClassLoadingMXBean();
317 319 }
318 320
319 321 /**
320 322 * Returns the managed bean for the memory system of
321 323 * the Java virtual machine.
322 324 *
323 325 * @return a {@link MemoryMXBean} object for the Java virtual machine.
324 326 */
325 327 public static MemoryMXBean getMemoryMXBean() {
326 328 return ManagementFactoryHelper.getMemoryMXBean();
327 329 }
328 330
329 331 /**
330 332 * Returns the managed bean for the thread system of
331 333 * the Java virtual machine.
332 334 *
333 335 * @return a {@link ThreadMXBean} object for the Java virtual machine.
334 336 */
335 337 public static ThreadMXBean getThreadMXBean() {
336 338 return ManagementFactoryHelper.getThreadMXBean();
337 339 }
338 340
339 341 /**
340 342 * Returns the managed bean for the runtime system of
341 343 * the Java virtual machine.
342 344 *
343 345 * @return a {@link RuntimeMXBean} object for the Java virtual machine.
344 346
345 347 */
346 348 public static RuntimeMXBean getRuntimeMXBean() {
347 349 return ManagementFactoryHelper.getRuntimeMXBean();
348 350 }
349 351
350 352 /**
351 353 * Returns the managed bean for the compilation system of
352 354 * the Java virtual machine. This method returns <tt>null</tt>
353 355 * if the Java virtual machine has no compilation system.
354 356 *
355 357 * @return a {@link CompilationMXBean} object for the Java virtual
356 358 * machine or <tt>null</tt> if the Java virtual machine has
357 359 * no compilation system.
358 360 */
359 361 public static CompilationMXBean getCompilationMXBean() {
360 362 return ManagementFactoryHelper.getCompilationMXBean();
361 363 }
362 364
363 365 /**
364 366 * Returns the managed bean for the operating system on which
365 367 * the Java virtual machine is running.
366 368 *
367 369 * @return an {@link OperatingSystemMXBean} object for
368 370 * the Java virtual machine.
369 371 */
370 372 public static OperatingSystemMXBean getOperatingSystemMXBean() {
371 373 return ManagementFactoryHelper.getOperatingSystemMXBean();
372 374 }
373 375
374 376 /**
375 377 * Returns a list of {@link MemoryPoolMXBean} objects in the
376 378 * Java virtual machine.
377 379 * The Java virtual machine can have one or more memory pools.
378 380 * It may add or remove memory pools during execution.
379 381 *
380 382 * @return a list of <tt>MemoryPoolMXBean</tt> objects.
381 383 *
382 384 */
383 385 public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() {
384 386 return ManagementFactoryHelper.getMemoryPoolMXBeans();
385 387 }
386 388
387 389 /**
388 390 * Returns a list of {@link MemoryManagerMXBean} objects
389 391 * in the Java virtual machine.
390 392 * The Java virtual machine can have one or more memory managers.
391 393 * It may add or remove memory managers during execution.
392 394 *
393 395 * @return a list of <tt>MemoryManagerMXBean</tt> objects.
394 396 *
395 397 */
396 398 public static List<MemoryManagerMXBean> getMemoryManagerMXBeans() {
397 399 return ManagementFactoryHelper.getMemoryManagerMXBeans();
398 400 }
399 401
400 402
401 403 /**
402 404 * Returns a list of {@link GarbageCollectorMXBean} objects
403 405 * in the Java virtual machine.
404 406 * The Java virtual machine may have one or more
405 407 * <tt>GarbageCollectorMXBean</tt> objects.
406 408 * It may add or remove <tt>GarbageCollectorMXBean</tt>
407 409 * during execution.
408 410 *
409 411 * @return a list of <tt>GarbageCollectorMXBean</tt> objects.
410 412 *
411 413 */
412 414 public static List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
413 415 return ManagementFactoryHelper.getGarbageCollectorMXBeans();
414 416 }
415 417
416 418 private static MBeanServer platformMBeanServer;
417 419 /**
418 420 * Returns the platform {@link javax.management.MBeanServer MBeanServer}.
419 421 * On the first call to this method, it first creates the platform
420 422 * {@code MBeanServer} by calling the
421 423 * {@link javax.management.MBeanServerFactory#createMBeanServer
422 424 * MBeanServerFactory.createMBeanServer}
423 425 * method and registers each platform MXBean in this platform
424 426 * {@code MBeanServer} with its
425 427 * {@link PlatformManagedObject#getObjectName ObjectName}.
426 428 * This method, in subsequent calls, will simply return the
427 429 * initially created platform {@code MBeanServer}.
428 430 * <p>
429 431 * MXBeans that get created and destroyed dynamically, for example,
430 432 * memory {@link MemoryPoolMXBean pools} and
431 433 * {@link MemoryManagerMXBean managers},
432 434 * will automatically be registered and deregistered into the platform
433 435 * {@code MBeanServer}.
434 436 * <p>
435 437 * If the system property {@code javax.management.builder.initial}
436 438 * is set, the platform {@code MBeanServer} creation will be done
437 439 * by the specified {@link javax.management.MBeanServerBuilder}.
438 440 * <p>
439 441 * It is recommended that this platform MBeanServer also be used
440 442 * to register other application managed beans
441 443 * besides the platform MXBeans.
442 444 * This will allow all MBeans to be published through the same
443 445 * {@code MBeanServer} and hence allow for easier network publishing
444 446 * and discovery.
445 447 * Name conflicts with the platform MXBeans should be avoided.
446 448 *
447 449 * @return the platform {@code MBeanServer}; the platform
448 450 * MXBeans are registered into the platform {@code MBeanServer}
449 451 * at the first time this method is called.
450 452 *
451 453 * @exception SecurityException if there is a security manager
452 454 * and the caller does not have the permission required by
453 455 * {@link javax.management.MBeanServerFactory#createMBeanServer}.
454 456 *
455 457 * @see javax.management.MBeanServerFactory
456 458 * @see javax.management.MBeanServerFactory#createMBeanServer
457 459 */
458 460 public static synchronized MBeanServer getPlatformMBeanServer() {
459 461 SecurityManager sm = System.getSecurityManager();
460 462 if (sm != null) {
461 463 Permission perm = new MBeanServerPermission("createMBeanServer");
462 464 sm.checkPermission(perm);
463 465 }
464 466
465 467 if (platformMBeanServer == null) {
466 468 platformMBeanServer = MBeanServerFactory.createMBeanServer();
467 469 for (PlatformComponent pc : PlatformComponent.values()) {
468 470 List<? extends PlatformManagedObject> list =
469 471 pc.getMXBeans(pc.getMXBeanInterface());
470 472 for (PlatformManagedObject o : list) {
471 473 // Each PlatformComponent represents one management
472 474 // interface. Some MXBean may extend another one.
473 475 // The MXBean instances for one platform component
474 476 // (returned by pc.getMXBeans()) might be also
↓ open down ↓ |
420 lines elided |
↑ open up ↑ |
475 477 // the MXBean instances for another platform component.
476 478 // e.g. com.sun.management.GarbageCollectorMXBean
477 479 //
478 480 // So need to check if an MXBean instance is registered
479 481 // before registering into the platform MBeanServer
480 482 if (!platformMBeanServer.isRegistered(o.getObjectName())) {
481 483 addMXBean(platformMBeanServer, o);
482 484 }
483 485 }
484 486 }
487 + HashMap<ObjectName, DynamicMBean> dynmbeans =
488 + ManagementFactoryHelper.getPlatformDynamicMBeans();
489 + for (ObjectName on : dynmbeans.keySet()) {
490 + addDynamicMBean(platformMBeanServer, dynmbeans.get(on), on);
491 + }
485 492 }
486 493 return platformMBeanServer;
487 494 }
488 495
489 496 /**
490 497 * Returns a proxy for a platform MXBean interface of a
491 498 * given <a href="#MXBeanNames">MXBean name</a>
492 499 * that forwards its method calls through the given
493 500 * <tt>MBeanServerConnection</tt>.
494 501 *
495 502 * <p>This method is equivalent to:
496 503 * <blockquote>
497 504 * {@link java.lang.reflect.Proxy#newProxyInstance
498 505 * Proxy.newProxyInstance}<tt>(mxbeanInterface.getClassLoader(),
499 506 * new Class[] { mxbeanInterface }, handler)</tt>
500 507 * </blockquote>
501 508 *
502 509 * where <tt>handler</tt> is an {@link java.lang.reflect.InvocationHandler
503 510 * InvocationHandler} to which method invocations to the MXBean interface
504 511 * are dispatched. This <tt>handler</tt> converts an input parameter
505 512 * from an MXBean data type to its mapped open type before forwarding
506 513 * to the <tt>MBeanServer</tt> and converts a return value from
507 514 * an MXBean method call through the <tt>MBeanServer</tt>
508 515 * from an open type to the corresponding return type declared in
509 516 * the MXBean interface.
510 517 *
511 518 * <p>
512 519 * If the MXBean is a notification emitter (i.e.,
513 520 * it implements
514 521 * {@link javax.management.NotificationEmitter NotificationEmitter}),
515 522 * both the <tt>mxbeanInterface</tt> and <tt>NotificationEmitter</tt>
516 523 * will be implemented by this proxy.
517 524 *
518 525 * <p>
519 526 * <b>Notes:</b>
520 527 * <ol>
521 528 * <li>Using an MXBean proxy is a convenience remote access to
522 529 * a platform MXBean of a running virtual machine. All method
523 530 * calls to the MXBean proxy are forwarded to an
524 531 * <tt>MBeanServerConnection</tt> where
525 532 * {@link java.io.IOException IOException} may be thrown
526 533 * when the communication problem occurs with the connector server.
527 534 * An application remotely accesses the platform MXBeans using
528 535 * proxy should prepare to catch <tt>IOException</tt> as if
529 536 * accessing with the <tt>MBeanServerConnector</tt> interface.</li>
530 537 *
531 538 * <li>When a client application is designed to remotely access MXBeans
532 539 * for a running virtual machine whose version is different than
533 540 * the version on which the application is running,
534 541 * it should prepare to catch
535 542 * {@link java.io.InvalidObjectException InvalidObjectException}
536 543 * which is thrown when an MXBean proxy receives a name of an
537 544 * enum constant which is missing in the enum class loaded in
538 545 * the client application. </li>
539 546 *
540 547 * <li>{@link javax.management.MBeanServerInvocationHandler
541 548 * MBeanServerInvocationHandler} or its
542 549 * {@link javax.management.MBeanServerInvocationHandler#newProxyInstance
543 550 * newProxyInstance} method cannot be used to create
544 551 * a proxy for a platform MXBean. The proxy object created
545 552 * by <tt>MBeanServerInvocationHandler</tt> does not handle
546 553 * the properties of the platform MXBeans described in
547 554 * the <a href="#MXBean">class specification</a>.
548 555 *</li>
549 556 * </ol>
550 557 *
551 558 * @param connection the <tt>MBeanServerConnection</tt> to forward to.
552 559 * @param mxbeanName the name of a platform MXBean within
553 560 * <tt>connection</tt> to forward to. <tt>mxbeanName</tt> must be
554 561 * in the format of {@link ObjectName ObjectName}.
555 562 * @param mxbeanInterface the MXBean interface to be implemented
556 563 * by the proxy.
557 564 *
558 565 * @throws IllegalArgumentException if
559 566 * <ul>
560 567 * <li><tt>mxbeanName</tt> is not with a valid
561 568 * {@link ObjectName ObjectName} format, or</li>
562 569 * <li>the named MXBean in the <tt>connection</tt> is
563 570 * not a MXBean provided by the platform, or</li>
564 571 * <li>the named MXBean is not registered in the
565 572 * <tt>MBeanServerConnection</tt>, or</li>
566 573 * <li>the named MXBean is not an instance of the given
567 574 * <tt>mxbeanInterface</tt></li>
568 575 * </ul>
569 576 *
570 577 * @throws java.io.IOException if a communication problem
571 578 * occurred when accessing the <tt>MBeanServerConnection</tt>.
572 579 */
573 580 public static <T> T
574 581 newPlatformMXBeanProxy(MBeanServerConnection connection,
575 582 String mxbeanName,
576 583 Class<T> mxbeanInterface)
577 584 throws java.io.IOException {
578 585
579 586 // Only allow MXBean interfaces from rt.jar loaded by the
580 587 // bootstrap class loader
581 588 final Class<?> cls = mxbeanInterface;
582 589 ClassLoader loader =
583 590 AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
584 591 public ClassLoader run() {
585 592 return cls.getClassLoader();
586 593 }
587 594 });
588 595 if (!sun.misc.VM.isSystemDomainLoader(loader)) {
589 596 throw new IllegalArgumentException(mxbeanName +
590 597 " is not a platform MXBean");
591 598 }
592 599
593 600 try {
594 601 final ObjectName objName = new ObjectName(mxbeanName);
595 602 // skip the isInstanceOf check for LoggingMXBean
596 603 String intfName = mxbeanInterface.getName();
597 604 if (!connection.isInstanceOf(objName, intfName)) {
598 605 throw new IllegalArgumentException(mxbeanName +
599 606 " is not an instance of " + mxbeanInterface);
600 607 }
601 608
602 609 final Class[] interfaces;
603 610 // check if the registered MBean is a notification emitter
604 611 boolean emitter = connection.isInstanceOf(objName, NOTIF_EMITTER);
605 612
606 613 // create an MXBean proxy
607 614 return JMX.newMXBeanProxy(connection, objName, mxbeanInterface,
608 615 emitter);
609 616 } catch (InstanceNotFoundException|MalformedObjectNameException e) {
610 617 throw new IllegalArgumentException(e);
611 618 }
612 619 }
613 620
614 621 /**
615 622 * Returns the platform MXBean implementing
616 623 * the given {@code mxbeanInterface} which is specified
617 624 * to have one single instance in the Java virtual machine.
618 625 * This method may return {@code null} if the management interface
619 626 * is not implemented in the Java virtual machine (for example,
620 627 * a Java virtual machine with no compilation system does not
621 628 * implement {@link CompilationMXBean});
622 629 * otherwise, this method is equivalent to calling:
623 630 * <pre>
624 631 * {@link #getPlatformMXBeans(Class)
625 632 * getPlatformMXBeans(mxbeanInterface)}.get(0);
626 633 * </pre>
627 634 *
628 635 * @param mxbeanInterface a management interface for a platform
629 636 * MXBean with one single instance in the Java virtual machine
630 637 * if implemented.
631 638 *
632 639 * @return the platform MXBean that implements
633 640 * {@code mxbeanInterface}, or {@code null} if not exist.
634 641 *
635 642 * @throws IllegalArgumentException if {@code mxbeanInterface}
636 643 * is not a platform management interface or
637 644 * not a singleton platform MXBean.
638 645 *
639 646 * @since 1.7
640 647 */
641 648 public static <T extends PlatformManagedObject>
642 649 T getPlatformMXBean(Class<T> mxbeanInterface) {
643 650 PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
644 651 if (pc == null)
645 652 throw new IllegalArgumentException(mxbeanInterface.getName() +
646 653 " is not a platform management interface");
647 654 if (!pc.isSingleton())
648 655 throw new IllegalArgumentException(mxbeanInterface.getName() +
649 656 " can have zero or more than one instances");
650 657
651 658 return pc.getSingletonMXBean(mxbeanInterface);
652 659 }
653 660
654 661 /**
655 662 * Returns the list of platform MXBeans implementing
656 663 * the given {@code mxbeanInterface} in the Java
657 664 * virtual machine.
658 665 * The returned list may contain zero, one, or more instances.
659 666 * The number of instances in the returned list is defined
660 667 * in the specification of the given management interface.
661 668 * The order is undefined and there is no guarantee that
662 669 * the list returned is in the same order as previous invocations.
663 670 *
664 671 * @param mxbeanInterface a management interface for a platform
665 672 * MXBean
666 673 *
667 674 * @return the list of platform MXBeans that implement
668 675 * {@code mxbeanInterface}.
669 676 *
670 677 * @throws IllegalArgumentException if {@code mxbeanInterface}
671 678 * is not a platform management interface.
672 679 *
673 680 * @since 1.7
674 681 */
675 682 public static <T extends PlatformManagedObject> List<T>
676 683 getPlatformMXBeans(Class<T> mxbeanInterface) {
677 684 PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
678 685 if (pc == null)
679 686 throw new IllegalArgumentException(mxbeanInterface.getName() +
680 687 " is not a platform management interface");
681 688 return Collections.unmodifiableList(pc.getMXBeans(mxbeanInterface));
682 689 }
683 690
684 691 /**
685 692 * Returns the platform MXBean proxy for
686 693 * {@code mxbeanInterface} which is specified to have one single
687 694 * instance in a Java virtual machine and the proxy will
688 695 * forward the method calls through the given {@code MBeanServerConnection}.
689 696 * This method may return {@code null} if the management interface
690 697 * is not implemented in the Java virtual machine being monitored
691 698 * (for example, a Java virtual machine with no compilation system
692 699 * does not implement {@link CompilationMXBean});
693 700 * otherwise, this method is equivalent to calling:
694 701 * <pre>
695 702 * {@link #getPlatformMXBeans(MBeanServerConnection, Class)
696 703 * getPlatformMXBeans(connection, mxbeanInterface)}.get(0);
697 704 * </pre>
698 705 *
699 706 * @param connection the {@code MBeanServerConnection} to forward to.
700 707 * @param mxbeanInterface a management interface for a platform
701 708 * MXBean with one single instance in the Java virtual machine
702 709 * being monitored, if implemented.
703 710 *
704 711 * @return the platform MXBean proxy for
705 712 * forwarding the method calls of the {@code mxbeanInterface}
706 713 * through the given {@code MBeanServerConnection},
707 714 * or {@code null} if not exist.
708 715 *
709 716 * @throws IllegalArgumentException if {@code mxbeanInterface}
710 717 * is not a platform management interface or
711 718 * not a singleton platform MXBean.
712 719 * @throws java.io.IOException if a communication problem
713 720 * occurred when accessing the {@code MBeanServerConnection}.
714 721 *
715 722 * @see #newPlatformMXBeanProxy
716 723 * @since 1.7
717 724 */
718 725 public static <T extends PlatformManagedObject>
719 726 T getPlatformMXBean(MBeanServerConnection connection,
720 727 Class<T> mxbeanInterface)
721 728 throws java.io.IOException
722 729 {
723 730 PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
724 731 if (pc == null)
725 732 throw new IllegalArgumentException(mxbeanInterface.getName() +
726 733 " is not a platform management interface");
727 734 if (!pc.isSingleton())
728 735 throw new IllegalArgumentException(mxbeanInterface.getName() +
729 736 " can have zero or more than one instances");
730 737 return pc.getSingletonMXBean(connection, mxbeanInterface);
731 738 }
732 739
733 740 /**
734 741 * Returns the list of the platform MXBean proxies for
735 742 * forwarding the method calls of the {@code mxbeanInterface}
736 743 * through the given {@code MBeanServerConnection}.
737 744 * The returned list may contain zero, one, or more instances.
738 745 * The number of instances in the returned list is defined
739 746 * in the specification of the given management interface.
740 747 * The order is undefined and there is no guarantee that
741 748 * the list returned is in the same order as previous invocations.
742 749 *
743 750 * @param connection the {@code MBeanServerConnection} to forward to.
744 751 * @param mxbeanInterface a management interface for a platform
745 752 * MXBean
746 753 *
747 754 * @return the list of platform MXBean proxies for
748 755 * forwarding the method calls of the {@code mxbeanInterface}
749 756 * through the given {@code MBeanServerConnection}.
750 757 *
751 758 * @throws IllegalArgumentException if {@code mxbeanInterface}
752 759 * is not a platform management interface.
753 760 *
754 761 * @throws java.io.IOException if a communication problem
755 762 * occurred when accessing the {@code MBeanServerConnection}.
756 763 *
757 764 * @see #newPlatformMXBeanProxy
758 765 * @since 1.7
759 766 */
760 767 public static <T extends PlatformManagedObject>
761 768 List<T> getPlatformMXBeans(MBeanServerConnection connection,
762 769 Class<T> mxbeanInterface)
763 770 throws java.io.IOException
764 771 {
765 772 PlatformComponent pc = PlatformComponent.getPlatformComponent(mxbeanInterface);
766 773 if (pc == null) {
767 774 throw new IllegalArgumentException(mxbeanInterface.getName() +
768 775 " is not a platform management interface");
769 776 }
770 777 return Collections.unmodifiableList(pc.getMXBeans(connection, mxbeanInterface));
771 778 }
772 779
773 780 /**
774 781 * Returns the set of {@code Class} objects, subinterface of
775 782 * {@link PlatformManagedObject}, representing
776 783 * all management interfaces for
777 784 * monitoring and managing the Java platform.
778 785 *
779 786 * @return the set of {@code Class} objects, subinterface of
780 787 * {@link PlatformManagedObject} representing
781 788 * the management interfaces for
782 789 * monitoring and managing the Java platform.
783 790 *
784 791 * @since 1.7
785 792 */
786 793 public static Set<Class<? extends PlatformManagedObject>>
787 794 getPlatformManagementInterfaces()
788 795 {
789 796 Set<Class<? extends PlatformManagedObject>> result =
790 797 new HashSet<>();
791 798 for (PlatformComponent component: PlatformComponent.values()) {
792 799 result.add(component.getMXBeanInterface());
793 800 }
794 801 return Collections.unmodifiableSet(result);
795 802 }
796 803
797 804 private static final String NOTIF_EMITTER =
798 805 "javax.management.NotificationEmitter";
799 806
800 807 /**
801 808 * Registers an MXBean.
802 809 */
803 810 private static void addMXBean(final MBeanServer mbs, final PlatformManagedObject pmo) {
804 811 // Make DynamicMBean out of MXBean by wrapping it with a StandardMBean
805 812 try {
806 813 AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
807 814 public Void run() throws InstanceAlreadyExistsException,
808 815 MBeanRegistrationException,
809 816 NotCompliantMBeanException {
810 817 final DynamicMBean dmbean;
811 818 if (pmo instanceof DynamicMBean) {
812 819 dmbean = DynamicMBean.class.cast(pmo);
813 820 } else if (pmo instanceof NotificationEmitter) {
814 821 dmbean = new StandardEmitterMBean(pmo, null, true, (NotificationEmitter) pmo);
815 822 } else {
816 823 dmbean = new StandardMBean(pmo, null, true);
817 824 }
↓ open down ↓ |
323 lines elided |
↑ open up ↑ |
818 825
819 826 mbs.registerMBean(dmbean, pmo.getObjectName());
820 827 return null;
821 828 }
822 829 });
823 830 } catch (PrivilegedActionException e) {
824 831 throw new RuntimeException(e.getException());
825 832 }
826 833 }
827 834
835 + /**
836 + * Registers a DynamicMBean.
837 + */
838 + private static void addDynamicMBean(final MBeanServer mbs,
839 + final DynamicMBean dmbean,
840 + final ObjectName on) {
841 + try {
842 + AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
843 + @Override
844 + public Void run() throws InstanceAlreadyExistsException,
845 + MBeanRegistrationException,
846 + NotCompliantMBeanException {
847 + mbs.registerMBean(dmbean, on);
848 + return null;
849 + }
850 + });
851 + } catch (PrivilegedActionException e) {
852 + throw new RuntimeException(e.getException());
853 + }
854 + }
828 855 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX