1 /* 2 * Copyright (c) 2005, 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 4486732 26 * @summary When a remote stub contains a client socket factory and a 27 * remote invocation is made using that stub, the factory should not 28 * be held strongly reachable by the RMI implementation forever; in 29 * particular, after the stub has become unreachable and all 30 * connections to its endpoint have been closed, then the factory 31 * should become unreachable too (through the RMI implementation). 32 * @author Peter Jones 33 * 34 * @library ../../testlibrary 35 * @modules java.rmi/sun.rmi.registry 36 * java.rmi/sun.rmi.server 37 * java.rmi/sun.rmi.transport 38 * java.rmi/sun.rmi.transport.tcp 39 * @build TestLibrary 40 * @run main/othervm -Dsun.rmi.transport.connectionTimeout=2000 41 * PinClientSocketFactory 42 */ 43 44 import java.io.IOException; 45 import java.io.ObjectInputStream; 46 import java.io.Serializable; 47 import java.lang.ref.Reference; 48 import java.lang.ref.WeakReference; 49 import java.net.ServerSocket; 50 import java.net.Socket; 51 import java.rmi.Remote; 52 import java.rmi.RemoteException; 53 import java.rmi.registry.LocateRegistry; 54 import java.rmi.registry.Registry; 55 import java.rmi.server.RMIClientSocketFactory; 56 import java.rmi.server.RMIServerSocketFactory; 57 import java.rmi.server.UnicastRemoteObject; 58 import java.util.ArrayList; 59 import java.util.Collections; 60 import java.util.List; 61 import java.util.concurrent.atomic.AtomicInteger; 62 63 public class PinClientSocketFactory { 64 65 private static final int SESSIONS = 50; 66 67 public interface Factory extends Remote { getSession()68 Session getSession() throws RemoteException; 69 } 70 71 public interface Session extends Remote { ping()72 void ping() throws RemoteException; 73 } 74 75 private static class FactoryImpl implements Factory { FactoryImpl()76 FactoryImpl() { } getSession()77 public Session getSession() throws RemoteException { 78 Session impl = new SessionImpl(); 79 UnicastRemoteObject.exportObject(impl, 0, new CSF(), new SSF()); 80 // return impl instead of stub to work around 4114579 81 return impl; 82 } 83 } 84 85 private static class SessionImpl implements Session { SessionImpl()86 SessionImpl() { } ping()87 public void ping() { } 88 } 89 main(String[] args)90 public static void main(String[] args) throws Exception { 91 System.err.println("\nRegression test for bug 4486732\n"); 92 93 Factory factoryImpl = new FactoryImpl(); 94 Factory factoryStub = 95 (Factory) UnicastRemoteObject.exportObject(factoryImpl, 0); 96 for (int i = 0; i < SESSIONS; i++) { 97 Session session = factoryStub.getSession(); 98 session.ping(); 99 } 100 UnicastRemoteObject.unexportObject(factoryImpl, true); 101 102 Registry registryImpl = TestLibrary.createRegistryOnEphemeralPort(); 103 int port = TestLibrary.getRegistryPort(registryImpl); 104 System.out.println("Registry listening on port " + port); 105 106 CSF csf = new CSF(); 107 Reference<CSF> registryRef = new WeakReference<CSF>(csf); 108 Registry registryStub = LocateRegistry.getRegistry("", port, csf); 109 csf = null; 110 registryStub.list(); 111 registryStub = null; 112 UnicastRemoteObject.unexportObject(registryImpl, true); 113 114 System.gc(); 115 // allow connections to time out 116 Thread.sleep(3 * Long.getLong("sun.rmi.transport.connectionTimeout", 117 15000)); 118 System.gc(); 119 120 if (CSF.deserializedInstances.size() != SESSIONS) { 121 throw new Error("unexpected number of deserialized instances: " + 122 CSF.deserializedInstances.size()); 123 } 124 125 int nonNullCount = 0; 126 for (Reference<CSF> ref : CSF.deserializedInstances) { 127 csf = ref.get(); 128 if (csf != null) { 129 System.err.println("non-null deserialized instance: " + csf); 130 nonNullCount++; 131 } 132 } 133 if (nonNullCount > 0) { 134 throw new Error("TEST FAILED: " + 135 nonNullCount + " non-null deserialized instances"); 136 } 137 138 csf = registryRef.get(); 139 if (csf != null) { 140 System.err.println("non-null registry instance: " + csf); 141 throw new Error("TEST FAILED: non-null registry instance"); 142 } 143 144 System.err.println("TEST PASSED"); 145 } 146 147 private static class CSF implements RMIClientSocketFactory, Serializable { 148 static final List<Reference<CSF>> deserializedInstances = 149 Collections.synchronizedList(new ArrayList<Reference<CSF>>()); 150 private static final AtomicInteger count = new AtomicInteger(0); 151 private int num = count.incrementAndGet(); CSF()152 CSF() { } createSocket(String host, int port)153 public Socket createSocket(String host, int port) throws IOException { 154 return new Socket(host, port); 155 } hashCode()156 public int hashCode() { 157 return num; 158 } equals(Object obj)159 public boolean equals(Object obj) { 160 return obj instanceof CSF && ((CSF) obj).num == num; 161 } readObject(ObjectInputStream in)162 private void readObject(ObjectInputStream in) 163 throws IOException, ClassNotFoundException 164 { 165 in.defaultReadObject(); 166 deserializedInstances.add(new WeakReference<CSF>(this)); 167 } 168 } 169 170 private static class SSF implements RMIServerSocketFactory { SSF()171 SSF() { } createServerSocket(int port)172 public ServerSocket createServerSocket(int port) throws IOException { 173 return new ServerSocket(port); 174 } 175 } 176 } 177