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.Set; 33 import java.util.logging.PlatformLoggingMXBean; 34 import java.nio.BufferPoolMXBean; 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 /** 64 * Class loading system of the Java virtual machine. 65 */ 66 CLASS_LOADING( 67 "java.lang.management.ClassLoadingMXBean", 68 "java.lang", "ClassLoading", defaultKeyProperties(), 69 new MXBeanFetcher<ClassLoadingMXBean>() { 70 public List<ClassLoadingMXBean> getMXBeans() { 71 return Collections.singletonList(ManagementFactoryHelper.getClassLoadingMXBean()); 72 } 73 }), 74 75 /** 76 * Compilation system of the Java virtual machine. 77 */ 78 COMPILATION( 79 "java.lang.management.CompilationMXBean", 80 "java.lang", "Compilation", defaultKeyProperties(), 81 new MXBeanFetcher<CompilationMXBean>() { 82 public List<CompilationMXBean> getMXBeans() { 83 CompilationMXBean m = ManagementFactoryHelper.getCompilationMXBean(); 84 if (m == null) { 85 return Collections.emptyList(); 86 } else { 87 return Collections.singletonList(m); 88 } 89 } 90 }), 91 92 /** 93 * Memory system of the Java virtual machine. 94 */ 95 MEMORY( 96 "java.lang.management.MemoryMXBean", 97 "java.lang", "Memory", defaultKeyProperties(), 98 new MXBeanFetcher<MemoryMXBean>() { 99 public List<MemoryMXBean> getMXBeans() { 100 return Collections.singletonList(ManagementFactoryHelper.getMemoryMXBean()); 101 } 102 }), 103 104 /** 105 * Garbage Collector in the Java virtual machine. 106 */ 107 GARBAGE_COLLECTOR( 108 "java.lang.management.GarbageCollectorMXBean", 109 "java.lang", "GarbageCollector", keyProperties("name"), 110 new MXBeanFetcher<GarbageCollectorMXBean>() { 111 public List<GarbageCollectorMXBean> getMXBeans() { 112 return ManagementFactoryHelper. 113 getGarbageCollectorMXBeans(); 114 } 115 }), 116 117 /** 118 * Memory manager in the Java virtual machine. 119 */ 120 MEMORY_MANAGER( 121 "java.lang.management.MemoryManagerMXBean", 122 "java.lang", "MemoryManager", keyProperties("name"), 123 new MXBeanFetcher<MemoryManagerMXBean>() { 124 public List<MemoryManagerMXBean> getMXBeans() { 125 return ManagementFactoryHelper.getMemoryManagerMXBeans(); 126 } 127 }, 128 GARBAGE_COLLECTOR), 129 130 /** 131 * Memory pool in the Java virtual machine. 132 */ 133 MEMORY_POOL( 134 "java.lang.management.MemoryPoolMXBean", 135 "java.lang", "MemoryPool", keyProperties("name"), 136 new MXBeanFetcher<MemoryPoolMXBean>() { 137 public List<MemoryPoolMXBean> getMXBeans() { 138 return ManagementFactoryHelper.getMemoryPoolMXBeans(); 139 } 140 }), 141 142 /** 143 * Operating system on which the Java virtual machine is running 144 */ 145 OPERATING_SYSTEM( 146 "java.lang.management.OperatingSystemMXBean", 147 "java.lang", "OperatingSystem", defaultKeyProperties(), 148 new MXBeanFetcher<OperatingSystemMXBean>() { 149 public List<OperatingSystemMXBean> getMXBeans() { 150 return Collections.singletonList(ManagementFactoryHelper.getOperatingSystemMXBean()); 151 } 152 }), 153 154 /** 155 * Runtime system of the Java virtual machine. 156 */ 157 RUNTIME( 158 "java.lang.management.RuntimeMXBean", 159 "java.lang", "Runtime", defaultKeyProperties(), 160 new MXBeanFetcher<RuntimeMXBean>() { 161 public List<RuntimeMXBean> getMXBeans() { 162 return Collections.singletonList(ManagementFactoryHelper.getRuntimeMXBean()); 163 } 164 }), 165 166 /** 167 * Threading system of the Java virtual machine. 168 */ 169 THREADING( 170 "java.lang.management.ThreadMXBean", 171 "java.lang", "Threading", defaultKeyProperties(), 172 new MXBeanFetcher<ThreadMXBean>() { 173 public List<ThreadMXBean> getMXBeans() { 174 return Collections.singletonList(ManagementFactoryHelper.getThreadMXBean()); 175 } 176 }), 177 178 179 /** 180 * Logging facility. 181 */ 182 LOGGING( 183 "java.util.logging.PlatformLoggingMXBean", 184 "java.util.logging", "Logging", defaultKeyProperties(), 185 new MXBeanFetcher<PlatformLoggingMXBean>() { 186 public List<PlatformLoggingMXBean> getMXBeans() { 187 return ManagementFactoryHelper.getLoggingMXBean(); 188 } 189 }), 190 191 /** 192 * Buffer pools. 193 */ 194 BUFFER_POOL( 195 "java.nio.BufferPoolMXBean", 196 "java.nio", "BufferPool", keyProperties("name"), 197 new MXBeanFetcher<BufferPoolMXBean>() { 198 public List<BufferPoolMXBean> getMXBeans() { 199 return ManagementFactoryHelper.getBufferPoolMXBeans(); 200 } 201 }), 202 203 204 // Sun Platform Extension 205 206 /** 207 * Sun extension garbage collector that performs collections in cycles. 208 */ 209 SUN_GARBAGE_COLLECTOR( 210 "com.sun.management.GarbageCollectorMXBean", 211 "java.lang", "GarbageCollector", keyProperties("name"), 212 new MXBeanFetcher<com.sun.management.GarbageCollectorMXBean>() { 213 public List<com.sun.management.GarbageCollectorMXBean> getMXBeans() { 214 return getGcMXBeanList(com.sun.management.GarbageCollectorMXBean.class); 215 } 216 }), 217 218 /** 219 * Sun extension operating system on which the Java virtual machine 220 * is running. 221 */ 222 SUN_OPERATING_SYSTEM( 223 "com.sun.management.OperatingSystemMXBean", 224 "java.lang", "OperatingSystem", defaultKeyProperties(), 225 new MXBeanFetcher<com.sun.management.OperatingSystemMXBean>() { 226 public List<com.sun.management.OperatingSystemMXBean> getMXBeans() { 227 return getOSMXBeanList(com.sun.management.OperatingSystemMXBean.class); 228 } 229 }), 230 231 /** 232 * Unix operating system. 233 */ 234 SUN_UNIX_OPERATING_SYSTEM( 235 "com.sun.management.UnixOperatingSystemMXBean", 236 "java.lang", "OperatingSystem", defaultKeyProperties(), 237 new MXBeanFetcher<UnixOperatingSystemMXBean>() { 238 public List<UnixOperatingSystemMXBean> getMXBeans() { 239 return getOSMXBeanList(com.sun.management.UnixOperatingSystemMXBean.class); 240 } 241 }), 242 243 /** 244 * Diagnostic support for the HotSpot Virtual Machine. 245 */ 246 HOTSPOT_DIAGNOSTIC( 247 "com.sun.management.HotSpotDiagnosticMXBean", 248 "com.sun.management", "HotSpotDiagnostic", defaultKeyProperties(), 249 new MXBeanFetcher<HotSpotDiagnosticMXBean>() { 250 public List<HotSpotDiagnosticMXBean> getMXBeans() { 251 return Collections.singletonList(ManagementFactoryHelper.getDiagnosticMXBean()); 252 } 253 }); 254 255 256 /** 257 * A task that returns the MXBeans for a component. 258 */ 259 interface MXBeanFetcher<T extends PlatformManagedObject> { 260 public List<T> getMXBeans(); 261 } 262 263 /* 264 * Returns a list of the GC MXBeans of the given type. 265 */ 266 private static <T extends GarbageCollectorMXBean> 267 List<T> getGcMXBeanList(Class<T> gcMXBeanIntf) { 268 List<GarbageCollectorMXBean> list = 279 /* 280 * Returns the OS mxbean instance of the given type. 281 */ 282 private static <T extends OperatingSystemMXBean> 283 List<T> getOSMXBeanList(Class<T> osMXBeanIntf) { 284 OperatingSystemMXBean m = 285 ManagementFactoryHelper.getOperatingSystemMXBean(); 286 if (osMXBeanIntf.isInstance(m)) { 287 return Collections.singletonList(osMXBeanIntf.cast(m)); 288 } else { 289 return Collections.emptyList(); 290 } 291 } 292 293 private final String mxbeanInterfaceName; 294 private final String domain; 295 private final String type; 296 private final Set<String> keyProperties; 297 private final MXBeanFetcher fetcher; 298 private final PlatformComponent[] subComponents; 299 300 private PlatformComponent(String intfName, 301 String domain, String type, 302 Set<String> keyProperties, 303 MXBeanFetcher fetcher) { 304 this.mxbeanInterfaceName = intfName; 305 this.domain = domain; 306 this.type = type; 307 this.keyProperties = keyProperties; 308 this.fetcher = fetcher; 309 this.subComponents = new PlatformComponent[0]; 310 } 311 private PlatformComponent(String intfName, 312 String domain, String type, 313 Set<String> keyProperties, 314 MXBeanFetcher fetcher, 315 PlatformComponent... subComponents) { 316 this.mxbeanInterfaceName = intfName; 317 this.domain = domain; 318 this.type = type; 319 this.keyProperties = keyProperties; 320 this.fetcher = fetcher; 321 this.subComponents = subComponents; 322 } 323 324 private static Set<String> defaultKeyProps; 325 private static Set<String> defaultKeyProperties() { 326 if (defaultKeyProps == null) { 327 defaultKeyProps = Collections.singleton("type"); 328 } 329 return defaultKeyProps; 330 } 331 332 private static Set<String> keyProperties(String... keyNames) { 333 Set<String> set = new HashSet<>(); 334 set.add("type"); 335 for (String s : keyNames) { 336 set.add(s); 337 } 338 return set; 339 } 340 341 String getMXBeanInterfaceName() { 342 return mxbeanInterfaceName; 343 } 344 345 @SuppressWarnings("unchecked") 346 Class<? extends PlatformManagedObject> getMXBeanInterface() { 347 try { 348 // Lazy loading the MXBean interface only when it is needed 349 return (Class<? extends PlatformManagedObject>) 350 Class.forName(mxbeanInterfaceName, false, null); 351 } catch (ClassNotFoundException x) { 352 throw new AssertionError(x); 353 } 354 } 355 356 @SuppressWarnings("unchecked") 357 <T extends PlatformManagedObject> 358 List<T> getMXBeans(Class<T> mxbeanInterface) 359 { 360 return fetcher.getMXBeans(); 361 } 362 363 <T extends PlatformManagedObject> 364 List<T> getMXBeans(MBeanServerConnection mbs, Class<T> mxbeanInterface) 365 throws java.io.IOException 366 { 367 List<T> result = new ArrayList<>(); 368 for (ObjectName on : getObjectNames(mbs)) { 369 result.add(ManagementFactory. 370 newPlatformMXBeanProxy(mbs, 371 on.getCanonicalName(), 372 mxbeanInterface) 373 ); 374 } 375 return result; 376 } 377 378 private Set<ObjectName> getObjectNames(MBeanServerConnection mbs) 379 throws java.io.IOException 380 { 381 String domainAndType = domain + ":type=" + type; 382 if (keyProperties.size() > 1) { 383 // if there are more than 1 key properties (i.e. other than "type") 384 domainAndType += ",*"; 385 } 386 ObjectName on = Util.newObjectName(domainAndType); 387 Set<ObjectName> set = mbs.queryNames(on, null); 388 for (PlatformComponent pc : subComponents) { 389 set.addAll(pc.getObjectNames(mbs)); 390 } 391 return set; 392 } 393 394 private static final long serialVersionUID = 6992337162326171013L; 395 } | 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 /** 64 * Class loading system of the Java virtual machine. 65 */ 66 CLASS_LOADING( 67 "java.lang.management.ClassLoadingMXBean", 68 "java.lang", "ClassLoading", defaultKeyProperties(), 69 true, // singleton 70 new MXBeanFetcher<ClassLoadingMXBean>() { 71 public List<ClassLoadingMXBean> getMXBeans() { 72 return Collections.singletonList(ManagementFactoryHelper.getClassLoadingMXBean()); 73 } 74 }), 75 76 /** 77 * Compilation system of the Java virtual machine. 78 */ 79 COMPILATION( 80 "java.lang.management.CompilationMXBean", 81 "java.lang", "Compilation", defaultKeyProperties(), 82 true, // singleton 83 new MXBeanFetcher<CompilationMXBean>() { 84 public List<CompilationMXBean> getMXBeans() { 85 CompilationMXBean m = ManagementFactoryHelper.getCompilationMXBean(); 86 if (m == null) { 87 return Collections.emptyList(); 88 } else { 89 return Collections.singletonList(m); 90 } 91 } 92 }), 93 94 /** 95 * Memory system of the Java virtual machine. 96 */ 97 MEMORY( 98 "java.lang.management.MemoryMXBean", 99 "java.lang", "Memory", defaultKeyProperties(), 100 true, // singleton 101 new MXBeanFetcher<MemoryMXBean>() { 102 public List<MemoryMXBean> getMXBeans() { 103 return Collections.singletonList(ManagementFactoryHelper.getMemoryMXBean()); 104 } 105 }), 106 107 /** 108 * Garbage Collector in the Java virtual machine. 109 */ 110 GARBAGE_COLLECTOR( 111 "java.lang.management.GarbageCollectorMXBean", 112 "java.lang", "GarbageCollector", keyProperties("name"), 113 false, // zero or more instances 114 new MXBeanFetcher<GarbageCollectorMXBean>() { 115 public List<GarbageCollectorMXBean> getMXBeans() { 116 return ManagementFactoryHelper. 117 getGarbageCollectorMXBeans(); 118 } 119 }), 120 121 /** 122 * Memory manager in the Java virtual machine. 123 */ 124 MEMORY_MANAGER( 125 "java.lang.management.MemoryManagerMXBean", 126 "java.lang", "MemoryManager", keyProperties("name"), 127 false, // zero or more instances 128 new MXBeanFetcher<MemoryManagerMXBean>() { 129 public List<MemoryManagerMXBean> getMXBeans() { 130 return ManagementFactoryHelper.getMemoryManagerMXBeans(); 131 } 132 }, 133 GARBAGE_COLLECTOR), 134 135 /** 136 * Memory pool in the Java virtual machine. 137 */ 138 MEMORY_POOL( 139 "java.lang.management.MemoryPoolMXBean", 140 "java.lang", "MemoryPool", keyProperties("name"), 141 false, // zero or more instances 142 new MXBeanFetcher<MemoryPoolMXBean>() { 143 public List<MemoryPoolMXBean> getMXBeans() { 144 return ManagementFactoryHelper.getMemoryPoolMXBeans(); 145 } 146 }), 147 148 /** 149 * Operating system on which the Java virtual machine is running 150 */ 151 OPERATING_SYSTEM( 152 "java.lang.management.OperatingSystemMXBean", 153 "java.lang", "OperatingSystem", defaultKeyProperties(), 154 true, // singleton 155 new MXBeanFetcher<OperatingSystemMXBean>() { 156 public List<OperatingSystemMXBean> getMXBeans() { 157 return Collections.singletonList(ManagementFactoryHelper.getOperatingSystemMXBean()); 158 } 159 }), 160 161 /** 162 * Runtime system of the Java virtual machine. 163 */ 164 RUNTIME( 165 "java.lang.management.RuntimeMXBean", 166 "java.lang", "Runtime", defaultKeyProperties(), 167 true, // singleton 168 new MXBeanFetcher<RuntimeMXBean>() { 169 public List<RuntimeMXBean> getMXBeans() { 170 return Collections.singletonList(ManagementFactoryHelper.getRuntimeMXBean()); 171 } 172 }), 173 174 /** 175 * Threading system of the Java virtual machine. 176 */ 177 THREADING( 178 "java.lang.management.ThreadMXBean", 179 "java.lang", "Threading", defaultKeyProperties(), 180 true, // singleton 181 new MXBeanFetcher<ThreadMXBean>() { 182 public List<ThreadMXBean> getMXBeans() { 183 return Collections.singletonList(ManagementFactoryHelper.getThreadMXBean()); 184 } 185 }), 186 187 188 /** 189 * Logging facility. 190 */ 191 LOGGING( 192 "java.lang.management.PlatformLoggingMXBean", 193 "java.util.logging", "Logging", defaultKeyProperties(), 194 true, // singleton 195 new MXBeanFetcher<PlatformLoggingMXBean>() { 196 public List<PlatformLoggingMXBean> getMXBeans() { 197 PlatformLoggingMXBean m = ManagementFactoryHelper.getPlatformLoggingMXBean(); 198 if (m == null) { 199 return Collections.emptyList(); 200 } else { 201 return Collections.singletonList(m); 202 } 203 } 204 }), 205 206 /** 207 * Buffer pools. 208 */ 209 BUFFER_POOL( 210 "java.lang.management.BufferPoolMXBean", 211 "java.nio", "BufferPool", keyProperties("name"), 212 false, // zero or more instances 213 new MXBeanFetcher<BufferPoolMXBean>() { 214 public List<BufferPoolMXBean> getMXBeans() { 215 return ManagementFactoryHelper.getBufferPoolMXBeans(); 216 } 217 }), 218 219 220 // Sun Platform Extension 221 222 /** 223 * Sun extension garbage collector that performs collections in cycles. 224 */ 225 SUN_GARBAGE_COLLECTOR( 226 "com.sun.management.GarbageCollectorMXBean", 227 "java.lang", "GarbageCollector", keyProperties("name"), 228 false, // zero or more instances 229 new MXBeanFetcher<com.sun.management.GarbageCollectorMXBean>() { 230 public List<com.sun.management.GarbageCollectorMXBean> getMXBeans() { 231 return getGcMXBeanList(com.sun.management.GarbageCollectorMXBean.class); 232 } 233 }), 234 235 /** 236 * Sun extension operating system on which the Java virtual machine 237 * is running. 238 */ 239 SUN_OPERATING_SYSTEM( 240 "com.sun.management.OperatingSystemMXBean", 241 "java.lang", "OperatingSystem", defaultKeyProperties(), 242 true, // singleton 243 new MXBeanFetcher<com.sun.management.OperatingSystemMXBean>() { 244 public List<com.sun.management.OperatingSystemMXBean> getMXBeans() { 245 return getOSMXBeanList(com.sun.management.OperatingSystemMXBean.class); 246 } 247 }), 248 249 /** 250 * Unix operating system. 251 */ 252 SUN_UNIX_OPERATING_SYSTEM( 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 = 299 /* 300 * Returns the OS mxbean instance of the given type. 301 */ 302 private static <T extends OperatingSystemMXBean> 303 List<T> getOSMXBeanList(Class<T> osMXBeanIntf) { 304 OperatingSystemMXBean m = 305 ManagementFactoryHelper.getOperatingSystemMXBean(); 306 if (osMXBeanIntf.isInstance(m)) { 307 return Collections.singletonList(osMXBeanIntf.cast(m)); 308 } else { 309 return Collections.emptyList(); 310 } 311 } 312 313 private final String mxbeanInterfaceName; 314 private final String domain; 315 private final String type; 316 private final Set<String> keyProperties; 317 private final MXBeanFetcher fetcher; 318 private final PlatformComponent[] subComponents; 319 private final boolean singleton; 320 321 private PlatformComponent(String intfName, 322 String domain, String type, 323 Set<String> keyProperties, 324 boolean singleton, 325 MXBeanFetcher fetcher, 326 PlatformComponent... subComponents) { 327 this.mxbeanInterfaceName = intfName; 328 this.domain = domain; 329 this.type = type; 330 this.keyProperties = keyProperties; 331 this.singleton = singleton; 332 this.fetcher = fetcher; 333 this.subComponents = subComponents; 334 } 335 336 private static Set<String> defaultKeyProps; 337 private static Set<String> defaultKeyProperties() { 338 if (defaultKeyProps == null) { 339 defaultKeyProps = Collections.singleton("type"); 340 } 341 return defaultKeyProps; 342 } 343 344 private static Set<String> keyProperties(String... keyNames) { 345 Set<String> set = new HashSet<>(); 346 set.add("type"); 347 for (String s : keyNames) { 348 set.add(s); 349 } 350 return set; 351 } 352 353 boolean isSingleton() { 354 return singleton; 355 } 356 357 String getMXBeanInterfaceName() { 358 return mxbeanInterfaceName; 359 } 360 361 @SuppressWarnings("unchecked") 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, null); 367 } catch (ClassNotFoundException x) { 368 throw new AssertionError(x); 369 } 370 } 371 372 @SuppressWarnings("unchecked") 373 <T extends PlatformManagedObject> 374 List<T> getMXBeans(Class<T> mxbeanInterface) 375 { 376 return fetcher.getMXBeans(); 377 } 378 379 <T extends PlatformManagedObject> T getSingletonMXBean(Class<T> mxbeanInterface) 380 { 381 if (!singleton) 382 throw new IllegalArgumentException(mxbeanInterfaceName + 383 " can have zero or more than one instances"); 384 385 List<T> list = fetcher.getMXBeans(); 386 assert list.size() == 1; 387 return list.isEmpty() ? null : list.get(0); 388 } 389 390 <T extends PlatformManagedObject> 391 T getSingletonMXBean(MBeanServerConnection mbs, Class<T> mxbeanInterface) 392 throws java.io.IOException 393 { 394 if (!singleton) 395 throw new IllegalArgumentException(mxbeanInterfaceName + 396 " can have zero or more than one instances"); 397 398 // ObjectName of a singleton MXBean contains only domain and type 399 assert keyProperties.size() == 1; 400 String on = domain + ":type=" + type; 401 return ManagementFactory.newPlatformMXBeanProxy(mbs, 402 on, 403 mxbeanInterface); 404 } 405 406 <T extends PlatformManagedObject> 407 List<T> getMXBeans(MBeanServerConnection mbs, Class<T> mxbeanInterface) 408 throws java.io.IOException 409 { 410 List<T> result = new ArrayList<>(); 411 for (ObjectName on : getObjectNames(mbs)) { 412 result.add(ManagementFactory. 413 newPlatformMXBeanProxy(mbs, 414 on.getCanonicalName(), 415 mxbeanInterface) 416 ); 417 } 418 return result; 419 } 420 421 private Set<ObjectName> getObjectNames(MBeanServerConnection mbs) 422 throws java.io.IOException 423 { 424 String domainAndType = domain + ":type=" + type; 425 if (keyProperties.size() > 1) { 426 // if there are more than 1 key properties (i.e. other than "type") 427 domainAndType += ",*"; 428 } 429 ObjectName on = Util.newObjectName(domainAndType); 430 Set<ObjectName> set = mbs.queryNames(on, null); 431 for (PlatformComponent pc : subComponents) { 432 set.addAll(pc.getObjectNames(mbs)); 433 } 434 return set; 435 } 436 437 // a map from MXBean interface name to PlatformComponent 438 private static Map<String, PlatformComponent> enumMap; 439 private static synchronized void ensureInitialized() { 440 if (enumMap == null) { 441 enumMap = new HashMap<>(); 442 for (PlatformComponent pc: PlatformComponent.values()) { 443 // Use String as the key rather than Class<?> to avoid 444 // causing unnecessary class loading of management interface 445 enumMap.put(pc.getMXBeanInterfaceName(), pc); 446 } 447 } 448 } 449 450 static boolean isPlatformMXBean(String cn) { 451 ensureInitialized(); 452 return enumMap.containsKey(cn); 453 } 454 455 static <T extends PlatformManagedObject> 456 PlatformComponent getPlatformComponent(Class<T> mxbeanInterface) 457 { 458 ensureInitialized(); 459 String cn = mxbeanInterface.getName(); 460 PlatformComponent pc = enumMap.get(cn); 461 if (pc != null && pc.getMXBeanInterface() == mxbeanInterface) 462 return pc; 463 return null; 464 } 465 466 private static final long serialVersionUID = 6992337162326171013L; 467 } |