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