0N/A/*
2362N/A * Copyright (c) 2005, 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
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/* @test
0N/A * @bug 4486732
0N/A * @summary When a remote stub contains a client socket factory and a
0N/A * remote invocation is made using that stub, the factory should not
0N/A * be held strongly reachable by the RMI implementation forever; in
0N/A * particular, after the stub has become unreachable and all
0N/A * connections to its endpoint have been closed, then the factory
0N/A * should become unreachable too (through the RMI implementation).
0N/A * @author Peter Jones
0N/A *
0N/A * @library ../../testlibrary
0N/A * @build TestLibrary
0N/A * @run main/othervm -Dsun.rmi.transport.connectionTimeout=2000
0N/A * PinClientSocketFactory
0N/A */
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.Serializable;
0N/Aimport java.lang.ref.Reference;
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.net.ServerSocket;
0N/Aimport java.net.Socket;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.rmi.registry.Registry;
0N/Aimport java.rmi.server.RMIClientSocketFactory;
0N/Aimport java.rmi.server.RMIServerSocketFactory;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.List;
0N/Aimport java.util.concurrent.atomic.AtomicInteger;
0N/A
0N/Apublic class PinClientSocketFactory {
0N/A
0N/A private static final int PORT = TestLibrary.getUnusedRandomPort();
0N/A private static final int SESSIONS = 50;
0N/A
0N/A public interface Factory extends Remote {
0N/A Session getSession() throws RemoteException;
0N/A }
0N/A
0N/A public interface Session extends Remote {
0N/A void ping() throws RemoteException;
0N/A }
0N/A
0N/A private static class FactoryImpl implements Factory {
0N/A FactoryImpl() { }
0N/A public Session getSession() throws RemoteException {
0N/A Session impl = new SessionImpl();
0N/A UnicastRemoteObject.exportObject(impl, 0, new CSF(), new SSF());
0N/A // return impl instead of stub to work around 4114579
0N/A return impl;
0N/A }
0N/A }
0N/A
0N/A private static class SessionImpl implements Session {
0N/A SessionImpl() { }
0N/A public void ping() { }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A System.err.println("\nRegression test for bug 4486732\n");
0N/A
0N/A Factory factoryImpl = new FactoryImpl();
0N/A Factory factoryStub =
0N/A (Factory) UnicastRemoteObject.exportObject(factoryImpl, 0);
0N/A for (int i = 0; i < SESSIONS; i++) {
0N/A Session session = factoryStub.getSession();
0N/A session.ping();
0N/A }
0N/A UnicastRemoteObject.unexportObject(factoryImpl, true);
0N/A
0N/A Registry registryImpl = LocateRegistry.createRegistry(PORT);
0N/A CSF csf = new CSF();
0N/A Reference<CSF> registryRef = new WeakReference<CSF>(csf);
0N/A Registry registryStub = LocateRegistry.getRegistry("", PORT, csf);
0N/A csf = null;
0N/A registryStub.list();
0N/A registryStub = null;
0N/A UnicastRemoteObject.unexportObject(registryImpl, true);
0N/A
0N/A System.gc();
0N/A // allow connections to time out
0N/A Thread.sleep(3 * Long.getLong("sun.rmi.transport.connectionTimeout",
0N/A 15000));
0N/A System.gc();
0N/A
0N/A if (CSF.deserializedInstances.size() != SESSIONS) {
0N/A throw new Error("unexpected number of deserialized instances: " +
0N/A CSF.deserializedInstances.size());
0N/A }
0N/A
0N/A int nonNullCount = 0;
0N/A for (Reference<CSF> ref : CSF.deserializedInstances) {
0N/A csf = ref.get();
0N/A if (csf != null) {
0N/A System.err.println("non-null deserialized instance: " + csf);
0N/A nonNullCount++;
0N/A }
0N/A }
0N/A if (nonNullCount > 0) {
0N/A throw new Error("TEST FAILED: " +
0N/A nonNullCount + " non-null deserialized instances");
0N/A }
0N/A
0N/A csf = registryRef.get();
0N/A if (csf != null) {
0N/A System.err.println("non-null registry instance: " + csf);
0N/A throw new Error("TEST FAILED: non-null registry instance");
0N/A }
0N/A
0N/A System.err.println("TEST PASSED");
0N/A }
0N/A
0N/A private static class CSF implements RMIClientSocketFactory, Serializable {
0N/A static final List<Reference<CSF>> deserializedInstances =
0N/A Collections.synchronizedList(new ArrayList<Reference<CSF>>());
0N/A private static final AtomicInteger count = new AtomicInteger(0);
0N/A private int num = count.incrementAndGet();
0N/A CSF() { }
0N/A public Socket createSocket(String host, int port) throws IOException {
0N/A return new Socket(host, port);
0N/A }
0N/A public int hashCode() {
0N/A return num;
0N/A }
0N/A public boolean equals(Object obj) {
0N/A return obj instanceof CSF && ((CSF) obj).num == num;
0N/A }
0N/A private void readObject(ObjectInputStream in)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A in.defaultReadObject();
0N/A deserializedInstances.add(new WeakReference<CSF>(this));
0N/A }
0N/A }
0N/A
0N/A private static class SSF implements RMIServerSocketFactory {
0N/A SSF() { }
0N/A public ServerSocket createServerSocket(int port) throws IOException {
0N/A return new ServerSocket(port);
0N/A }
0N/A }
0N/A}
0N/A