8034999: change rmidRunning to a simple lookup
authorsmarks
Wed, 26 Feb 2014 18:22:56 -0800
changeset 23025 ee5c4f07f86c
parent 23024 7d5ecb31115f
child 23026 bdc41bab378b
8034999: change rmidRunning to a simple lookup Reviewed-by: darcy
jdk/test/java/rmi/testlibrary/ActivationLibrary.java
jdk/test/java/rmi/testlibrary/RMID.java
--- a/jdk/test/java/rmi/testlibrary/ActivationLibrary.java	Wed Feb 26 13:02:05 2014 -0800
+++ b/jdk/test/java/rmi/testlibrary/ActivationLibrary.java	Wed Feb 26 18:22:56 2014 -0800
@@ -104,72 +104,6 @@
         }
     }
 
-    /**
-     * Simple method call to see if rmid is running.
-     *
-     * This method intentionally avoids performing a lookup on the
-     * activation system.
-     */
-    public static boolean rmidRunning(int port) {
-        int allowedNotReady = 50;
-        int connectionRefusedExceptions = 0;
-
-        /* We wait as much as a total of 7.5 secs trying to see Rmid running.
-         * We do this by pausing steps of 100 milliseconds (so up to 75 steps),
-         * right after trying to lookup and find RMID running in the other vm.
-         */
-        final long rmidWaitingStepTime = 100;
-        for (int i = 0; i <= 74; i++) {
-
-            try {
-                LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
-                mesg("Activation System available after " +
-                     (i * rmidWaitingStepTime) + " milliseconds");
-                return true;
-
-            } catch (java.rmi.ConnectException e) {
-                mesg("Remote connection refused after " +
-                     (i * rmidWaitingStepTime) + " milliseconds");
-
-                // ignore connect exceptions until we decide rmid is not up
-                if ((connectionRefusedExceptions ++) >= allowedNotReady) {
-                    return false;
-                }
-
-            } catch (java.rmi.NoSuchObjectException nsoe) {
-                /* Activation System still unavailable.
-                 * Ignore this since we are just waiting for its availibility.
-                 * Just signal unavailibility.
-                 */
-                mesg("Activation System still unavailable after more than " +
-                     (i * rmidWaitingStepTime) + " milliseconds");
-
-            } catch (NotBoundException e) {
-                return false;
-
-            } catch (Exception e) {
-                /* print out other types of exceptions as an FYI.
-                 * test should not fail as rmid is likely to be in an
-                 * undetermined state at this point.
-                 */
-                mesg("caught an exception trying to" +
-                     " start rmid, last exception was: " +
-                     e.getMessage());
-                e.printStackTrace();
-            }
-
-            // Waiting for another 100 milliseconds.
-            try {
-                Thread.sleep(100);
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                mesg("Thread interrupted while checking if Activation System is running. Exiting check");
-                return false;
-            }
-        }
-        return false;
-    }
-
     /** cleanup after rmid */
     public static void rmidCleanup(RMID rmid) {
         if (rmid != null) {
@@ -217,7 +151,7 @@
         }
 
         public void run() {
-            if (ActivationLibrary.rmidRunning(port)) {
+            if (RMID.lookupSystem(port) != null) {
                 rmid.destroy();
                 synchronized (this) {
                     // flag that the test was able to shutdown rmid
--- a/jdk/test/java/rmi/testlibrary/RMID.java	Wed Feb 26 13:02:05 2014 -0800
+++ b/jdk/test/java/rmi/testlibrary/RMID.java	Wed Feb 26 18:22:56 2014 -0800
@@ -28,6 +28,7 @@
 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
@@ -38,6 +39,9 @@
  */
 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 */
@@ -207,6 +211,22 @@
         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) {
+        try {
+            return (ActivationSystem)LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
+        } catch (RemoteException | NotBoundException ex) {
+            return null;
+        }
+    }
+
     public void start(long waitTime) throws IOException {
 
         // if rmid is already running, then the test will fail with
@@ -238,8 +258,8 @@
             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.
@@ -249,11 +269,11 @@
                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
                 mesg("finished starting rmid.");
                 return;
+            } else {
+                if (waitTime > 0) {
+                    mesg("rmid not started, will retry for " + waitTime + "ms");
+                }
             }
-            else {
-                mesg("rmid still not started");
-            }
-
         } while (waitTime > 0);
         TestLibrary.bomb("start rmid failed... giving up", null);
     }
@@ -274,24 +294,13 @@
     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();
-
         } catch (RemoteException re) {
             mesg("shutting down the activation daemon failed");
         } catch (Exception e) {