Path: blob/master/test/jdk/javax/management/remote/mandatory/loading/TargetMBeanTest.java
41159 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 491042826* @summary Tests target MBean class loader used before JSR 160 loader27* @author Eamonn McManus28*29* @run clean TargetMBeanTest30* @run build TargetMBeanTest31* @run main TargetMBeanTest32*/3334/*35The JSR 160 spec says that, when invoking a method (or setting an36attribute or creating) on a target MBean, that MBean's class loader37is used to deserialize parameters. The problem is that the RMI38connector protocol wraps these parameters as MarshalledObjects.39When you call get() on a MarshalledObject, the context class loader40is used to deserialize, so if we set this to the target MBean's41class loader everything should work. EXCEPT that MarshalledObject42first tries to load classes using the first class loader it finds in43the caller's stack. If our JSR 160 implementation is part of J2SE,44it will not find any such class loader (only the system class45loader). But if it's standalone, then it will find the class loader46of the JSR 160 implementation. If the class name of a parameter is47known to both the 160 loader and the target MBean loader, then we48will use the wrong loader for deserialization and the attempt to49invoke the target MBean with the deserialized object will fail.5051We test this as follows. We fabricate an MLet that has the same set52of URLs as the 160 class loader, which we assume is the system class53loader (or at least, it is a URLClassLoader). This MLet is54therefore a "shadow class loader" -- for every class name known to55the 160 class loader, it can load the same name, but the result is56not the same class, since it has not been loaded by the same loader.57Then, we use the MLet to create an RMIConnectorServer MBean. This58MBean is an instance of "shadow RMIConnectorServer", and its59constructor has a parameter of type "shadow JMXServiceURL". If the60constructor is invoked with "real JMXServiceURL" it will fail.6162While we are at it, we also test that the behaviour is correct for63the JMXMP protocol, if that optional protocol is present.64*/65import java.lang.reflect.*;66import java.net.*;67import java.util.*;68import javax.management.*;69import javax.management.loading.*;70import javax.management.remote.*;71import javax.management.remote.rmi.RMIConnectorServer;7273public class TargetMBeanTest {74private static final ObjectName mletName;75static {76try {77mletName = new ObjectName("x:type=mlet");78} catch (Exception e) {79e.printStackTrace();80throw new Error();81}82}8384public static void main(String[] args) throws Exception {85System.out.println("Test that target MBean class loader is used " +86"before JMX Remote API class loader");8788ClassLoader jmxRemoteClassLoader =89JMXServiceURL.class.getClassLoader();90if (jmxRemoteClassLoader == null) {91System.out.println("JMX Remote API loaded by bootstrap " +92"class loader, this test is irrelevant");93return;94}95if (!(jmxRemoteClassLoader instanceof URLClassLoader)) {96System.out.println("TEST INVALID: JMX Remote API not loaded by " +97"URLClassLoader");98System.exit(1);99}100101URLClassLoader jrcl = (URLClassLoader) jmxRemoteClassLoader;102URL[] urls = jrcl.getURLs();103PrivateMLet mlet = new PrivateMLet(urls, null, false);104Class shadowClass = mlet.loadClass(JMXServiceURL.class.getName());105if (shadowClass == JMXServiceURL.class) {106System.out.println("TEST INVALID: MLet got original " +107"JMXServiceURL not shadow");108System.exit(1);109}110111MBeanServer mbs = MBeanServerFactory.newMBeanServer();112mbs.registerMBean(mlet, mletName);113114final String[] protos = {"rmi", "iiop", "jmxmp"};115boolean ok = true;116for (int i = 0; i < protos.length; i++) {117try {118ok &= test(protos[i], mbs);119} catch (Exception e) {120System.out.println("TEST FAILED WITH EXCEPTION:");121e.printStackTrace(System.out);122ok = false;123}124}125126if (ok)127System.out.println("Test passed");128else {129System.out.println("TEST FAILED");130System.exit(1);131}132}133134private static boolean test(String proto, MBeanServer mbs)135throws Exception {136System.out.println("Testing for proto " + proto);137138JMXConnectorServer cs;139JMXServiceURL url = new JMXServiceURL(proto, null, 0);140try {141cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,142mbs);143} catch (MalformedURLException e) {144System.out.println("System does not recognize URL: " + url +145"; ignoring");146return true;147}148cs.start();149JMXServiceURL addr = cs.getAddress();150JMXServiceURL rmiurl = new JMXServiceURL("rmi", null, 0);151JMXConnector client = JMXConnectorFactory.connect(addr);152MBeanServerConnection mbsc = client.getMBeanServerConnection();153ObjectName on = new ObjectName("x:proto=" + proto + ",ok=yes");154mbsc.createMBean(RMIConnectorServer.class.getName(),155on,156mletName,157new Object[] {rmiurl, null},158new String[] {JMXServiceURL.class.getName(),159Map.class.getName()});160System.out.println("Successfully deserialized with " + proto);161mbsc.unregisterMBean(on);162163client.close();164cs.stop();165return true;166}167}168169170