169N/A/*
2362N/A * Copyright (c) 2000, 2008, 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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary When an object is retrieved from a MarshalledObject, callbacks
0N/A * that were registered by objects in the graph for execution when the
0N/A * unmarshalling is done should get executed. This is verified by way of
0N/A * an exported object's stub getting unmarshalled, and then garbage
0N/A * collected, in which case the impl's unreferenced() method should get
0N/A * invoked.
0N/A * @author Peter Jones
0N/A *
0N/A * @build MarshalledObjectGet_Stub
0N/A * @run main/othervm/timeout=120 MarshalledObjectGet
0N/A */
0N/A
0N/Aimport java.rmi.MarshalledObject;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/Aimport java.rmi.server.Unreferenced;
0N/A
0N/Apublic class MarshalledObjectGet implements Remote, Unreferenced {
0N/A
0N/A private static final String BINDING = "MarshalledObjectGet";
0N/A private static final long GC_INTERVAL = 6000;
0N/A private static final long TIMEOUT = 50000;
0N/A
0N/A private Object lock = new Object();
0N/A private boolean unreferencedInvoked;
0N/A
0N/A public void unreferenced() {
169N/A System.err.println("unreferenced() method invoked");
169N/A synchronized (lock) {
169N/A unreferencedInvoked = true;
169N/A lock.notify();
169N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A
169N/A System.err.println(
169N/A "\nTest to verify correction interaction of " +
169N/A "MarshalledObject.get and DGC registration\n");
0N/A
169N/A /*
169N/A * Set the interval that RMI will request for GC latency (before RMI
169N/A * gets initialized and this property is read) to an unrealistically
169N/A * small value, so that this test shouldn't have to wait too long.
169N/A */
169N/A System.setProperty("sun.rmi.dgc.client.gcInterval",
169N/A String.valueOf(GC_INTERVAL));
0N/A
169N/A MarshalledObjectGet obj = new MarshalledObjectGet();
0N/A
169N/A try {
169N/A Remote stub = UnicastRemoteObject.exportObject(obj);
169N/A System.err.println("exported remote object");
0N/A
169N/A MarshalledObject mobj = new MarshalledObject(stub);
169N/A Remote unmarshalledStub = (Remote) mobj.get();
169N/A System.err.println("unmarshalled stub from marshalled object");
0N/A
169N/A synchronized (obj.lock) {
169N/A obj.unreferencedInvoked = false;
0N/A
169N/A unmarshalledStub = null;
169N/A System.gc();
169N/A System.err.println("cleared unmarshalled stub");
169N/A System.err.println("waiting for unreferenced() callback " +
169N/A "(SHOULD happen)...");
169N/A obj.lock.wait(TIMEOUT);
0N/A
169N/A if (obj.unreferencedInvoked) {
169N/A // TEST PASSED
169N/A } else {
169N/A throw new RuntimeException(
169N/A "TEST FAILED: unrefereced() not invoked after " +
169N/A ((double) TIMEOUT / 1000.0) + " seconds");
169N/A }
169N/A }
0N/A
169N/A System.err.println("TEST PASSED");
0N/A
169N/A } catch (Exception e) {
169N/A if (e instanceof RuntimeException) {
169N/A throw (RuntimeException) e;
169N/A } else {
169N/A throw new RuntimeException(
169N/A "TEST FAILED: unexpected exception: " + e.toString());
169N/A }
169N/A } finally {
169N/A if (obj != null) {
169N/A try {
169N/A UnicastRemoteObject.unexportObject(obj, true);
169N/A } catch (Exception e) {
169N/A }
169N/A }
169N/A }
0N/A }
0N/A}