8151099: java.lang.management.ManagementFactory.getPlatformMXBeans() should work even if jdk.management is not present.
authorvtewari
Wed, 26 Oct 2016 15:08:29 +0530
changeset 42102 3e0a2861efe1
parent 42101 e75889484926
child 42103 8e1fe4345fd7
8151099: java.lang.management.ManagementFactory.getPlatformMXBeans() should work even if jdk.management is not present. Summary: Removed dependency of java.management over jdk.management. Reviewed-by: mchung, dfuchs, dholmes Contributed-by: amit.sapre@oracle.com
jdk/src/java.management/share/classes/sun/management/VMManagementImpl.java
jdk/src/java.management/share/native/libmanagement/VMManagementImpl.c
jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java
jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.java
jdk/test/java/lang/management/ManagementFactory/DefaultManagementProviderTest.java
--- a/jdk/src/java.management/share/classes/sun/management/VMManagementImpl.java	Tue Oct 25 14:49:35 2016 +0300
+++ b/jdk/src/java.management/share/classes/sun/management/VMManagementImpl.java	Wed Oct 26 15:08:29 2016 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -102,7 +102,13 @@
     }
 
     public boolean isGcNotificationSupported() {
-        return gcNotificationSupport;
+        boolean isSupported = true;
+        try {
+            Class.forName("com.sun.management.GarbageCollectorMXBean");
+        } catch (ClassNotFoundException x) {
+            isSupported = false;
+        }
+        return isSupported;
     }
 
     public boolean isRemoteDiagnosticCommandsSupported() {
--- a/jdk/src/java.management/share/native/libmanagement/VMManagementImpl.c	Tue Oct 25 14:49:35 2016 +0300
+++ b/jdk/src/java.management/share/native/libmanagement/VMManagementImpl.c	Wed Oct 26 15:08:29 2016 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -96,13 +96,6 @@
 
     value = mos.isRemoteDiagnosticCommandsSupported;
     setStaticBooleanField(env, cls, "remoteDiagnosticCommandsSupport", value);
-
-    if ((jmm_version > JMM_VERSION_1_2) ||
-        (jmm_version == JMM_VERSION_1_2 && ((jmm_version&0xFF) >= 1))) {
-        setStaticBooleanField(env, cls, "gcNotificationSupport", JNI_TRUE);
-    } else {
-        setStaticBooleanField(env, cls, "gcNotificationSupport", JNI_FALSE);
-    }
 }
 
 JNIEXPORT jobjectArray JNICALL
--- a/jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java	Tue Oct 25 14:49:35 2016 +0300
+++ b/jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java	Wed Oct 26 15:08:29 2016 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -72,22 +72,9 @@
 
     public static void main(String[] args) throws Exception {
         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-        final Boolean isNotificationSupported = AccessController.doPrivileged (new PrivilegedAction<Boolean>() {
-                public Boolean run() {
-                    try {
-                        Class cl = Class.forName("sun.management.VMManagementImpl");
-                        Field f = cl.getDeclaredField("gcNotificationSupport");
-                        f.setAccessible(true);
-                        return f.getBoolean(null);
-                    } catch(ClassNotFoundException e) {
-                        return false;
-                    } catch(NoSuchFieldException e) {
-                        return false;
-                    } catch(IllegalAccessException e) {
-                        return false;
-                    }
-                }
-            });
+        final boolean isNotificationSupported =
+                 sun.management.ManagementFactoryHelper.getVMManagement().isGcNotificationSupported();
+
         if(!isNotificationSupported) {
             System.out.println("GC Notification not supported by the JVM, test skipped");
             return;
--- a/jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.java	Tue Oct 25 14:49:35 2016 +0300
+++ b/jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.java	Wed Oct 26 15:08:29 2016 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -71,22 +71,9 @@
 
     public static void main(String[] args) throws Exception {
         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-        final Boolean isNotificationSupported = AccessController.doPrivileged (new PrivilegedAction<Boolean>() {
-                public Boolean run() {
-                    try {
-                        Class cl = Class.forName("sun.management.VMManagementImpl");
-                        Field f = cl.getDeclaredField("gcNotificationSupport");
-                        f.setAccessible(true);
-                        return f.getBoolean(null);
-                    } catch(ClassNotFoundException e) {
-                        return false;
-                    } catch(NoSuchFieldException e) {
-                        return false;
-                    } catch(IllegalAccessException e) {
-                        return false;
-                    }
-                }
-            });
+        final boolean isNotificationSupported =
+                 sun.management.ManagementFactoryHelper.getVMManagement().isGcNotificationSupported();
+
         if(!isNotificationSupported) {
             System.out.println("GC Notification not supported by the JVM, test skipped");
             return;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/management/ManagementFactory/DefaultManagementProviderTest.java	Wed Oct 26 15:08:29 2016 +0530
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8151099
+ * @summary Verify platform MXBeans initialized properly with java.management
+ *          module only. No other management provider
+ * @run main/othervm --limit-modules=java.management DefaultManagementProviderTest
+ */
+import java.lang.management.ManagementFactory;
+
+public class DefaultManagementProviderTest {
+    public static void main(String[] argv) {
+        ManagementFactory.getPlatformMBeanServer();
+        System.out.println("Test case passed");
+    }
+}