Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/loading/UserClassLoaderTest.java
38867 views
/*1* Copyright (c) 2005, 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* @run clean UserClassLoaderTest29* @run build UserClassLoaderTest30* @run main UserClassLoaderTest31*/3233import java.util.*;34import java.net.*;35import java.io.IOException;3637import javax.management.*;38import javax.management.remote.*;3940public class UserClassLoaderTest {41private static final String[] protocols = {"rmi", "iiop", "jmxmp"};42private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();43private static ObjectName timer;44private final static NotificationListener listener = new NotificationListener() {45public void handleNotification(Notification notification, Object handback) {46}47};4849public static void main(String[] args) throws Exception {50System.out.println("main: we should not lose client classloader.");5152timer = new ObjectName("test:name=timer");53mbs.createMBean("javax.management.timer.Timer", timer);5455boolean ok = true;56for (int i = 0; i < protocols.length; i++) {57try {58if (!test(protocols[i])) {59System.out.println("main: Test failed for " + protocols[i]);60ok = false;61} else {62System.out.println("main: Test successed for " + protocols[i]);63}64} catch (Exception e) {65System.out.println("main: Test failed for " + protocols[i]);66e.printStackTrace(System.out);67ok = false;68} }6970if (ok) {71System.out.println("main: Tests passed");72} else {73System.out.println("main: Tests FAILED");74System.exit(1);75}76}7778private static boolean test(String proto) throws Exception {79System.out.println("\ntest: Test for protocol " + proto);8081JMXServiceURL u = null;82JMXConnectorServer server = null;8384try {85u = new JMXServiceURL(proto, null, 0);86server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);87} catch (MalformedURLException e) {88System.out.println("Skipping unsupported URL " + proto);89return true;90}9192server.start();93u = server.getAddress();9495System.out.println("test: create a server on: "+u);9697JMXConnector client = JMXConnectorFactory.connect(u, null);98MBeanServerConnection conn = client.getMBeanServerConnection();99100final ClassLoader orgCL = Thread.currentThread().getContextClassLoader();101System.out.println("test: the orginal classloader is "+orgCL);102103final URL url = new URL("file:/xxx");104final ClassLoader newCL = new URLClassLoader(new URL[]{url}, orgCL);105106try {107System.out.println("test: set classloader to "+newCL);108Thread.currentThread().setContextClassLoader(newCL);109110// reproduce the bug111conn.addNotificationListener(timer, listener, null, null);112113client.close();114server.stop();115116if (Thread.currentThread().getContextClassLoader() != newCL) {117System.out.println("ERROR: The client class loader is lost.");118119return false;120} else {121System.out.println("test: Bye bye.");122123return true;124}125} finally {126Thread.currentThread().setContextClassLoader(orgCL);127}128}129}130131132