test/java/rmi/testlibrary/ActivationLibrary.java

Print this page




  23 
  24 /**
  25  *
  26  */
  27 
  28 import java.io.File;
  29 import java.rmi.Naming;
  30 import java.rmi.NoSuchObjectException;
  31 import java.rmi.NotBoundException;
  32 import java.rmi.Remote;
  33 import java.rmi.activation.Activatable;
  34 import java.rmi.activation.ActivationID;
  35 import java.rmi.activation.ActivationSystem;
  36 import java.rmi.registry.LocateRegistry;
  37 
  38 /**
  39  * Class of test utility/library methods related to Activatable
  40  * objects.
  41  */
  42 public class ActivationLibrary {
  43     /** time safeDestroy should wait before failing on shutdown rmid */
  44     private static final int SAFE_WAIT_TIME;
  45     static {
  46         int slopFactor = 1;
  47         try {
  48             slopFactor = Integer.valueOf(
  49                 TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
  50         } catch (NumberFormatException ignore) {}
  51         SAFE_WAIT_TIME = 60000 * slopFactor;
  52     }
  53 
  54     private static final String SYSTEM_NAME =
  55         ActivationSystem.class.getName();
  56 
  57     private static void mesg(Object mesg) {
  58         System.err.println("ACTIVATION_LIBRARY: " + mesg.toString());
  59     }
  60 
  61     /**
  62      * Deactivate an activated Activatable
  63      */
  64     public static void deactivate(Remote remote,
  65                                   ActivationID id) {
  66         // We do as much as 50 deactivation trials, each separated by
  67         // at least 100 milliseconds sleep time (max sleep time of 5 secs).
  68         final long deactivateSleepTime = 100;
  69         long stopTime = System.currentTimeMillis() + deactivateSleepTime * 50;
  70         while (System.currentTimeMillis() < stopTime) {


  71             try {
  72                 if (Activatable.inactive(id) == true) {
  73                     mesg("inactive successful");
  74                     return;
  75                 } else {
  76                     mesg("inactive trial failed. Sleeping " +
  77                          deactivateSleepTime +
  78                          " milliseconds before next trial");
  79                     Thread.sleep(deactivateSleepTime);
  80                 }
  81             } catch (InterruptedException e) {
  82                 Thread.currentThread().interrupt();
  83                 mesg("Thread interrupted while trying to deactivate activatable. Exiting deactivation");
  84                 return;
  85             } catch (Exception e) {
  86                 try {
  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 }


  23 
  24 /**
  25  *
  26  */
  27 
  28 import java.io.File;
  29 import java.rmi.Naming;
  30 import java.rmi.NoSuchObjectException;
  31 import java.rmi.NotBoundException;
  32 import java.rmi.Remote;
  33 import java.rmi.activation.Activatable;
  34 import java.rmi.activation.ActivationID;
  35 import java.rmi.activation.ActivationSystem;
  36 import java.rmi.registry.LocateRegistry;
  37 
  38 /**
  39  * Class of test utility/library methods related to Activatable
  40  * objects.
  41  */
  42 public class ActivationLibrary {














  43     private static void mesg(Object mesg) {
  44         System.err.println("ACTIVATION_LIBRARY: " + mesg.toString());
  45     }
  46 
  47     /**
  48      * Deactivate an activated Activatable
  49      */
  50     public static void deactivate(Remote remote,
  51                                   ActivationID id) {
  52         final long POLLTIME_MS = 100L;
  53         final long DEACTIVATE_TIME_MS = 30_000L;
  54 
  55         long startTime = System.currentTimeMillis();
  56         long deadline = TestLibrary.computeDeadline(startTime, DEACTIVATE_TIME_MS);
  57 
  58         while (System.currentTimeMillis() < deadline) {
  59             try {
  60                 if (Activatable.inactive(id) == true) {
  61                     mesg("inactive successful");
  62                     return;
  63                 } else {
  64                     Thread.sleep(POLLTIME_MS);



  65                 }
  66             } catch (InterruptedException e) {
  67                 Thread.currentThread().interrupt();
  68                 mesg("Thread interrupted while trying to deactivate activatable. Exiting deactivation");
  69                 return;
  70             } catch (Exception e) {
  71                 try {
  72                     // forcibly unexport the object
  73                     mesg("Unexpected exception. Have to forcibly unexport the object." +
  74                          " Exception was :");
  75                     e.printStackTrace();
  76                     Activatable.unexportObject(remote, true);
  77                 } catch (NoSuchObjectException ex) {
  78                 }
  79                 return;
  80             }
  81         }
  82 
  83         mesg("unable to inactivate after " +
  84             (System.currentTimeMillis() - startTime) + "ms.");
  85         mesg("unexporting object forcibly instead");
  86 
  87         try {
  88             Activatable.unexportObject(remote, true);
  89         } catch (NoSuchObjectException e) {
  90         }
  91     }
  92 }