1 /*
   2  * Copyright (c) 2005, 2015, 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  * @modules java.management
  30  * @run clean UserClassLoaderTest
  31  * @run build UserClassLoaderTest
  32  * @run main UserClassLoaderTest
  33  */
  34 
  35 import java.util.*;
  36 import java.net.*;
  37 import java.io.IOException;
  38 
  39 import javax.management.*;
  40 import javax.management.remote.*;
  41 
  42 public class UserClassLoaderTest {
  43     private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
  44     private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  45     private static ObjectName timer;
  46     private final static NotificationListener listener = new NotificationListener() {
  47             public void handleNotification(Notification notification, Object handback) {
  48             }
  49         };
  50 
  51     public static void main(String[] args) throws Exception {
  52         System.out.println("main: we should not lose client classloader.");
  53 
  54         timer = new ObjectName("test:name=timer");
  55         mbs.createMBean("javax.management.timer.Timer", timer);
  56 
  57         boolean ok = true;
  58         for (int i = 0; i < protocols.length; i++) {
  59             try {
  60                 if (!test(protocols[i])) {
  61                     System.out.println("main: Test failed for " + protocols[i]);
  62                     ok = false;
  63                 } else {
  64                     System.out.println("main: Test successed for " + protocols[i]);
  65                 }
  66             } catch (Exception e) {
  67                 System.out.println("main: Test failed for " + protocols[i]);
  68                 e.printStackTrace(System.out);
  69                 ok = false;
  70             }        }
  71 
  72         if (ok) {
  73             System.out.println("main: Tests passed");
  74         } else {
  75             System.out.println("main: Tests FAILED");
  76             System.exit(1);
  77         }
  78     }
  79 
  80     private static boolean test(String proto) throws Exception {
  81         System.out.println("\ntest: Test for protocol " + proto);
  82 
  83         JMXServiceURL u = null;
  84         JMXConnectorServer server = null;
  85 
  86         try {
  87             u = new JMXServiceURL(proto, null, 0);
  88             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
  89         } catch (MalformedURLException e) {
  90             System.out.println("Skipping unsupported URL " + proto);
  91             return true;
  92         }
  93 
  94         server.start();
  95         u = server.getAddress();
  96 
  97         System.out.println("test: create a server on: "+u);
  98 
  99         JMXConnector client = JMXConnectorFactory.connect(u, null);
 100         MBeanServerConnection conn = client.getMBeanServerConnection();
 101 
 102         final ClassLoader orgCL = Thread.currentThread().getContextClassLoader();
 103         System.out.println("test: the orginal classloader is "+orgCL);
 104 
 105         final URL url = new URL("file:/xxx");
 106         final ClassLoader newCL = new URLClassLoader(new URL[]{url}, orgCL);
 107 
 108         try {
 109             System.out.println("test: set classloader to "+newCL);
 110             Thread.currentThread().setContextClassLoader(newCL);
 111 
 112             // reproduce the bug
 113             conn.addNotificationListener(timer, listener, null, null);
 114 
 115             client.close();
 116             server.stop();
 117 
 118             if (Thread.currentThread().getContextClassLoader() != newCL) {
 119                 System.out.println("ERROR: The client class loader is lost.");
 120 
 121                 return false;
 122             } else {
 123                 System.out.println("test: Bye bye.");
 124 
 125                 return true;
 126             }
 127         } finally {
 128             Thread.currentThread().setContextClassLoader(orgCL);
 129         }
 130     }
 131 }