From 69718bb834c2a53a954b02bad623650158ff9d52 Mon Sep 17 00:00:00 2001 From: Jaroslav Bachorik Date: Thu, 14 Mar 2013 14:42:12 +0100 Subject: [PATCH] 8008585: Better JMX data handling Reviewed-by: alanb, dfuchs, jfdenise, skoivu, sjiang --- .../remote/JMXConnectorFactory.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java b/jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java index 4f6533a49af..f78ff505105 100644 --- a/jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java +++ b/jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java @@ -39,6 +39,7 @@ import java.security.PrivilegedAction; import com.sun.jmx.remote.util.ClassLogger; import com.sun.jmx.remote.util.EnvHelp; +import sun.reflect.misc.ReflectUtil; /** @@ -412,10 +413,10 @@ public class JMXConnectorFactory { } static T getProvider(JMXServiceURL serviceURL, - Map environment, + final Map environment, String providerClassName, Class targetInterface, - ClassLoader loader) + final ClassLoader loader) throws IOException { final String protocol = serviceURL.getProtocol(); @@ -425,11 +426,14 @@ public class JMXConnectorFactory { 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 @@ public class JMXConnectorFactory { return serviceLoader.iterator(); } + private static ClassLoader wrap(final ClassLoader parent) { + return parent != null ? AccessController.doPrivileged(new PrivilegedAction() { + @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 map) @@ -552,5 +571,4 @@ public class JMXConnectorFactory { private static String protocol2package(String protocol) { return protocol.replace('+', '.').replace('-', '_'); } - }