test/java/rmi/activation/Activatable/forceLogSnapshot/ForceLogSnapshot.java

Print this page

        

@@ -30,16 +30,17 @@
  * @build TestLibrary RMID ActivationLibrary
  *     ActivateMe ForceLogSnapshot_Stub
  * @run main/othervm/policy=security.policy/timeout=640 ForceLogSnapshot
  */
 
-import java.io.*;
+import java.io.IOException;
 import java.rmi.*;
 import java.rmi.activation.*;
 import java.rmi.server.*;
-import java.rmi.registry.*;
 import java.util.*;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
 public class ForceLogSnapshot
         implements ActivateMe
 {
     /** how many activatable remote objects to create to test rmid */

@@ -47,64 +48,59 @@
     final public static int NUM_GROUPS = 4;
     /** cause RMID to generate a snapshot every 10th activated object */
     final public static int SNAPSHOT_INTERVAL = 10;
 
     private ActivationID id;
-    private Vector responders = new Vector();
 
     private static final String RESTARTABLE = "restartable";
     private static final String ACTIVATABLE = "activatable";
 
-    private static Object lock = new Object();
-    private static boolean[] restartedObjects = new boolean[HOW_MANY];
-    private static boolean[] activatedObjects = new boolean[HOW_MANY];
+    private static final CountDownLatch restartedLatch = new CountDownLatch(HOW_MANY);
+    private static final CountDownLatch activatedLatch = new CountDownLatch(HOW_MANY);
 
     public ForceLogSnapshot(ActivationID id, MarshalledObject mobj)
         throws ActivationException, RemoteException
     {
         this.id = id;
-        int intId = 0;
 
         Activatable.exportObject(this, id, 0);
         ActivateMe obj;
         String responder;
         try {
             Object[] stuff = (Object[]) mobj.get();
 
-            intId = ((Integer) stuff[0]).intValue();
+            int intId = ((Integer) stuff[0]).intValue();
             responder = (String) stuff[1];
             obj = (ActivateMe) stuff[2];
 
             System.err.println(responder + " service started");
-        } catch (Exception e) {
+            obj.ping(intId, responder);
+        } catch (IOException | ClassNotFoundException e) {
             System.err.println("unable to obtain stub from marshalled object");
             System.err.println(e.getMessage());
-            e.printStackTrace();
-            return;
         }
-
-        obj.ping(intId, responder);
     }
 
     public ForceLogSnapshot() throws RemoteException {
         UnicastRemoteObject.exportObject(this, 0);
     }
 
+    @Override
     public void ping(int intId, String responder) {
         System.err.println("ForceLogSnapshot: received ping from " +
                            responder);
-        if (responder.equals(RESTARTABLE)) {
-            synchronized (lock) {
-                restartedObjects[intId] = true;
-            }
-        } else if (responder.equals(ACTIVATABLE)) {
-            synchronized (lock) {
-                activatedObjects[intId] = true;
-            }
+        switch (responder) {
+            case RESTARTABLE:
+                restartedLatch.countDown();
+                break;
+            case ACTIVATABLE:
+                activatedLatch.countDown();
+                break;
         }
     }
 
+    @Override
     public void crash() {
         System.exit(0);
     }
 
     public ActivationID getID() {

@@ -139,16 +135,16 @@
                   TestParams.defaultGroupPolicy);
             p.put("java.security.manager",
                   TestParams.defaultSecurityManager);
 
             Object[][] stuff = new Object[HOW_MANY][];
-            MarshalledObject restartMobj = null;
-            ActivationGroupDesc groupDesc = null;
-            MarshalledObject activateMobj = null;
+            MarshalledObject restartMobj;
+            ActivationGroupDesc groupDesc;
+            MarshalledObject activateMobj;
             ActivationGroupID[] groupIDs = new ActivationGroupID[NUM_GROUPS];
-            ActivationDesc restartableDesc = null;
-            ActivationDesc activatableDesc = null;
+            ActivationDesc restartableDesc;
+            ActivationDesc activatableDesc;
             ActivateMe[] restartableObj = new ActivateMe[HOW_MANY];
             ActivateMe[] activatableObj = new ActivateMe[HOW_MANY];
 
             /*
              * Create unicast object to be contacted when service is activated.

@@ -212,22 +208,22 @@
                 /*
                  * Restart rmid; it should start up the restartable service
                  */
                 rmid.restart();
 
-                if (howManyRestarted(restartedObjects, 10) < HOW_MANY) {
+                if (waitAllStarted(restartedLatch, 10) != HOW_MANY) {
                         TestLibrary.bomb("Test1 failed: a service would not " +
                                          "restart");
                 }
                 System.err.println("Test1 passed: rmid " +
                                    "all service(s) restarted. Performing next test.");
 
                 /*
                  * Make sure no activatable services were automatically
                  * restarted.
                  */
-                if (howManyRestarted(activatedObjects, 2) != 0) {
+                if (waitAllStarted(activatedLatch, 2) != 0) {
                     TestLibrary.bomb("Test2 failed: activatable service restarted!",
                                      null);
                 }
                 System.err.println("Test2 passed: rmid did not " +
                                    "restart activatable service(s)");

@@ -243,43 +239,36 @@
                 }
 
             } while (repeatOnce-- > 0);
 
 
-        } catch (Exception e) {
+        } catch (IOException | ActivationException e) {
             TestLibrary.bomb("test failed", e);
         } finally {
             ActivationLibrary.rmidCleanup(rmid);
             for (int i = 0 ; i < HOW_MANY ; i ++) {
                 TestLibrary.unexport(unicastObjs[i]);
             }
         }
     }
 
     /**
-     * Check to see how many services have been automatically
-     * restarted.
+     * Check to see if all services have been automatically restarted.
+     * 
+     * @waitObjects a CountDownLatch that used to sync all services.
+     * @retries     retries times
+     * @return      with retry retries times, how many services has been started
      */
-    private static int howManyRestarted(boolean[] startedObjects, int retries) {
-        int succeeded = 0;
-        int restarted = 0;
+    private static int waitAllStarted(CountDownLatch waitObjects, int retries) {
         int atry = 0;
 
-        while ((restarted < HOW_MANY) && (atry < retries)) {
-            restarted = 0;
-            for (int j = 0 ; j < HOW_MANY ; j ++ ) {
-                synchronized(lock) {
-                    if (startedObjects[j]) {
-                        restarted ++;
-                    }
-                }
-            }
-            System.err.println("not all objects restarted, retrying...");
+        while (atry++ < retries) {
             try {
-                Thread.sleep(10000);
-            } catch (InterruptedException ie) {
+                if(waitObjects.await(10, TimeUnit.SECONDS))
+                    return HOW_MANY;
+            } catch (InterruptedException ex) {
+                throw new RuntimeException(ex);
             }
-            atry ++;
         }
-        return restarted;
+        return HOW_MANY - (int)waitObjects.getCount();
     }
 }