test/java/rmi/testlibrary/RMID.java

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


  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *
  26  */
  27 
  28 import java.io.*;
  29 import java.rmi.*;
  30 import java.rmi.activation.*;

  31 
  32 /**
  33  * Utility class that creates an instance of rmid with a policy
  34  * file of name <code>TestParams.defaultPolicy</code>.
  35  *
  36  * Activation groups should run with the same security manager as the
  37  * test.
  38  */
  39 public class RMID extends JavaVM {
  40 



  41     public static String MANAGER_OPTION="-Djava.security.manager=";
  42 
  43     /** Test port for rmid */
  44     private final int port;
  45 
  46     /** Initial log name */
  47     protected static String log = "log";
  48     /** rmid's logfile directory; currently must be "." */
  49     protected static String LOGDIR = ".";
  50 
  51     private static void mesg(Object mesg) {
  52         System.err.println("RMID: " + mesg.toString());
  53     }
  54 
  55     /** make test options and arguments */
  56     private static String makeOptions(boolean debugExec) {
  57 
  58         String options = " -Dsun.rmi.server.activation.debugExec=" +
  59             debugExec;
  60         // +


 190     }
 191 
 192     /**
 193      * This method is used for adding arguments to rmid (not its VM)
 194      * for passing as VM options to its child group VMs.
 195      * Returns the extra command line arguments required
 196      * to turn on jcov code coverage analysis for rmid child VMs.
 197      */
 198     protected static String getCodeCoverageArgs() {
 199         return TestLibrary.getExtraProperty("rmid.jcov.args","");
 200     }
 201 
 202     public void start() throws IOException {
 203         start(10000);
 204     }
 205 
 206     public void slowStart() throws IOException {
 207         start(60000);
 208     }
 209 


















 210     public void start(long waitTime) throws IOException {
 211 
 212         // if rmid is already running, then the test will fail with
 213         // a well recognized exception (port already in use...).
 214 
 215         mesg("starting rmid on port #" + port + "...");
 216         super.start();
 217 
 218         int slopFactor = 1;
 219         try {
 220             slopFactor = Integer.valueOf(
 221                 TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
 222         } catch (NumberFormatException ignore) {}
 223         waitTime = waitTime * slopFactor;
 224 
 225         // We check several times (as many as provides passed waitTime) to
 226         // see if Rmid is currently running. Waiting steps last 100 msecs.
 227         final long rmidStartSleepTime = 100;
 228         do {
 229             // Sleeping for another rmidStartSleepTime time slice.
 230             try {
 231                 Thread.sleep(Math.min(waitTime, rmidStartSleepTime));
 232             } catch (InterruptedException ie) {
 233                 Thread.currentThread().interrupt();
 234                 mesg("Thread interrupted while checking for start of Activation System. Giving up check.");
 235                 mesg("Activation System state unknown");
 236                 return;
 237             }
 238             waitTime -= rmidStartSleepTime;
 239 
 240             // Checking if rmid is present
 241             if (ActivationLibrary.rmidRunning(port)) {
 242                 /**
 243                  * We need to set the java.rmi.activation.port value as the
 244                  * activation system will use the property to determine the
 245                  * port #.  The activation system will use this value if set.
 246                  * If it isn't set, the activation system will set it to an
 247                  * incorrect value.
 248                  */
 249                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
 250                 mesg("finished starting rmid.");
 251                 return;
 252             }
 253             else {
 254                 mesg("rmid still not started");
 255             }
 256 
 257         } while (waitTime > 0);
 258         TestLibrary.bomb("start rmid failed... giving up", null);
 259     }
 260 
 261     public void restart() throws IOException {
 262         destroy();
 263         start();
 264     }
 265 
 266     /**
 267      * Ask rmid to shutdown gracefully using a remote method call.
 268      * catch any errors that might occur from rmid not being present
 269      * at time of shutdown invocation.
 270      *
 271      * Shutdown does not nullify possible references to the rmid
 272      * process object (destroy does though).
 273      */
 274     public static void shutdown(int port) {
 275 
 276         try {
 277             ActivationSystem system = null;
 278 
 279             try {
 280                 mesg("getting a reference to the activation system");
 281                 system = (ActivationSystem) Naming.lookup("//:" +
 282                     port +
 283                     "/java.rmi.activation.ActivationSystem");
 284                 mesg("obtained a reference to the activation system");
 285             } catch (RemoteException re) {
 286                 mesg("could not contact registry while trying to shutdown activation system");
 287             } catch (java.net.MalformedURLException mue) {
 288             }
 289 
 290             if (system == null) {
 291                 TestLibrary.bomb("reference to the activation system was null");
 292             }
 293             system.shutdown();
 294 

 295         } catch (RemoteException re) {
 296             mesg("shutting down the activation daemon failed");
 297         } catch (Exception e) {
 298             mesg("caught exception trying to shutdown rmid");
 299             mesg(e.getMessage());
 300             e.printStackTrace();
 301         }
 302 
 303         mesg("testlibrary finished shutting down rmid");
 304     }
 305 
 306     /**
 307      * Ask rmid to shutdown gracefully but then destroy the rmid
 308      * process if it does not exit by itself.  This method only works
 309      * if rmid is a child process of the current VM.
 310      */
 311     public void destroy() {
 312         // attempt graceful shutdown of the activation system
 313         shutdown(port);
 314 




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *
  26  */
  27 
  28 import java.io.*;
  29 import java.rmi.*;
  30 import java.rmi.activation.*;
  31 import java.rmi.registry.*;
  32 
  33 /**
  34  * Utility class that creates an instance of rmid with a policy
  35  * file of name <code>TestParams.defaultPolicy</code>.
  36  *
  37  * Activation groups should run with the same security manager as the
  38  * test.
  39  */
  40 public class RMID extends JavaVM {
  41 
  42     private static final String SYSTEM_NAME = ActivationSystem.class.getName();
  43         // "java.rmi.activation.ActivationSystem"
  44 
  45     public static String MANAGER_OPTION="-Djava.security.manager=";
  46 
  47     /** Test port for rmid */
  48     private final int port;
  49 
  50     /** Initial log name */
  51     protected static String log = "log";
  52     /** rmid's logfile directory; currently must be "." */
  53     protected static String LOGDIR = ".";
  54 
  55     private static void mesg(Object mesg) {
  56         System.err.println("RMID: " + mesg.toString());
  57     }
  58 
  59     /** make test options and arguments */
  60     private static String makeOptions(boolean debugExec) {
  61 
  62         String options = " -Dsun.rmi.server.activation.debugExec=" +
  63             debugExec;
  64         // +


 194     }
 195 
 196     /**
 197      * This method is used for adding arguments to rmid (not its VM)
 198      * for passing as VM options to its child group VMs.
 199      * Returns the extra command line arguments required
 200      * to turn on jcov code coverage analysis for rmid child VMs.
 201      */
 202     protected static String getCodeCoverageArgs() {
 203         return TestLibrary.getExtraProperty("rmid.jcov.args","");
 204     }
 205 
 206     public void start() throws IOException {
 207         start(10000);
 208     }
 209 
 210     public void slowStart() throws IOException {
 211         start(60000);
 212     }
 213 
 214     /**
 215      * Looks up the activation system in the registry on the given port,
 216      * returning its stub, or null if it's not present. This method differs from
 217      * ActivationGroup.getSystem() because this method looks on a specific port
 218      * instead of using the java.rmi.activation.port property like
 219      * ActivationGroup.getSystem() does. This method also returns null instead
 220      * of throwing exceptions.
 221      */
 222     public static ActivationSystem lookupSystem(int port) {
 223         mesg("getting activation system");
 224         try {
 225             return (ActivationSystem)LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
 226         } catch (RemoteException | NotBoundException ex) {
 227             mesg("couldn't get activation system: " + ex);
 228             return null;
 229         }
 230     }
 231 
 232     public void start(long waitTime) throws IOException {
 233 
 234         // if rmid is already running, then the test will fail with
 235         // a well recognized exception (port already in use...).
 236 
 237         mesg("starting rmid on port #" + port + "...");
 238         super.start();
 239 
 240         int slopFactor = 1;
 241         try {
 242             slopFactor = Integer.valueOf(
 243                 TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
 244         } catch (NumberFormatException ignore) {}
 245         waitTime = waitTime * slopFactor;
 246 
 247         // We check several times (as many as provides passed waitTime) to
 248         // see if Rmid is currently running. Waiting steps last 100 msecs.
 249         final long rmidStartSleepTime = 100;
 250         do {
 251             // Sleeping for another rmidStartSleepTime time slice.
 252             try {
 253                 Thread.sleep(Math.min(waitTime, rmidStartSleepTime));
 254             } catch (InterruptedException ie) {
 255                 Thread.currentThread().interrupt();
 256                 mesg("Thread interrupted while checking for start of Activation System. Giving up check.");
 257                 mesg("Activation System state unknown");
 258                 return;
 259             }
 260             waitTime -= rmidStartSleepTime;
 261 
 262             // Checking if rmid is present
 263             if (lookupSystem(port) != null) {
 264                 /*
 265                  * We need to set the java.rmi.activation.port value as the
 266                  * activation system will use the property to determine the
 267                  * port #.  The activation system will use this value if set.
 268                  * If it isn't set, the activation system will set it to an
 269                  * incorrect value.
 270                  */
 271                 System.setProperty("java.rmi.activation.port", Integer.toString(port));
 272                 mesg("finished starting rmid.");
 273                 return;
 274             }
 275             else {
 276                 mesg("rmid still not started");
 277             }
 278 
 279         } while (waitTime > 0);
 280         TestLibrary.bomb("start rmid failed... giving up", null);
 281     }
 282 
 283     public void restart() throws IOException {
 284         destroy();
 285         start();
 286     }
 287 
 288     /**
 289      * Ask rmid to shutdown gracefully using a remote method call.
 290      * catch any errors that might occur from rmid not being present
 291      * at time of shutdown invocation.
 292      *
 293      * Shutdown does not nullify possible references to the rmid
 294      * process object (destroy does though).
 295      */
 296     public static void shutdown(int port) {
 297 
 298         try {
 299             ActivationSystem system = lookupSystem(port);











 300 
 301             if (system == null) {
 302                 TestLibrary.bomb("reference to the activation system was null");
 303             }

 304 
 305             system.shutdown();
 306         } catch (RemoteException re) {
 307             mesg("shutting down the activation daemon failed");
 308         } catch (Exception e) {
 309             mesg("caught exception trying to shutdown rmid");
 310             mesg(e.getMessage());
 311             e.printStackTrace();
 312         }
 313 
 314         mesg("testlibrary finished shutting down rmid");
 315     }
 316 
 317     /**
 318      * Ask rmid to shutdown gracefully but then destroy the rmid
 319      * process if it does not exit by itself.  This method only works
 320      * if rmid is a child process of the current VM.
 321      */
 322     public void destroy() {
 323         // attempt graceful shutdown of the activation system
 324         shutdown(port);
 325