1 /*
   2  * Copyright (c) 1998, 2012, 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 4171278
  26  * @summary A remote object's unreferenced() method should be invoked with the
  27  * context class loader set to the same context class loader that would be set
  28  * when remote calls for that object are being executed: the object's class's
  29  * class loader, or the context class loader set when the remote object was
  30  * exported, if it is a child of the remote object's class loader.
  31  * @author Peter Jones
  32  *
  33  * @bug 4214123
  34  * @summary Unreferenced.unreferenced(...) threads should run in the nonSystem group.
  35  *          To complete the fix for, 4182104, RMI unreferenced threads should also
  36  *          run in the nonSystem so that they do not need permissions to modify the
  37  *          system thread group.
  38  *
  39  * @author Laird Dornin
  40  *
  41  * @library ../../../testlibrary
  42  * @build UnreferencedContext
  43  * @build UnreferencedContext_Stub
  44  * @build TestLibrary
  45  * @run main/othervm/timeout=120 UnreferencedContext
  46  */
  47 
  48 import java.net.*;
  49 import java.rmi.*;
  50 import java.rmi.registry.*;
  51 import java.rmi.server.*;
  52 
  53 public class UnreferencedContext implements Remote, Unreferenced, Runnable {
  54 
  55     private final static String BINDING = "UnreferencedContext";
  56     private final static long GC_INTERVAL = 6000;
  57     private final static long TIMEOUT = 60000;
  58 
  59     private Object lock = new Object();
  60     private boolean unreferencedInvoked = false;
  61     private ClassLoader unreferencedContext;
  62 
  63     public void run() {
  64         System.err.println("unreferenced method created thread succesfully");
  65     }
  66 
  67     public void unreferenced() {
  68         // turn on security to ensure that the action below will not
  69         // require extra permissions
  70         System.setSecurityManager(new java.rmi.RMISecurityManager());
  71 
  72         // exercise functionality prohibited by 4214123
  73         (new Thread(this)).start();
  74 
  75         System.err.println("unreferenced() method invoked");
  76         synchronized (lock) {
  77             unreferencedInvoked = true;
  78             unreferencedContext =
  79                 Thread.currentThread().getContextClassLoader();
  80             lock.notify();
  81         }
  82     }
  83 
  84     public static void main(String[] args) {
  85 
  86         System.err.println("\nRegression test for bug 4171278\n");
  87 
  88         /*
  89          * Set the interval that RMI will request for GC latency (before RMI
  90          * gets initialized and this property is read) to an unrealistically
  91          * small value, so that this test shouldn't have to wait too long.
  92          */
  93         System.setProperty("sun.rmi.dgc.client.gcInterval",
  94             String.valueOf(GC_INTERVAL));
  95 
  96         UnreferencedContext obj = new UnreferencedContext();
  97 
  98         try {
  99             /*
 100              * This little trick is necessary to make sure that the RMI server
 101              * threads for objects created on the default port get created
 102              * before we set our special context class loader, so that they
 103              * don't *accidentally* inherit it when making the unreferenced()
 104              * callback.
 105              */
 106             UnicastRemoteObject.exportObject(obj);
 107             UnicastRemoteObject.unexportObject(obj, true);
 108 
 109             /*
 110              * Now create special context class loader before exporting the
 111              * remote object for real, so that it should be set when the
 112              * object's unreferenced() method is called.
 113              */
 114             ClassLoader intendedContext = new URLClassLoader(new URL[0]);
 115             Thread.currentThread().setContextClassLoader(intendedContext);
 116             System.err.println(
 117                 "created and set intended context class loader: " +
 118                 intendedContext);
 119 
 120             UnicastRemoteObject.exportObject(obj);
 121             System.err.println("exported remote object");
 122 
 123             Registry registry1 = TestLibrary.createRegistryOnUnusedPort();
 124             int port = TestLibrary.getRegistryPort(registry1);
 125             System.err.println("created registry");
 126 
 127             Registry registry = LocateRegistry.getRegistry("", port);
 128             registry.bind(BINDING, obj);
 129             System.err.println("bound remote object in registry");
 130 
 131             synchronized (obj.lock) {
 132                 registry.unbind(BINDING);
 133                 System.err.println("unbound remote object from registry; " +
 134                     "waiting for unreferenced() callback...");
 135                 /*
 136                  * This incantation seems sufficient to work around the
 137                  * ramifications of 4164696, so that this test will actually
 138                  * prove something useful about 1.2Beta4 or 1.2FCS before
 139                  * 4171278 was fixed.
 140                  */
 141                 for (int i = 0; i < 10; i++) {
 142                     System.gc();
 143                     obj.lock.wait(TIMEOUT / 10);
 144                     if (obj.unreferencedInvoked) {
 145                         break;
 146                     }
 147                 }
 148 
 149                 if (obj.unreferencedInvoked) {
 150                     System.err.println(
 151                         "invoked with context class loader: " +
 152                         obj.unreferencedContext);
 153 
 154                     if (obj.unreferencedContext == intendedContext) {
 155                         System.err.println(
 156                             "TEST PASSED: unreferenced() invoked" +
 157                             " with intended context class loader");
 158                     } else {
 159                         throw new RuntimeException(
 160                             "TEST FAILED: unreferenced() invoked" +
 161                             " with incorrect context class loader");
 162                     }
 163                 } else {
 164                     throw new RuntimeException(
 165                         "TEST FAILED: unreferenced() not invoked after " +
 166                         ((double) TIMEOUT / 1000.0) + " seconds or unreferenced failed to create a thread");
 167                 }
 168             }
 169 
 170         } catch (Exception e) {
 171             if (e instanceof RuntimeException) {
 172                 throw (RuntimeException) e;
 173             } else {
 174                 throw new RuntimeException(
 175                     "TEST FAILED: unexpected exception: " + e.toString());
 176             }
 177         } finally {
 178             /*
 179              * When all is said and done, try to unexport the remote object
 180              * so that the VM has a chance to exit.
 181              */
 182             try {
 183                 UnicastRemoteObject.unexportObject(obj, true);
 184             } catch (RemoteException e) {
 185             }
 186         }
 187     }
 188 }