1 /*
2 * Copyright (c) 2003, 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 sun.management;
27
28 import java.lang.management.*;
29
30 import javax.management.InstanceAlreadyExistsException;
31 import javax.management.InstanceNotFoundException;
32 import javax.management.MBeanServer;
33 import javax.management.MBeanRegistrationException;
34 import javax.management.NotCompliantMBeanException;
35 import javax.management.ObjectName;
36 import javax.management.RuntimeOperationsException;
37 import java.security.AccessController;
38 import java.security.PrivilegedActionException;
39 import java.security.PrivilegedExceptionAction;
40
41 import sun.util.logging.LoggingSupport;
42
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
46 import com.sun.management.OSMBeanFactory;
47 import com.sun.management.HotSpotDiagnosticMXBean;
48
49 import static java.lang.management.ManagementFactory.*;
50
51 /**
52 * ManagementFactoryHelper provides static factory methods to create
53 * instances of the management interface.
54 */
55 public class ManagementFactoryHelper {
56 private ManagementFactoryHelper() {};
57
58 private static VMManagement jvm;
59
60 private static ClassLoadingImpl classMBean = null;
61 private static MemoryImpl memoryMBean = null;
62 private static ThreadImpl threadMBean = null;
63 private static RuntimeImpl runtimeMBean = null;
64 private static CompilationImpl compileMBean = null;
65 private static OperatingSystemImpl osMBean = null;
246 public long getCount() {
247 return pool.getCount();
248 }
249 @Override
250 public long getTotalCapacity() {
251 return pool.getTotalCapacity();
252 }
253 @Override
254 public long getMemoryUsed() {
255 return pool.getMemoryUsed();
256 }
257 };
258 }
259
260 private static HotSpotDiagnostic hsDiagMBean = null;
261 private static HotspotRuntime hsRuntimeMBean = null;
262 private static HotspotClassLoading hsClassMBean = null;
263 private static HotspotThread hsThreadMBean = null;
264 private static HotspotCompilation hsCompileMBean = null;
265 private static HotspotMemory hsMemoryMBean = null;
266
267 public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {
268 if (hsDiagMBean == null) {
269 hsDiagMBean = new HotSpotDiagnostic();
270 }
271 return hsDiagMBean;
272 }
273
274 /**
275 * This method is for testing only.
276 */
277 public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
278 if (hsRuntimeMBean == null) {
279 hsRuntimeMBean = new HotspotRuntime(jvm);
280 }
281 return hsRuntimeMBean;
282 }
283
284 /**
285 * This method is for testing only.
294 /**
295 * This method is for testing only.
296 */
297 public static synchronized HotspotThreadMBean getHotspotThreadMBean() {
298 if (hsThreadMBean == null) {
299 hsThreadMBean = new HotspotThread(jvm);
300 }
301 return hsThreadMBean;
302 }
303
304 /**
305 * This method is for testing only.
306 */
307 public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
308 if (hsMemoryMBean == null) {
309 hsMemoryMBean = new HotspotMemory(jvm);
310 }
311 return hsMemoryMBean;
312 }
313
314 /**
315 * This method is for testing only.
316 */
317 public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() {
318 if (hsCompileMBean == null) {
319 hsCompileMBean = new HotspotCompilation(jvm);
320 }
321 return hsCompileMBean;
322 }
323
324 /**
325 * Registers a given MBean if not registered in the MBeanServer;
326 * otherwise, just return.
327 */
328 private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
329 try {
330 final ObjectName objName = Util.newObjectName(mbeanName);
331
332 // inner class requires these fields to be final
333 final MBeanServer mbs0 = mbs;
348 } catch (PrivilegedActionException e) {
349 throw Util.newException(e.getException());
350 }
351 }
352
353 private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME =
354 "sun.management:type=HotspotClassLoading";
355
356 private final static String HOTSPOT_COMPILATION_MBEAN_NAME =
357 "sun.management:type=HotspotCompilation";
358
359 private final static String HOTSPOT_MEMORY_MBEAN_NAME =
360 "sun.management:type=HotspotMemory";
361
362 private static final String HOTSPOT_RUNTIME_MBEAN_NAME =
363 "sun.management:type=HotspotRuntime";
364
365 private final static String HOTSPOT_THREAD_MBEAN_NAME =
366 "sun.management:type=HotspotThreading";
367
368 static void registerInternalMBeans(MBeanServer mbs) {
369 // register all internal MBeans if not registered
370 // No exception is thrown if a MBean with that object name
371 // already registered
372 addMBean(mbs, getHotspotClassLoadingMBean(),
373 HOTSPOT_CLASS_LOADING_MBEAN_NAME);
374 addMBean(mbs, getHotspotMemoryMBean(),
375 HOTSPOT_MEMORY_MBEAN_NAME);
376 addMBean(mbs, getHotspotRuntimeMBean(),
377 HOTSPOT_RUNTIME_MBEAN_NAME);
378 addMBean(mbs, getHotspotThreadMBean(),
379 HOTSPOT_THREAD_MBEAN_NAME);
380
381 // CompilationMBean may not exist
382 if (getCompilationMXBean() != null) {
383 addMBean(mbs, getHotspotCompilationMBean(),
384 HOTSPOT_COMPILATION_MBEAN_NAME);
385 }
386 }
387
|
1 /*
2 * Copyright (c) 2003, 2013, 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 sun.management;
27
28 import java.lang.management.*;
29
30 import javax.management.DynamicMBean;
31 import javax.management.InstanceAlreadyExistsException;
32 import javax.management.InstanceNotFoundException;
33 import javax.management.MBeanServer;
34 import javax.management.MBeanRegistrationException;
35 import javax.management.NotCompliantMBeanException;
36 import javax.management.ObjectName;
37 import javax.management.RuntimeOperationsException;
38 import java.security.AccessController;
39 import java.security.PrivilegedActionException;
40 import java.security.PrivilegedExceptionAction;
41
42 import sun.util.logging.LoggingSupport;
43
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.List;
48 import com.sun.management.DiagnosticCommandMBean;
49 import com.sun.management.OSMBeanFactory;
50 import com.sun.management.HotSpotDiagnosticMXBean;
51
52 import static java.lang.management.ManagementFactory.*;
53
54 /**
55 * ManagementFactoryHelper provides static factory methods to create
56 * instances of the management interface.
57 */
58 public class ManagementFactoryHelper {
59 private ManagementFactoryHelper() {};
60
61 private static VMManagement jvm;
62
63 private static ClassLoadingImpl classMBean = null;
64 private static MemoryImpl memoryMBean = null;
65 private static ThreadImpl threadMBean = null;
66 private static RuntimeImpl runtimeMBean = null;
67 private static CompilationImpl compileMBean = null;
68 private static OperatingSystemImpl osMBean = null;
249 public long getCount() {
250 return pool.getCount();
251 }
252 @Override
253 public long getTotalCapacity() {
254 return pool.getTotalCapacity();
255 }
256 @Override
257 public long getMemoryUsed() {
258 return pool.getMemoryUsed();
259 }
260 };
261 }
262
263 private static HotSpotDiagnostic hsDiagMBean = null;
264 private static HotspotRuntime hsRuntimeMBean = null;
265 private static HotspotClassLoading hsClassMBean = null;
266 private static HotspotThread hsThreadMBean = null;
267 private static HotspotCompilation hsCompileMBean = null;
268 private static HotspotMemory hsMemoryMBean = null;
269 private static DiagnosticCommandImpl hsDiagCommandMBean = null;
270
271 public static synchronized HotSpotDiagnosticMXBean getDiagnosticMXBean() {
272 if (hsDiagMBean == null) {
273 hsDiagMBean = new HotSpotDiagnostic();
274 }
275 return hsDiagMBean;
276 }
277
278 /**
279 * This method is for testing only.
280 */
281 public static synchronized HotspotRuntimeMBean getHotspotRuntimeMBean() {
282 if (hsRuntimeMBean == null) {
283 hsRuntimeMBean = new HotspotRuntime(jvm);
284 }
285 return hsRuntimeMBean;
286 }
287
288 /**
289 * This method is for testing only.
298 /**
299 * This method is for testing only.
300 */
301 public static synchronized HotspotThreadMBean getHotspotThreadMBean() {
302 if (hsThreadMBean == null) {
303 hsThreadMBean = new HotspotThread(jvm);
304 }
305 return hsThreadMBean;
306 }
307
308 /**
309 * This method is for testing only.
310 */
311 public static synchronized HotspotMemoryMBean getHotspotMemoryMBean() {
312 if (hsMemoryMBean == null) {
313 hsMemoryMBean = new HotspotMemory(jvm);
314 }
315 return hsMemoryMBean;
316 }
317
318 public static synchronized DiagnosticCommandMBean getDiagnosticCommandMBean() {
319 // Remote Diagnostic Commands may not be supported
320 if (hsDiagCommandMBean == null && jvm.isRemoteDiagnosticCommandsSupported()) {
321 hsDiagCommandMBean = new DiagnosticCommandImpl(jvm);
322 }
323 return hsDiagCommandMBean;
324 }
325
326 /**
327 * This method is for testing only.
328 */
329 public static synchronized HotspotCompilationMBean getHotspotCompilationMBean() {
330 if (hsCompileMBean == null) {
331 hsCompileMBean = new HotspotCompilation(jvm);
332 }
333 return hsCompileMBean;
334 }
335
336 /**
337 * Registers a given MBean if not registered in the MBeanServer;
338 * otherwise, just return.
339 */
340 private static void addMBean(MBeanServer mbs, Object mbean, String mbeanName) {
341 try {
342 final ObjectName objName = Util.newObjectName(mbeanName);
343
344 // inner class requires these fields to be final
345 final MBeanServer mbs0 = mbs;
360 } catch (PrivilegedActionException e) {
361 throw Util.newException(e.getException());
362 }
363 }
364
365 private final static String HOTSPOT_CLASS_LOADING_MBEAN_NAME =
366 "sun.management:type=HotspotClassLoading";
367
368 private final static String HOTSPOT_COMPILATION_MBEAN_NAME =
369 "sun.management:type=HotspotCompilation";
370
371 private final static String HOTSPOT_MEMORY_MBEAN_NAME =
372 "sun.management:type=HotspotMemory";
373
374 private static final String HOTSPOT_RUNTIME_MBEAN_NAME =
375 "sun.management:type=HotspotRuntime";
376
377 private final static String HOTSPOT_THREAD_MBEAN_NAME =
378 "sun.management:type=HotspotThreading";
379
380 final static String HOTSPOT_DIAGNOSTIC_COMMAND_MBEAN_NAME =
381 "com.sun.management:type=DiagnosticCommand";
382
383 public static HashMap<ObjectName, DynamicMBean> getPlatformDynamicMBeans() {
384 HashMap<ObjectName, DynamicMBean> map = new HashMap<>();
385 if (getDiagnosticCommandMBean() != null) {
386 map.put(Util.newObjectName(HOTSPOT_DIAGNOSTIC_COMMAND_MBEAN_NAME), getDiagnosticCommandMBean());
387 }
388 return map;
389 }
390
391 static void registerInternalMBeans(MBeanServer mbs) {
392 // register all internal MBeans if not registered
393 // No exception is thrown if a MBean with that object name
394 // already registered
395 addMBean(mbs, getHotspotClassLoadingMBean(),
396 HOTSPOT_CLASS_LOADING_MBEAN_NAME);
397 addMBean(mbs, getHotspotMemoryMBean(),
398 HOTSPOT_MEMORY_MBEAN_NAME);
399 addMBean(mbs, getHotspotRuntimeMBean(),
400 HOTSPOT_RUNTIME_MBEAN_NAME);
401 addMBean(mbs, getHotspotThreadMBean(),
402 HOTSPOT_THREAD_MBEAN_NAME);
403
404 // CompilationMBean may not exist
405 if (getCompilationMXBean() != null) {
406 addMBean(mbs, getHotspotCompilationMBean(),
407 HOTSPOT_COMPILATION_MBEAN_NAME);
408 }
409 }
410
|