1 /*
   2  * Copyright (c) 2000, 2014, 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 /* @test
  25  * @bug 4289544
  26  * @summary ActivationGroupImpl.newInstance does not set context classloader for impl
  27  * @author Laird Dornin; code borrowed from Ann Wollrath
  28  *
  29  * @library ../../../testlibrary
  30  * @modules java.rmi/sun.rmi.registry
  31  *          java.rmi/sun.rmi.server
  32  *          java.rmi/sun.rmi.transport
  33  *          java.rmi/sun.rmi.transport.tcp
  34  *          java.base/sun.nio.ch
  35  * @build TestLibrary RMID RMIDSelectorProvider
  36  *     MyRMI ActivatableImpl ActivatableImpl ActivatableImpl_Stub
  37  * @run main/othervm/policy=security.policy/timeout=150 CheckImplClassLoader
  38  */
  39 
  40 import java.io.*;
  41 import java.rmi.*;
  42 import java.rmi.server.*;
  43 import java.rmi.activation.*;
  44 import java.net.URL;
  45 
  46 /**
  47  * sun.rmi.server.ActivationGroupImpl.newInstance() needs to set the
  48  * context class loader when it constructs the implementation class of
  49  * an Activatable object.  It needs to set the ccl to be the class
  50  * loader of the implementation class.
  51  *
  52  * Test creates an Activatable object whose impl is loaded outside of
  53  * CLASSPATH.  The impls constructor checks to make sure that the
  54  * correct context class loader has been set when the constructor is
  55  * invoked.
  56  */
  57 public class CheckImplClassLoader {
  58 
  59     private static Object dummy = new Object();
  60     private static MyRMI myRMI = null;
  61     private static ActivationGroup group = null;
  62 
  63     public static void main(String args[]) {
  64         /*
  65          * The following line is required with the JDK 1.2 VM because
  66          * of gc hocus pocus that may no longer be needed with an
  67          * exact vm (hotspot).
  68          */
  69         Object dummy1 = new Object();
  70         RMID rmid = null;
  71 
  72         System.err.println("\nRegression test for bug/rfe 4289544\n");
  73 
  74         try {
  75 
  76             URL implcb = TestLibrary.installClassInCodebase("ActivatableImpl",
  77                                                             "implcb");
  78             TestLibrary.installClassInCodebase("ActivatableImpl_Stub",
  79                                                "implcb");
  80             TestLibrary.suggestSecurityManager(
  81                 TestParams.defaultSecurityManager);
  82 
  83             RMID.removeLog();
  84             rmid = RMID.createRMIDOnEphemeralPort();
  85             rmid.start();
  86 
  87             System.err.println("Create activation group in this VM");
  88             ActivationGroupDesc groupDesc =
  89                 new ActivationGroupDesc(null, null);
  90             ActivationSystem system = ActivationGroup.getSystem();
  91             ActivationGroupID groupID = system.registerGroup(groupDesc);
  92             group = ActivationGroup.createGroup(groupID, groupDesc, 0);
  93 
  94             ActivationDesc desc = new ActivationDesc("ActivatableImpl",
  95                                                      implcb.toString(), null);
  96             myRMI = (MyRMI) Activatable.register(desc);
  97 
  98             System.err.println("Checking that impl has correct " +
  99                                "context class loader");
 100             if (!myRMI.classLoaderOk()) {
 101                 TestLibrary.bomb("incorrect context class loader for " +
 102                                  "activation constructor");
 103             }
 104 
 105             System.err.println("Deactivate object via method call");
 106             myRMI.shutdown();
 107 
 108             System.err.println("\nsuccess: CheckImplClassLoader test passed ");
 109 
 110         } catch (Exception e) {
 111             TestLibrary.bomb("\nfailure: unexpected exception ", e);
 112         } finally {
 113             try {
 114                 Thread.sleep(4000);
 115             } catch (InterruptedException e) {
 116             }
 117 
 118             myRMI = null;
 119             System.err.println("rmid shut down");
 120             rmid.cleanup();
 121             TestLibrary.unexport(group);
 122         }
 123     }
 124 }