Path: blob/master/test/jdk/javax/management/remote/mandatory/loading/UserClassLoaderTest.java
41159 views
/*1* Copyright (c) 2005, 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 635645826* @summary test to not lose a user classloader27* @author Shanliang JIANG28*29* @run clean UserClassLoaderTest30* @run build UserClassLoaderTest31* @run main UserClassLoaderTest32*/3334import java.util.*;35import java.net.*;36import java.io.IOException;3738import javax.management.*;39import javax.management.remote.*;4041public class UserClassLoaderTest {42private static final String[] protocols = {"rmi", "iiop", "jmxmp"};43private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();44private static ObjectName timer;45private final static NotificationListener listener = new NotificationListener() {46public void handleNotification(Notification notification, Object handback) {47}48};4950public static void main(String[] args) throws Exception {51System.out.println("main: we should not lose client classloader.");5253timer = new ObjectName("test:name=timer");54mbs.createMBean("javax.management.timer.Timer", timer);5556boolean ok = true;57for (int i = 0; i < protocols.length; i++) {58try {59if (!test(protocols[i])) {60System.out.println("main: Test failed for " + protocols[i]);61ok = false;62} else {63System.out.println("main: Test successed for " + protocols[i]);64}65} catch (Exception e) {66System.out.println("main: Test failed for " + protocols[i]);67e.printStackTrace(System.out);68ok = false;69} }7071if (ok) {72System.out.println("main: Tests passed");73} else {74System.out.println("main: Tests FAILED");75System.exit(1);76}77}7879private static boolean test(String proto) throws Exception {80System.out.println("\ntest: Test for protocol " + proto);8182JMXServiceURL u = null;83JMXConnectorServer server = null;8485try {86u = new JMXServiceURL(proto, null, 0);87server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);88} catch (MalformedURLException e) {89System.out.println("Skipping unsupported URL " + proto);90return true;91}9293server.start();94u = server.getAddress();9596System.out.println("test: create a server on: "+u);9798JMXConnector client = JMXConnectorFactory.connect(u, null);99MBeanServerConnection conn = client.getMBeanServerConnection();100101final ClassLoader orgCL = Thread.currentThread().getContextClassLoader();102System.out.println("test: the orginal classloader is "+orgCL);103104final URL url = new URL("file:/xxx");105final ClassLoader newCL = new URLClassLoader(new URL[]{url}, orgCL);106107try {108System.out.println("test: set classloader to "+newCL);109Thread.currentThread().setContextClassLoader(newCL);110111// reproduce the bug112conn.addNotificationListener(timer, listener, null, null);113114client.close();115server.stop();116117if (Thread.currentThread().getContextClassLoader() != newCL) {118System.out.println("ERROR: The client class loader is lost.");119120return false;121} else {122System.out.println("test: Bye bye.");123124return true;125}126} finally {127Thread.currentThread().setContextClassLoader(orgCL);128}129}130}131132133