1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6356458 27 * @summary test to not lose a user classloader 28 * @author Shanliang JIANG 29 * @run clean UserClassLoaderTest 30 * @run build UserClassLoaderTest 31 * @run main UserClassLoaderTest 32 */ 33 34 import java.util.*; 35 import java.net.*; 36 import java.io.IOException; 37 38 import javax.management.*; 39 import javax.management.remote.*; 40 41 public class UserClassLoaderTest { 42 private static final String[] protocols = {"rmi", "iiop", "jmxmp"}; 43 private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 44 private static ObjectName timer; 45 private final static NotificationListener listener = new NotificationListener() { 46 public void handleNotification(Notification notification, Object handback) { 47 } 48 }; 49 50 public static void main(String[] args) throws Exception { 51 System.out.println("main: we should not lose client classloader."); 52 53 timer = new ObjectName("test:name=timer"); 54 mbs.createMBean("javax.management.timer.Timer", timer); 55 56 boolean ok = true; 57 for (int i = 0; i < protocols.length; i++) { 58 try { 59 if (!test(protocols[i])) { 60 System.out.println("main: Test failed for " + protocols[i]); 61 ok = false; 62 } else { 63 System.out.println("main: Test successed for " + protocols[i]); 64 } 65 } catch (Exception e) { 66 System.out.println("main: Test failed for " + protocols[i]); 67 e.printStackTrace(System.out); 68 ok = false; 69 } } 70 71 if (ok) { 72 System.out.println("main: Tests passed"); 73 } else { 74 System.out.println("main: Tests FAILED"); 75 System.exit(1); 76 } 77 } 78 79 private static boolean test(String proto) throws Exception { 80 System.out.println("\ntest: Test for protocol " + proto); 81 82 JMXServiceURL u = null; 83 JMXConnectorServer server = null; 84 85 try { 86 u = new JMXServiceURL(proto, null, 0); 87 server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs); 88 } catch (MalformedURLException e) { 89 System.out.println("Skipping unsupported URL " + proto); 90 return true; 91 } 92 93 server.start(); 94 u = server.getAddress(); 95 96 System.out.println("test: create a server on: "+u); 97 98 JMXConnector client = JMXConnectorFactory.connect(u, null); 99 MBeanServerConnection conn = client.getMBeanServerConnection(); 100 101 final ClassLoader orgCL = Thread.currentThread().getContextClassLoader(); 102 System.out.println("test: the orginal classloader is "+orgCL); 103 104 final URL url = new URL("file:/xxx"); 105 final ClassLoader newCL = new URLClassLoader(new URL[]{url}, orgCL); 106 107 try { 108 System.out.println("test: set classloader to "+newCL); 109 Thread.currentThread().setContextClassLoader(newCL); 110 111 // reproduce the bug 112 conn.addNotificationListener(timer, listener, null, null); 113 114 client.close(); 115 server.stop(); 116 117 if (Thread.currentThread().getContextClassLoader() != newCL) { 118 System.out.println("ERROR: The client class loader is lost."); 119 120 return false; 121 } else { 122 System.out.println("test: Bye bye."); 123 124 return true; 125 } 126 } finally { 127 Thread.currentThread().setContextClassLoader(orgCL); 128 } 129 } 130 }