1 /*
2 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.management;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.HashSet;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Set;
35 import javax.management.MBeanServerConnection;
36 import javax.management.ObjectName;
37
38 import com.sun.management.HotSpotDiagnosticMXBean;
39 import com.sun.management.UnixOperatingSystemMXBean;
40
41 import sun.management.ManagementFactoryHelper;
42 import sun.management.Util;
43
44 /**
45 * This enum class defines the list of platform components
46 * that provides monitoring and management support.
47 * Each enum represents one MXBean interface. A MXBean
48 * instance could implement one or more MXBean interfaces.
49 *
50 * For example, com.sun.management.GarbageCollectorMXBean
51 * extends java.lang.management.GarbageCollectorMXBean
52 * and there is one set of garbage collection MXBean instances,
53 * each of which implements both c.s.m. and j.l.m. interfaces.
54 * There are two separate enums GARBAGE_COLLECTOR
55 * and SUN_GARBAGE_COLLECTOR so that ManagementFactory.getPlatformMXBeans(Class)
56 * will return the list of MXBeans of the specified type.
57 *
58 * To add a new MXBean interface for the Java platform,
59 * add a new enum constant and implement the MXBeanFetcher.
60 */
61 enum PlatformComponent {
62
63 /**
253 "com.sun.management.UnixOperatingSystemMXBean",
254 "java.lang", "OperatingSystem", defaultKeyProperties(),
255 true, // singleton
256 new MXBeanFetcher<UnixOperatingSystemMXBean>() {
257 public List<UnixOperatingSystemMXBean> getMXBeans() {
258 return getOSMXBeanList(com.sun.management.UnixOperatingSystemMXBean.class);
259 }
260 }),
261
262 /**
263 * Diagnostic support for the HotSpot Virtual Machine.
264 */
265 HOTSPOT_DIAGNOSTIC(
266 "com.sun.management.HotSpotDiagnosticMXBean",
267 "com.sun.management", "HotSpotDiagnostic", defaultKeyProperties(),
268 true, // singleton
269 new MXBeanFetcher<HotSpotDiagnosticMXBean>() {
270 public List<HotSpotDiagnosticMXBean> getMXBeans() {
271 return Collections.singletonList(ManagementFactoryHelper.getDiagnosticMXBean());
272 }
273 });
274
275
276 /**
277 * A task that returns the MXBeans for a component.
278 */
279 interface MXBeanFetcher<T extends PlatformManagedObject> {
280 public List<T> getMXBeans();
281 }
282
283 /*
284 * Returns a list of the GC MXBeans of the given type.
285 */
286 private static <T extends GarbageCollectorMXBean>
287 List<T> getGcMXBeanList(Class<T> gcMXBeanIntf) {
288 List<GarbageCollectorMXBean> list =
289 ManagementFactoryHelper.getGarbageCollectorMXBeans();
290 List<T> result = new ArrayList<>(list.size());
291 for (GarbageCollectorMXBean m : list) {
292 if (gcMXBeanIntf.isInstance(m)) {
293 result.add(gcMXBeanIntf.cast(m));
294 }
362 Class<? extends PlatformManagedObject> getMXBeanInterface() {
363 try {
364 // Lazy loading the MXBean interface only when it is needed
365 return (Class<? extends PlatformManagedObject>)
366 Class.forName(mxbeanInterfaceName, false,
367 PlatformManagedObject.class.getClassLoader());
368 } catch (ClassNotFoundException x) {
369 throw new AssertionError(x);
370 }
371 }
372
373 @SuppressWarnings("unchecked")
374 <T extends PlatformManagedObject>
375 List<T> getMXBeans(Class<T> mxbeanInterface)
376 {
377 return (List<T>) fetcher.getMXBeans();
378 }
379
380 <T extends PlatformManagedObject> T getSingletonMXBean(Class<T> mxbeanInterface)
381 {
382 if (!singleton)
383 throw new IllegalArgumentException(mxbeanInterfaceName +
384 " can have zero or more than one instances");
385
386 List<T> list = getMXBeans(mxbeanInterface);
387 assert list.size() == 1;
388 return list.isEmpty() ? null : list.get(0);
389 }
390
391 <T extends PlatformManagedObject>
392 T getSingletonMXBean(MBeanServerConnection mbs, Class<T> mxbeanInterface)
393 throws java.io.IOException
394 {
395 if (!singleton)
396 throw new IllegalArgumentException(mxbeanInterfaceName +
397 " can have zero or more than one instances");
398
399 // ObjectName of a singleton MXBean contains only domain and type
400 assert keyProperties.size() == 1;
401 String on = domain + ":type=" + type;
402 return ManagementFactory.newPlatformMXBeanProxy(mbs,
403 on,
404 mxbeanInterface);
405 }
406
407 <T extends PlatformManagedObject>
408 List<T> getMXBeans(MBeanServerConnection mbs, Class<T> mxbeanInterface)
409 throws java.io.IOException
410 {
411 List<T> result = new ArrayList<>();
412 for (ObjectName on : getObjectNames(mbs)) {
413 result.add(ManagementFactory.
414 newPlatformMXBeanProxy(mbs,
415 on.getCanonicalName(),
416 mxbeanInterface)
417 );
442 enumMap = new HashMap<>();
443 for (PlatformComponent pc: PlatformComponent.values()) {
444 // Use String as the key rather than Class<?> to avoid
445 // causing unnecessary class loading of management interface
446 enumMap.put(pc.getMXBeanInterfaceName(), pc);
447 }
448 }
449 }
450
451 static boolean isPlatformMXBean(String cn) {
452 ensureInitialized();
453 return enumMap.containsKey(cn);
454 }
455
456 static <T extends PlatformManagedObject>
457 PlatformComponent getPlatformComponent(Class<T> mxbeanInterface)
458 {
459 ensureInitialized();
460 String cn = mxbeanInterface.getName();
461 PlatformComponent pc = enumMap.get(cn);
462 if (pc != null && pc.getMXBeanInterface() == mxbeanInterface)
463 return pc;
464 return null;
465 }
466
467 private static final long serialVersionUID = 6992337162326171013L;
468 }
|
1 /*
2 * Copyright (c) 2008, 2019, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.lang.management;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.HashSet;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Set;
35 import javax.management.MBeanServerConnection;
36 import javax.management.ObjectName;
37
38 import com.sun.management.HotSpotDiagnosticMXBean;
39 import com.sun.management.UnixOperatingSystemMXBean;
40 import com.sun.management.VMOption;
41
42 import sun.management.ManagementFactoryHelper;
43 import sun.management.Util;
44
45 import jdk.management.jfr.FlightRecorderMXBean;
46
47 /**
48 * This enum class defines the list of platform components
49 * that provides monitoring and management support.
50 * Each enum represents one MXBean interface. A MXBean
51 * instance could implement one or more MXBean interfaces.
52 *
53 * For example, com.sun.management.GarbageCollectorMXBean
54 * extends java.lang.management.GarbageCollectorMXBean
55 * and there is one set of garbage collection MXBean instances,
56 * each of which implements both c.s.m. and j.l.m. interfaces.
57 * There are two separate enums GARBAGE_COLLECTOR
58 * and SUN_GARBAGE_COLLECTOR so that ManagementFactory.getPlatformMXBeans(Class)
59 * will return the list of MXBeans of the specified type.
60 *
61 * To add a new MXBean interface for the Java platform,
62 * add a new enum constant and implement the MXBeanFetcher.
63 */
64 enum PlatformComponent {
65
66 /**
256 "com.sun.management.UnixOperatingSystemMXBean",
257 "java.lang", "OperatingSystem", defaultKeyProperties(),
258 true, // singleton
259 new MXBeanFetcher<UnixOperatingSystemMXBean>() {
260 public List<UnixOperatingSystemMXBean> getMXBeans() {
261 return getOSMXBeanList(com.sun.management.UnixOperatingSystemMXBean.class);
262 }
263 }),
264
265 /**
266 * Diagnostic support for the HotSpot Virtual Machine.
267 */
268 HOTSPOT_DIAGNOSTIC(
269 "com.sun.management.HotSpotDiagnosticMXBean",
270 "com.sun.management", "HotSpotDiagnostic", defaultKeyProperties(),
271 true, // singleton
272 new MXBeanFetcher<HotSpotDiagnosticMXBean>() {
273 public List<HotSpotDiagnosticMXBean> getMXBeans() {
274 return Collections.singletonList(ManagementFactoryHelper.getDiagnosticMXBean());
275 }
276 }),
277
278 /**
279 * Flight Recorder.
280 */
281 FLIGHT_RECORDER(
282 "jdk.management.jfr.FlightRecorderMXBean",
283 "jdk.management.jfr", "FlightRecorder", defaultKeyProperties(),
284 true,
285 new MXBeanFetcher<FlightRecorderMXBean>() {
286 public List<FlightRecorderMXBean> getMXBeans() {
287 HotSpotDiagnosticMXBean hsDiagMBean = ManagementFactoryHelper.getDiagnosticMXBean();
288 VMOption opt = hsDiagMBean.getVMOption("EnableJFR");
289 if (Boolean.valueOf(opt.getValue())) {
290 FlightRecorderMXBean m = ManagementFactoryHelper.getFlightRecorderMXBean();
291 if (m != null) {
292 return Collections.singletonList(m);
293 }
294 }
295 return Collections.emptyList();
296 }
297 });
298
299 /**
300 * A task that returns the MXBeans for a component.
301 */
302 interface MXBeanFetcher<T extends PlatformManagedObject> {
303 public List<T> getMXBeans();
304 }
305
306 /*
307 * Returns a list of the GC MXBeans of the given type.
308 */
309 private static <T extends GarbageCollectorMXBean>
310 List<T> getGcMXBeanList(Class<T> gcMXBeanIntf) {
311 List<GarbageCollectorMXBean> list =
312 ManagementFactoryHelper.getGarbageCollectorMXBeans();
313 List<T> result = new ArrayList<>(list.size());
314 for (GarbageCollectorMXBean m : list) {
315 if (gcMXBeanIntf.isInstance(m)) {
316 result.add(gcMXBeanIntf.cast(m));
317 }
385 Class<? extends PlatformManagedObject> getMXBeanInterface() {
386 try {
387 // Lazy loading the MXBean interface only when it is needed
388 return (Class<? extends PlatformManagedObject>)
389 Class.forName(mxbeanInterfaceName, false,
390 PlatformManagedObject.class.getClassLoader());
391 } catch (ClassNotFoundException x) {
392 throw new AssertionError(x);
393 }
394 }
395
396 @SuppressWarnings("unchecked")
397 <T extends PlatformManagedObject>
398 List<T> getMXBeans(Class<T> mxbeanInterface)
399 {
400 return (List<T>) fetcher.getMXBeans();
401 }
402
403 <T extends PlatformManagedObject> T getSingletonMXBean(Class<T> mxbeanInterface)
404 {
405 if (!singleton) {
406 throw new IllegalArgumentException(mxbeanInterfaceName +
407 " can have zero or more than one instances");
408 }
409
410 List<T> list = getMXBeans(mxbeanInterface);
411 assert list.size() == 1;
412 return list.isEmpty() ? null : list.get(0);
413 }
414
415 <T extends PlatformManagedObject>
416 T getSingletonMXBean(MBeanServerConnection mbs, Class<T> mxbeanInterface)
417 throws java.io.IOException
418 {
419 if (!singleton) {
420 throw new IllegalArgumentException(mxbeanInterfaceName +
421 " can have zero or more than one instances");
422 }
423
424 // ObjectName of a singleton MXBean contains only domain and type
425 assert keyProperties.size() == 1;
426 String on = domain + ":type=" + type;
427 return ManagementFactory.newPlatformMXBeanProxy(mbs,
428 on,
429 mxbeanInterface);
430 }
431
432 <T extends PlatformManagedObject>
433 List<T> getMXBeans(MBeanServerConnection mbs, Class<T> mxbeanInterface)
434 throws java.io.IOException
435 {
436 List<T> result = new ArrayList<>();
437 for (ObjectName on : getObjectNames(mbs)) {
438 result.add(ManagementFactory.
439 newPlatformMXBeanProxy(mbs,
440 on.getCanonicalName(),
441 mxbeanInterface)
442 );
467 enumMap = new HashMap<>();
468 for (PlatformComponent pc: PlatformComponent.values()) {
469 // Use String as the key rather than Class<?> to avoid
470 // causing unnecessary class loading of management interface
471 enumMap.put(pc.getMXBeanInterfaceName(), pc);
472 }
473 }
474 }
475
476 static boolean isPlatformMXBean(String cn) {
477 ensureInitialized();
478 return enumMap.containsKey(cn);
479 }
480
481 static <T extends PlatformManagedObject>
482 PlatformComponent getPlatformComponent(Class<T> mxbeanInterface)
483 {
484 ensureInitialized();
485 String cn = mxbeanInterface.getName();
486 PlatformComponent pc = enumMap.get(cn);
487 if (pc != null && pc.getMXBeanInterface() == mxbeanInterface) {
488 return pc;
489 }
490 return null;
491 }
492
493 private static final long serialVersionUID = 6992337162326171013L;
494 }
|