0N/A/*
694N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6356458
0N/A * @summary test to not lose a user classloader
0N/A * @author Shanliang JIANG
289N/A * @run clean UserClassLoaderTest
1451N/A * @run build UserClassLoaderTest
0N/A * @run main UserClassLoaderTest
0N/A */
0N/A
0N/Aimport java.util.*;
694N/Aimport java.net.*;
694N/Aimport java.io.IOException;
0N/A
518N/Aimport javax.management.*;
0N/Aimport javax.management.remote.*;
518N/A
518N/Apublic class UserClassLoaderTest {
0N/A private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
0N/A private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A private static ObjectName timer;
0N/A private final static NotificationListener listener = new NotificationListener() {
0N/A public void handleNotification(Notification notification, Object handback) {
0N/A }
0N/A };
0N/A
518N/A public static void main(String[] args) throws Exception {
518N/A System.out.println("main: we should not lose client classloader.");
518N/A
518N/A timer = new ObjectName("test:name=timer");
289N/A mbs.createMBean("javax.management.timer.Timer", timer);
0N/A
0N/A boolean ok = true;
0N/A for (int i = 0; i < protocols.length; i++) {
0N/A try {
0N/A if (!test(protocols[i])) {
289N/A System.out.println("main: Test failed for " + protocols[i]);
289N/A ok = false;
289N/A } else {
289N/A System.out.println("main: Test successed for " + protocols[i]);
289N/A }
289N/A } catch (Exception e) {
289N/A System.out.println("main: Test failed for " + protocols[i]);
518N/A e.printStackTrace(System.out);
289N/A ok = false;
694N/A } }
289N/A
0N/A if (ok) {
0N/A System.out.println("main: Tests passed");
0N/A } else {
0N/A System.out.println("main: Tests FAILED");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A private static boolean test(String proto) throws Exception {
0N/A System.out.println("\ntest: Test for protocol " + proto);
0N/A
0N/A JMXServiceURL u = null;
0N/A JMXConnectorServer server = null;
0N/A
0N/A try {
0N/A u = new JMXServiceURL(proto, null, 0);
0N/A server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
0N/A } catch (MalformedURLException e) {
0N/A System.out.println("Skipping unsupported URL " + proto);
289N/A return true;
289N/A }
289N/A
289N/A server.start();
289N/A u = server.getAddress();
518N/A
289N/A System.out.println("test: create a server on: "+u);
289N/A
289N/A JMXConnector client = JMXConnectorFactory.connect(u, null);
289N/A MBeanServerConnection conn = client.getMBeanServerConnection();
289N/A
289N/A final ClassLoader orgCL = Thread.currentThread().getContextClassLoader();
289N/A System.out.println("test: the orginal classloader is "+orgCL);
289N/A
289N/A final URL url = new URL("file:/xxx");
289N/A final ClassLoader newCL = new URLClassLoader(new URL[]{url}, orgCL);
289N/A
0N/A try {
0N/A System.out.println("test: set classloader to "+newCL);
0N/A Thread.currentThread().setContextClassLoader(newCL);
0N/A
0N/A // reproduce the bug
0N/A conn.addNotificationListener(timer, listener, null, null);
0N/A
289N/A client.close();
0N/A server.stop();
0N/A
0N/A if (Thread.currentThread().getContextClassLoader() != newCL) {
0N/A System.out.println("ERROR: The client class loader is lost.");
0N/A
289N/A return false;
0N/A } else {
0N/A System.out.println("test: Bye bye.");
0N/A
0N/A return true;
289N/A }
289N/A } finally {
0N/A Thread.currentThread().setContextClassLoader(orgCL);
0N/A }
518N/A }
518N/A}
518N/A