changeset 7399:9114ea4791ec

8008585: Better JMX data handling Reviewed-by: alanb, dfuchs, jfdenise, skoivu, sjiang
author jbachorik
date Thu, 14 Mar 2013 14:42:12 +0100
parents 363547f54176
children 200ae4b8f192
files src/share/classes/javax/management/remote/JMXConnectorFactory.java
diffstat 1 files changed, 23 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/management/remote/JMXConnectorFactory.java	Tue Mar 12 11:09:37 2013 +0100
+++ b/src/share/classes/javax/management/remote/JMXConnectorFactory.java	Thu Mar 14 14:42:12 2013 +0100
@@ -39,6 +39,7 @@
 
 import com.sun.jmx.remote.util.ClassLogger;
 import com.sun.jmx.remote.util.EnvHelp;
+import sun.reflect.misc.ReflectUtil;
 
 
 /**
@@ -412,10 +413,10 @@
     }
 
     static <T> T getProvider(JMXServiceURL serviceURL,
-                             Map<String, Object> environment,
+                             final Map<String, Object> environment,
                              String providerClassName,
                              Class<T> targetInterface,
-                             ClassLoader loader)
+                             final ClassLoader loader)
             throws IOException {
 
         final String protocol = serviceURL.getProtocol();
@@ -425,11 +426,14 @@
         T instance = null;
 
         if (pkgs != null) {
-            environment.put(PROTOCOL_PROVIDER_CLASS_LOADER, loader);
-
             instance =
                 getProvider(protocol, pkgs, loader, providerClassName,
                             targetInterface);
+
+            if (instance != null) {
+                boolean needsWrap = (loader != instance.getClass().getClassLoader());
+                environment.put(PROTOCOL_PROVIDER_CLASS_LOADER, needsWrap ? wrap(loader) : loader);
+            }
         }
 
         return instance;
@@ -442,6 +446,21 @@
        return serviceLoader.iterator();
     }
 
+    private static ClassLoader wrap(final ClassLoader parent) {
+        return parent != null ? AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
+            @Override
+            public ClassLoader run() {
+                return new ClassLoader(parent) {
+                    @Override
+                    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
+                        ReflectUtil.checkPackageAccess(name);
+                        return super.loadClass(name, resolve);
+                    }
+                };
+            }
+        }) : null;
+    }
+
     private static JMXConnector getConnectorAsService(ClassLoader loader,
                                                       JMXServiceURL url,
                                                       Map<String, ?> map)
@@ -552,5 +571,4 @@
     private static String protocol2package(String protocol) {
         return protocol.replace('+', '.').replace('-', '_');
     }
-
 }