test/java/rmi/transport/pinClientSocketFactory/PinClientSocketFactory.java

Print this page




  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) {


 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 }


  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.RMISecurityManager;
  46 import java.rmi.Remote;
  47 import java.rmi.RemoteException;
  48 import java.rmi.registry.LocateRegistry;
  49 import java.rmi.registry.Registry;
  50 import java.rmi.server.RMIClientSocketFactory;
  51 import java.rmi.server.RMIServerSocketFactory;
  52 import java.rmi.server.RMISocketFactory;
  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 SESSIONS = 50;
  62 
  63     public interface Factory extends Remote {
  64         Session getSession() throws RemoteException;
  65     }
  66 
  67     public interface Session extends Remote {
  68         void ping() throws RemoteException;
  69     }
  70 
  71     private static class FactoryImpl implements Factory {
  72         FactoryImpl() { }
  73         public Session getSession() throws RemoteException {
  74             Session impl = new SessionImpl();
  75             UnicastRemoteObject.exportObject(impl, 0, new CSF(), new SSF());
  76             // return impl instead of stub to work around 4114579
  77             return impl;
  78         }
  79     }
  80 
  81     private static class SessionImpl implements Session {
  82         SessionImpl() { }
  83         public void ping() { }
  84     }
  85 
  86     public static void main(String[] args) throws Exception {
  87         System.err.println("\nRegression test for bug 4486732\n");
  88 
  89         // Delegate to capture the actual port being used by the server socket.
  90         DelegateRMISocketFactory delegateFactory = new DelegateRMISocketFactory();
  91         RMISocketFactory.setSocketFactory(delegateFactory);
  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         Registry registryImpl = LocateRegistry.createRegistry(DelegateRMISocketFactory.EPHEMERAL_PORT);
 102         int port = delegateFactory.getServerPort();
 103 

 104         CSF csf = new CSF();
 105         Reference<CSF> registryRef = new WeakReference<CSF>(csf);
 106         Registry registryStub = LocateRegistry.getRegistry("", port, csf);
 107         csf = null;
 108         registryStub.list();
 109         registryStub = null;
 110         UnicastRemoteObject.unexportObject(registryImpl, true);
 111 
 112         System.gc();
 113         // allow connections to time out
 114         Thread.sleep(3 * Long.getLong("sun.rmi.transport.connectionTimeout",
 115                                       15000));
 116         System.gc();
 117 
 118         if (CSF.deserializedInstances.size() != SESSIONS) {
 119             throw new Error("unexpected number of deserialized instances: " +
 120                             CSF.deserializedInstances.size());
 121         }
 122 
 123         int nonNullCount = 0;
 124         for (Reference<CSF> ref : CSF.deserializedInstances) {
 125             csf = ref.get();
 126             if (csf != null) {


 152             return new Socket(host, port);
 153         }
 154         public int hashCode() {
 155             return num;
 156         }
 157         public boolean equals(Object obj) {
 158             return obj instanceof CSF && ((CSF) obj).num == num;
 159         }
 160         private void readObject(ObjectInputStream in)
 161             throws IOException, ClassNotFoundException
 162         {
 163             in.defaultReadObject();
 164             deserializedInstances.add(new WeakReference<CSF>(this));
 165         }
 166     }
 167 
 168     private static class SSF implements RMIServerSocketFactory {
 169         SSF() { }
 170         public ServerSocket createServerSocket(int port) throws IOException {
 171             return new ServerSocket(port);
 172         }
 173     }
 174 
 175     /** Delegate socket factory to support listening on an ephemeral port. */
 176     private static class DelegateRMISocketFactory extends RMISocketFactory {
 177         // Map requests for port 9999 to the empemeral port.
 178         public static final int EPHEMERAL_PORT = 9999;
 179 
 180         private static RMISocketFactory factory = RMISocketFactory.getDefaultSocketFactory();
 181         private int port;
 182 
 183         @Override
 184         public ServerSocket createServerSocket(int p) throws IOException {
 185             if (p == EPHEMERAL_PORT)
 186                 p = 0;
 187             ServerSocket ss =  new ServerSocket(p);
 188             port = ss.getLocalPort();
 189             return ss;
 190         }
 191 
 192         @Override
 193         public Socket createSocket(String host, int p) throws IOException {
 194             return factory.createSocket(host, p);
 195         }
 196 
 197         /** Returns the port of the most recently created server socket */
 198         public int getServerPort() {
 199             return port;
 200         }
 201     }
 202 }