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