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

Print this page

        

@@ -29,12 +29,10 @@
  * particular, after the stub has become unreachable and all
  * connections to its endpoint have been closed, then the factory
  * should become unreachable too (through the RMI implementation).
  * @author Peter Jones
  *
- * @library ../../testlibrary
- * @build TestLibrary
  * @run main/othervm -Dsun.rmi.transport.connectionTimeout=2000
  *     PinClientSocketFactory
  */
 
 import java.io.IOException;

@@ -42,25 +40,26 @@
 import java.io.Serializable;
 import java.lang.ref.Reference;
 import java.lang.ref.WeakReference;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.rmi.RMISecurityManager;
 import java.rmi.Remote;
 import java.rmi.RemoteException;
 import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
 import java.rmi.server.RMIClientSocketFactory;
 import java.rmi.server.RMIServerSocketFactory;
+import java.rmi.server.RMISocketFactory;
 import java.rmi.server.UnicastRemoteObject;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
 
 public class PinClientSocketFactory {
 
-    private static final int PORT = TestLibrary.getUnusedRandomPort();
     private static final int SESSIONS = 50;
 
     public interface Factory extends Remote {
         Session getSession() throws RemoteException;
     }

@@ -85,23 +84,28 @@
     }
 
     public static void main(String[] args) throws Exception {
         System.err.println("\nRegression test for bug 4486732\n");
 
+        // Delegate to capture the actual port being used by the server socket.
+        DelegateRMISocketFactory delegateFactory = new DelegateRMISocketFactory();
+        RMISocketFactory.setSocketFactory(delegateFactory);
+
         Factory factoryImpl = new FactoryImpl();
         Factory factoryStub =
             (Factory) UnicastRemoteObject.exportObject(factoryImpl, 0);
         for (int i = 0; i < SESSIONS; i++) {
             Session session = factoryStub.getSession();
             session.ping();
         }
         UnicastRemoteObject.unexportObject(factoryImpl, true);
+        Registry registryImpl = LocateRegistry.createRegistry(DelegateRMISocketFactory.EPHEMERAL_PORT);
+        int port = delegateFactory.getServerPort();
 
-        Registry registryImpl = LocateRegistry.createRegistry(PORT);
         CSF csf = new CSF();
         Reference<CSF> registryRef = new WeakReference<CSF>(csf);
-        Registry registryStub = LocateRegistry.getRegistry("", PORT, csf);
+        Registry registryStub = LocateRegistry.getRegistry("", port, csf);
         csf = null;
         registryStub.list();
         registryStub = null;
         UnicastRemoteObject.unexportObject(registryImpl, true);
 

@@ -165,6 +169,34 @@
         SSF() { }
         public ServerSocket createServerSocket(int port) throws IOException {
             return new ServerSocket(port);
         }
     }
+
+    /** Delegate socket factory to support listening on an ephemeral port. */
+    private static class DelegateRMISocketFactory extends RMISocketFactory {
+        // Map requests for port 9999 to the empemeral port.
+        public static final int EPHEMERAL_PORT = 9999;
+
+        private static RMISocketFactory factory = RMISocketFactory.getDefaultSocketFactory();
+        private int port;
+
+        @Override
+        public ServerSocket createServerSocket(int p) throws IOException {
+            if (p == EPHEMERAL_PORT)
+                p = 0;
+            ServerSocket ss =  new ServerSocket(p);
+            port = ss.getLocalPort();
+            return ss;
+        }
+
+        @Override
+        public Socket createSocket(String host, int p) throws IOException {
+            return factory.createSocket(host, p);
+        }
+
+        /** Returns the port of the most recently created server socket */
+        public int getServerPort() {
+            return port;
+        }
+    }
 }