test/java/rmi/testlibrary/RMID.java

Print this page
rev 9296 : 8034999: change rmidRunning to a simple lookup
Reviewed-by: XXX

@@ -26,20 +26,24 @@
  */
 
 import java.io.*;
 import java.rmi.*;
 import java.rmi.activation.*;
+import java.rmi.registry.*;
 
 /**
  * Utility class that creates an instance of rmid with a policy
  * file of name <code>TestParams.defaultPolicy</code>.
  *
  * Activation groups should run with the same security manager as the
  * test.
  */
 public class RMID extends JavaVM {
 
+    private static final String SYSTEM_NAME = ActivationSystem.class.getName();
+        // "java.rmi.activation.ActivationSystem"
+
     public static String MANAGER_OPTION="-Djava.security.manager=";
 
     /** Test port for rmid */
     private final int port;
 

@@ -205,10 +209,28 @@
 
     public void slowStart() throws IOException {
         start(60000);
     }
 
+    /**
+     * Looks up the activation system in the registry on the given port,
+     * returning its stub, or null if it's not present. This method differs from
+     * ActivationGroup.getSystem() because this method looks on a specific port
+     * instead of using the java.rmi.activation.port property like
+     * ActivationGroup.getSystem() does. This method also returns null instead
+     * of throwing exceptions.
+     */
+    public static ActivationSystem lookupSystem(int port) {
+        mesg("getting activation system");
+        try {
+            return (ActivationSystem)LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
+        } catch (RemoteException | NotBoundException ex) {
+            mesg("couldn't get activation system: " + ex);
+            return null;
+        }
+    }
+
     public void start(long waitTime) throws IOException {
 
         // if rmid is already running, then the test will fail with
         // a well recognized exception (port already in use...).
 

@@ -236,12 +258,12 @@
                 return;
             }
             waitTime -= rmidStartSleepTime;
 
             // Checking if rmid is present
-            if (ActivationLibrary.rmidRunning(port)) {
-                /**
+            if (lookupSystem(port) != null) {
+                /*
                  * We need to set the java.rmi.activation.port value as the
                  * activation system will use the property to determine the
                  * port #.  The activation system will use this value if set.
                  * If it isn't set, the activation system will set it to an
                  * incorrect value.

@@ -272,28 +294,17 @@
      * process object (destroy does though).
      */
     public static void shutdown(int port) {
 
         try {
-            ActivationSystem system = null;
-
-            try {
-                mesg("getting a reference to the activation system");
-                system = (ActivationSystem) Naming.lookup("//:" +
-                    port +
-                    "/java.rmi.activation.ActivationSystem");
-                mesg("obtained a reference to the activation system");
-            } catch (RemoteException re) {
-                mesg("could not contact registry while trying to shutdown activation system");
-            } catch (java.net.MalformedURLException mue) {
-            }
+            ActivationSystem system = lookupSystem(port);
 
             if (system == null) {
                 TestLibrary.bomb("reference to the activation system was null");
             }
-            system.shutdown();
 
+            system.shutdown();
         } catch (RemoteException re) {
             mesg("shutting down the activation daemon failed");
         } catch (Exception e) {
             mesg("caught exception trying to shutdown rmid");
             mesg(e.getMessage());