src/share/classes/java/lang/management/PlatformComponent.java
Print this page
@@ -27,13 +27,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashSet;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Set;
-import java.util.logging.PlatformLoggingMXBean;
-import java.nio.BufferPoolMXBean;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.UnixOperatingSystemMXBean;
@@ -64,10 +64,11 @@
* Class loading system of the Java virtual machine.
*/
CLASS_LOADING(
"java.lang.management.ClassLoadingMXBean",
"java.lang", "ClassLoading", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<ClassLoadingMXBean>() {
public List<ClassLoadingMXBean> getMXBeans() {
return Collections.singletonList(ManagementFactoryHelper.getClassLoadingMXBean());
}
}),
@@ -76,10 +77,11 @@
* Compilation system of the Java virtual machine.
*/
COMPILATION(
"java.lang.management.CompilationMXBean",
"java.lang", "Compilation", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<CompilationMXBean>() {
public List<CompilationMXBean> getMXBeans() {
CompilationMXBean m = ManagementFactoryHelper.getCompilationMXBean();
if (m == null) {
return Collections.emptyList();
@@ -93,10 +95,11 @@
* Memory system of the Java virtual machine.
*/
MEMORY(
"java.lang.management.MemoryMXBean",
"java.lang", "Memory", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<MemoryMXBean>() {
public List<MemoryMXBean> getMXBeans() {
return Collections.singletonList(ManagementFactoryHelper.getMemoryMXBean());
}
}),
@@ -105,10 +108,11 @@
* Garbage Collector in the Java virtual machine.
*/
GARBAGE_COLLECTOR(
"java.lang.management.GarbageCollectorMXBean",
"java.lang", "GarbageCollector", keyProperties("name"),
+ false, // zero or more instances
new MXBeanFetcher<GarbageCollectorMXBean>() {
public List<GarbageCollectorMXBean> getMXBeans() {
return ManagementFactoryHelper.
getGarbageCollectorMXBeans();
}
@@ -118,10 +122,11 @@
* Memory manager in the Java virtual machine.
*/
MEMORY_MANAGER(
"java.lang.management.MemoryManagerMXBean",
"java.lang", "MemoryManager", keyProperties("name"),
+ false, // zero or more instances
new MXBeanFetcher<MemoryManagerMXBean>() {
public List<MemoryManagerMXBean> getMXBeans() {
return ManagementFactoryHelper.getMemoryManagerMXBeans();
}
},
@@ -131,10 +136,11 @@
* Memory pool in the Java virtual machine.
*/
MEMORY_POOL(
"java.lang.management.MemoryPoolMXBean",
"java.lang", "MemoryPool", keyProperties("name"),
+ false, // zero or more instances
new MXBeanFetcher<MemoryPoolMXBean>() {
public List<MemoryPoolMXBean> getMXBeans() {
return ManagementFactoryHelper.getMemoryPoolMXBeans();
}
}),
@@ -143,10 +149,11 @@
* Operating system on which the Java virtual machine is running
*/
OPERATING_SYSTEM(
"java.lang.management.OperatingSystemMXBean",
"java.lang", "OperatingSystem", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<OperatingSystemMXBean>() {
public List<OperatingSystemMXBean> getMXBeans() {
return Collections.singletonList(ManagementFactoryHelper.getOperatingSystemMXBean());
}
}),
@@ -155,10 +162,11 @@
* Runtime system of the Java virtual machine.
*/
RUNTIME(
"java.lang.management.RuntimeMXBean",
"java.lang", "Runtime", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<RuntimeMXBean>() {
public List<RuntimeMXBean> getMXBeans() {
return Collections.singletonList(ManagementFactoryHelper.getRuntimeMXBean());
}
}),
@@ -167,10 +175,11 @@
* Threading system of the Java virtual machine.
*/
THREADING(
"java.lang.management.ThreadMXBean",
"java.lang", "Threading", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<ThreadMXBean>() {
public List<ThreadMXBean> getMXBeans() {
return Collections.singletonList(ManagementFactoryHelper.getThreadMXBean());
}
}),
@@ -178,24 +187,31 @@
/**
* Logging facility.
*/
LOGGING(
- "java.util.logging.PlatformLoggingMXBean",
+ "java.lang.management.PlatformLoggingMXBean",
"java.util.logging", "Logging", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<PlatformLoggingMXBean>() {
public List<PlatformLoggingMXBean> getMXBeans() {
- return ManagementFactoryHelper.getLoggingMXBean();
+ PlatformLoggingMXBean m = ManagementFactoryHelper.getPlatformLoggingMXBean();
+ if (m == null) {
+ return Collections.emptyList();
+ } else {
+ return Collections.singletonList(m);
}
+ }
}),
/**
* Buffer pools.
*/
BUFFER_POOL(
- "java.nio.BufferPoolMXBean",
+ "java.lang.management.BufferPoolMXBean",
"java.nio", "BufferPool", keyProperties("name"),
+ false, // zero or more instances
new MXBeanFetcher<BufferPoolMXBean>() {
public List<BufferPoolMXBean> getMXBeans() {
return ManagementFactoryHelper.getBufferPoolMXBeans();
}
}),
@@ -207,10 +223,11 @@
* Sun extension garbage collector that performs collections in cycles.
*/
SUN_GARBAGE_COLLECTOR(
"com.sun.management.GarbageCollectorMXBean",
"java.lang", "GarbageCollector", keyProperties("name"),
+ false, // zero or more instances
new MXBeanFetcher<com.sun.management.GarbageCollectorMXBean>() {
public List<com.sun.management.GarbageCollectorMXBean> getMXBeans() {
return getGcMXBeanList(com.sun.management.GarbageCollectorMXBean.class);
}
}),
@@ -220,10 +237,11 @@
* is running.
*/
SUN_OPERATING_SYSTEM(
"com.sun.management.OperatingSystemMXBean",
"java.lang", "OperatingSystem", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<com.sun.management.OperatingSystemMXBean>() {
public List<com.sun.management.OperatingSystemMXBean> getMXBeans() {
return getOSMXBeanList(com.sun.management.OperatingSystemMXBean.class);
}
}),
@@ -232,10 +250,11 @@
* Unix operating system.
*/
SUN_UNIX_OPERATING_SYSTEM(
"com.sun.management.UnixOperatingSystemMXBean",
"java.lang", "OperatingSystem", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<UnixOperatingSystemMXBean>() {
public List<UnixOperatingSystemMXBean> getMXBeans() {
return getOSMXBeanList(com.sun.management.UnixOperatingSystemMXBean.class);
}
}),
@@ -244,10 +263,11 @@
* Diagnostic support for the HotSpot Virtual Machine.
*/
HOTSPOT_DIAGNOSTIC(
"com.sun.management.HotSpotDiagnosticMXBean",
"com.sun.management", "HotSpotDiagnostic", defaultKeyProperties(),
+ true, // singleton
new MXBeanFetcher<HotSpotDiagnosticMXBean>() {
public List<HotSpotDiagnosticMXBean> getMXBeans() {
return Collections.singletonList(ManagementFactoryHelper.getDiagnosticMXBean());
}
});
@@ -294,31 +314,23 @@
private final String domain;
private final String type;
private final Set<String> keyProperties;
private final MXBeanFetcher fetcher;
private final PlatformComponent[] subComponents;
+ private final boolean singleton;
private PlatformComponent(String intfName,
String domain, String type,
Set<String> keyProperties,
- MXBeanFetcher fetcher) {
- this.mxbeanInterfaceName = intfName;
- this.domain = domain;
- this.type = type;
- this.keyProperties = keyProperties;
- this.fetcher = fetcher;
- this.subComponents = new PlatformComponent[0];
- }
- private PlatformComponent(String intfName,
- String domain, String type,
- Set<String> keyProperties,
+ boolean singleton,
MXBeanFetcher fetcher,
PlatformComponent... subComponents) {
this.mxbeanInterfaceName = intfName;
this.domain = domain;
this.type = type;
this.keyProperties = keyProperties;
+ this.singleton = singleton;
this.fetcher = fetcher;
this.subComponents = subComponents;
}
private static Set<String> defaultKeyProps;
@@ -336,10 +348,14 @@
set.add(s);
}
return set;
}
+ boolean isSingleton() {
+ return singleton;
+ }
+
String getMXBeanInterfaceName() {
return mxbeanInterfaceName;
}
@SuppressWarnings("unchecked")
@@ -358,11 +374,38 @@
List<T> getMXBeans(Class<T> mxbeanInterface)
{
return fetcher.getMXBeans();
}
+ <T extends PlatformManagedObject> T getSingletonMXBean(Class<T> mxbeanInterface)
+ {
+ if (!singleton)
+ throw new IllegalArgumentException(mxbeanInterfaceName +
+ " can have zero or more than one instances");
+
+ List<T> list = fetcher.getMXBeans();
+ assert list.size() == 1;
+ return list.isEmpty() ? null : list.get(0);
+ }
+
<T extends PlatformManagedObject>
+ T getSingletonMXBean(MBeanServerConnection mbs, Class<T> mxbeanInterface)
+ throws java.io.IOException
+ {
+ if (!singleton)
+ throw new IllegalArgumentException(mxbeanInterfaceName +
+ " can have zero or more than one instances");
+
+ // ObjectName of a singleton MXBean contains only domain and type
+ assert keyProperties.size() == 1;
+ String on = domain + ":type=" + type;
+ return ManagementFactory.newPlatformMXBeanProxy(mbs,
+ on,
+ mxbeanInterface);
+ }
+
+ <T extends PlatformManagedObject>
List<T> getMXBeans(MBeanServerConnection mbs, Class<T> mxbeanInterface)
throws java.io.IOException
{
List<T> result = new ArrayList<>();
for (ObjectName on : getObjectNames(mbs)) {
@@ -389,7 +432,36 @@
set.addAll(pc.getObjectNames(mbs));
}
return set;
}
+ // a map from MXBean interface name to PlatformComponent
+ private static Map<String, PlatformComponent> enumMap;
+ private static synchronized void ensureInitialized() {
+ if (enumMap == null) {
+ enumMap = new HashMap<>();
+ for (PlatformComponent pc: PlatformComponent.values()) {
+ // Use String as the key rather than Class<?> to avoid
+ // causing unnecessary class loading of management interface
+ enumMap.put(pc.getMXBeanInterfaceName(), pc);
+ }
+ }
+ }
+
+ static boolean isPlatformMXBean(String cn) {
+ ensureInitialized();
+ return enumMap.containsKey(cn);
+ }
+
+ static <T extends PlatformManagedObject>
+ PlatformComponent getPlatformComponent(Class<T> mxbeanInterface)
+ {
+ ensureInitialized();
+ String cn = mxbeanInterface.getName();
+ PlatformComponent pc = enumMap.get(cn);
+ if (pc != null && pc.getMXBeanInterface() == mxbeanInterface)
+ return pc;
+ return null;
+ }
+
private static final long serialVersionUID = 6992337162326171013L;
}