8008585: Better JMX data handling
authorjbachorik
Thu, 14 Mar 2013 14:42:12 +0100
changeset 18190 e45d26861c25
parent 18189 ef33730f6b2b
child 18191 be617b8c4427
8008585: Better JMX data handling Reviewed-by: alanb, dfuchs, jfdenise, skoivu, sjiang
jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java
--- a/jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java	Tue Mar 12 11:09:37 2013 +0100
+++ b/jdk/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('-', '_');
     }
-
 }