0N/A/*
4565N/A * Copyright (c) 1998, 2012, 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 *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/* @test
1879N/A * @bug 4171278
1879N/A * @summary A remote object's unreferenced() method should be invoked with the
1879N/A * context class loader set to the same context class loader that would be set
1879N/A * when remote calls for that object are being executed: the object's class's
2062N/A * class loader, or the context class loader set when the remote object was
1879N/A * exported, if it is a child of the remote object's class loader.
1879N/A * @author Peter Jones
1879N/A *
1879N/A * @bug 4214123
3932N/A * @summary Unreferenced.unreferenced(...) threads should run in the nonSystem group.
1879N/A * To complete the fix for, 4182104, RMI unreferenced threads should also
0N/A * run in the nonSystem so that they do not need permissions to modify the
0N/A * system thread group.
0N/A *
0N/A * @author Laird Dornin
0N/A *
0N/A * @library ../../../testlibrary
0N/A * @build TestLibrary UnreferencedContext_Stub
0N/A * @run main/othervm/timeout=120 UnreferencedContext
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.rmi.*;
0N/Aimport java.rmi.registry.*;
0N/Aimport java.rmi.server.*;
0N/A
0N/Apublic class UnreferencedContext implements Remote, Unreferenced, Runnable {
0N/A
0N/A private final static String BINDING = "UnreferencedContext";
0N/A private final static long GC_INTERVAL = 6000;
0N/A private final static long TIMEOUT = 60000;
0N/A
0N/A private Object lock = new Object();
0N/A private boolean unreferencedInvoked = false;
0N/A private ClassLoader unreferencedContext;
0N/A
0N/A public void run() {
0N/A System.err.println("unreferenced method created thread succesfully");
0N/A }
0N/A
0N/A public void unreferenced() {
0N/A // turn on security to ensure that the action below will not
0N/A // require extra permissions
0N/A System.setSecurityManager(new java.rmi.RMISecurityManager());
0N/A
0N/A // exercise functionality prohibited by 4214123
0N/A (new Thread(this)).start();
0N/A
0N/A System.err.println("unreferenced() method invoked");
0N/A synchronized (lock) {
0N/A unreferencedInvoked = true;
0N/A unreferencedContext =
0N/A Thread.currentThread().getContextClassLoader();
0N/A lock.notify();
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A
0N/A System.err.println("\nRegression test for bug 4171278\n");
0N/A
0N/A /*
0N/A * Set the interval that RMI will request for GC latency (before RMI
0N/A * gets initialized and this property is read) to an unrealistically
0N/A * small value, so that this test shouldn't have to wait too long.
0N/A */
0N/A System.setProperty("sun.rmi.dgc.client.gcInterval",
0N/A String.valueOf(GC_INTERVAL));
0N/A
0N/A UnreferencedContext obj = new UnreferencedContext();
0N/A
0N/A try {
0N/A /*
0N/A * This little trick is necessary to make sure that the RMI server
0N/A * threads for objects created on the default port get created
0N/A * before we set our special context class loader, so that they
0N/A * don't *accidentally* inherit it when making the unreferenced()
0N/A * callback.
0N/A */
0N/A UnicastRemoteObject.exportObject(obj);
0N/A UnicastRemoteObject.unexportObject(obj, true);
0N/A
0N/A /*
0N/A * Now create special context class loader before exporting the
0N/A * remote object for real, so that it should be set when the
0N/A * object's unreferenced() method is called.
0N/A */
0N/A ClassLoader intendedContext = new URLClassLoader(new URL[0]);
0N/A Thread.currentThread().setContextClassLoader(intendedContext);
0N/A System.err.println(
0N/A "created and set intended context class loader: " +
0N/A intendedContext);
0N/A
0N/A UnicastRemoteObject.exportObject(obj);
0N/A System.err.println("exported remote object");
0N/A
0N/A Registry registry1 = TestLibrary.createRegistryOnUnusedPort();
0N/A int port = TestLibrary.getRegistryPort(registry1);
0N/A System.err.println("created registry");
0N/A
0N/A Registry registry = LocateRegistry.getRegistry("", port);
0N/A registry.bind(BINDING, obj);
2062N/A System.err.println("bound remote object in registry");
0N/A
0N/A synchronized (obj.lock) {
0N/A registry.unbind(BINDING);
0N/A System.err.println("unbound remote object from registry; " +
0N/A "waiting for unreferenced() callback...");
0N/A /*
0N/A * This incantation seems sufficient to work around the
0N/A * ramifications of 4164696, so that this test will actually
0N/A * prove something useful about 1.2Beta4 or 1.2FCS before
0N/A * 4171278 was fixed.
0N/A */
0N/A for (int i = 0; i < 10; i++) {
0N/A System.gc();
0N/A obj.lock.wait(TIMEOUT / 10);
0N/A if (obj.unreferencedInvoked) {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (obj.unreferencedInvoked) {
0N/A System.err.println(
0N/A "invoked with context class loader: " +
0N/A obj.unreferencedContext);
0N/A
0N/A if (obj.unreferencedContext == intendedContext) {
0N/A System.err.println(
0N/A "TEST PASSED: unreferenced() invoked" +
0N/A " with intended context class loader");
0N/A } else {
0N/A throw new RuntimeException(
0N/A "TEST FAILED: unreferenced() invoked" +
0N/A " with incorrect context class loader");
0N/A }
0N/A } else {
0N/A throw new RuntimeException(
0N/A "TEST FAILED: unreferenced() not invoked after " +
0N/A ((double) TIMEOUT / 1000.0) + " seconds or unreferenced failed to create a thread");
0N/A }
0N/A }
0N/A
0N/A } catch (Exception e) {
0N/A if (e instanceof RuntimeException) {
0N/A throw (RuntimeException) e;
0N/A } else {
0N/A throw new RuntimeException(
0N/A "TEST FAILED: unexpected exception: " + e.toString());
0N/A }
0N/A } finally {
0N/A /*
0N/A * When all is said and done, try to unexport the remote object
0N/A * so that the VM has a chance to exit.
0N/A */
0N/A try {
2062N/A UnicastRemoteObject.unexportObject(obj, true);
0N/A } catch (RemoteException e) {
0N/A }
0N/A }
0N/A }
0N/A}
0N/A