test/java/rmi/testlibrary/ActivationLibrary.java

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


  87                     // forcibly unexport the object
  88                     mesg("Unexpected exception. Have to forcibly unexport the object." +
  89                          " Exception was :");
  90                     e.printStackTrace();
  91                     Activatable.unexportObject(remote, true);
  92                 } catch (NoSuchObjectException ex) {
  93                 }
  94                 return;
  95             }
  96         }
  97 
  98         mesg("unable to inactivate after several attempts");
  99         mesg("unexporting object forcibly instead");
 100 
 101         try {
 102             Activatable.unexportObject(remote, true);
 103         } catch (NoSuchObjectException e) {
 104         }
 105     }
 106 
 107     /**
 108      * Simple method call to see if rmid is running.
 109      *
 110      * This method intentionally avoids performing a lookup on the
 111      * activation system.
 112      */
 113     public static boolean rmidRunning(int port) {
 114         int allowedNotReady = 50;
 115         int connectionRefusedExceptions = 0;
 116 
 117         /* We wait as much as a total of 7.5 secs trying to see Rmid running.
 118          * We do this by pausing steps of 100 milliseconds (so up to 75 steps),
 119          * right after trying to lookup and find RMID running in the other vm.
 120          */
 121         final long rmidWaitingStepTime = 100;
 122         for (int i = 0; i <= 74; i++) {
 123 
 124             try {
 125                 LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
 126                 mesg("Activation System available after " +
 127                      (i * rmidWaitingStepTime) + " milliseconds");
 128                 return true;
 129 
 130             } catch (java.rmi.ConnectException e) {
 131                 mesg("Remote connection refused after " +
 132                      (i * rmidWaitingStepTime) + " milliseconds");
 133 
 134                 // ignore connect exceptions until we decide rmid is not up
 135                 if ((connectionRefusedExceptions ++) >= allowedNotReady) {
 136                     return false;
 137                 }
 138 
 139             } catch (java.rmi.NoSuchObjectException nsoe) {
 140                 /* Activation System still unavailable.
 141                  * Ignore this since we are just waiting for its availibility.
 142                  * Just signal unavailibility.
 143                  */
 144                 mesg("Activation System still unavailable after more than " +
 145                      (i * rmidWaitingStepTime) + " milliseconds");
 146 
 147             } catch (NotBoundException e) {
 148                 return false;
 149 
 150             } catch (Exception e) {
 151                 /* print out other types of exceptions as an FYI.
 152                  * test should not fail as rmid is likely to be in an
 153                  * undetermined state at this point.
 154                  */
 155                 mesg("caught an exception trying to" +
 156                      " start rmid, last exception was: " +
 157                      e.getMessage());
 158                 e.printStackTrace();
 159             }
 160 
 161             // Waiting for another 100 milliseconds.
 162             try {
 163                 Thread.sleep(100);
 164             } catch (InterruptedException e) {
 165                 Thread.currentThread().interrupt();
 166                 mesg("Thread interrupted while checking if Activation System is running. Exiting check");
 167                 return false;
 168             }
 169         }
 170         return false;
 171     }
 172 
 173     /** cleanup after rmid */
 174     public static void rmidCleanup(RMID rmid) {
 175         if (rmid != null) {
 176             if (!ActivationLibrary.safeDestroy(rmid, SAFE_WAIT_TIME)) {
 177                 TestLibrary.bomb("rmid not destroyed in: " +
 178                                  SAFE_WAIT_TIME +
 179                                  " milliseconds");
 180             }
 181         }
 182         RMID.removeLog();
 183     }
 184 
 185     /**
 186      * Invoke shutdown on rmid in a way that will not cause a test
 187      * to hang.
 188      *
 189      * @return whether or not shutdown completed succesfully in the
 190      *         timeAllowed
 191      */
 192     private static boolean safeDestroy(RMID rmid, long timeAllowed) {


 200         }
 201 
 202         return destroyThread.shutdownSucceeded();
 203     }
 204 
 205     /**
 206      * Thread class to handle the destruction of rmid
 207      */
 208     private static class DestroyThread extends Thread {
 209         private final RMID rmid;
 210         private final int port;
 211         private boolean succeeded = false;
 212 
 213         DestroyThread(RMID rmid) {
 214             this.rmid = rmid;
 215             this.port = rmid.getPort();
 216             this.setDaemon(true);
 217         }
 218 
 219         public void run() {
 220             if (ActivationLibrary.rmidRunning(port)) {
 221                 rmid.destroy();
 222                 synchronized (this) {
 223                     // flag that the test was able to shutdown rmid
 224                     succeeded = true;
 225                 }
 226                 mesg("finished destroying rmid");
 227             } else {
 228                 mesg("tried to shutdown when rmid was not running");
 229             }
 230         }
 231 
 232         public synchronized boolean shutdownSucceeded() {
 233             return succeeded;
 234         }
 235     }
 236 }


  87                     // forcibly unexport the object
  88                     mesg("Unexpected exception. Have to forcibly unexport the object." +
  89                          " Exception was :");
  90                     e.printStackTrace();
  91                     Activatable.unexportObject(remote, true);
  92                 } catch (NoSuchObjectException ex) {
  93                 }
  94                 return;
  95             }
  96         }
  97 
  98         mesg("unable to inactivate after several attempts");
  99         mesg("unexporting object forcibly instead");
 100 
 101         try {
 102             Activatable.unexportObject(remote, true);
 103         } catch (NoSuchObjectException e) {
 104         }
 105     }
 106 


































































 107     /** cleanup after rmid */
 108     public static void rmidCleanup(RMID rmid) {
 109         if (rmid != null) {
 110             if (!ActivationLibrary.safeDestroy(rmid, SAFE_WAIT_TIME)) {
 111                 TestLibrary.bomb("rmid not destroyed in: " +
 112                                  SAFE_WAIT_TIME +
 113                                  " milliseconds");
 114             }
 115         }
 116         RMID.removeLog();
 117     }
 118 
 119     /**
 120      * Invoke shutdown on rmid in a way that will not cause a test
 121      * to hang.
 122      *
 123      * @return whether or not shutdown completed succesfully in the
 124      *         timeAllowed
 125      */
 126     private static boolean safeDestroy(RMID rmid, long timeAllowed) {


 134         }
 135 
 136         return destroyThread.shutdownSucceeded();
 137     }
 138 
 139     /**
 140      * Thread class to handle the destruction of rmid
 141      */
 142     private static class DestroyThread extends Thread {
 143         private final RMID rmid;
 144         private final int port;
 145         private boolean succeeded = false;
 146 
 147         DestroyThread(RMID rmid) {
 148             this.rmid = rmid;
 149             this.port = rmid.getPort();
 150             this.setDaemon(true);
 151         }
 152 
 153         public void run() {
 154             if (RMID.lookupSystem(port) != null) {
 155                 rmid.destroy();
 156                 synchronized (this) {
 157                     // flag that the test was able to shutdown rmid
 158                     succeeded = true;
 159                 }
 160                 mesg("finished destroying rmid");
 161             } else {
 162                 mesg("tried to shutdown when rmid was not running");
 163             }
 164         }
 165 
 166         public synchronized boolean shutdownSucceeded() {
 167             return succeeded;
 168         }
 169     }
 170 }