test/java/rmi/reliability/juicer/AppleUserImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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  */


  42  * RMI profile, ApplicationServer must be started by AppleUserImpl
  43  * and the complete juicer runs in a single process.
  44  *
  45  * The second server process instructs the AppleUserImpl to "use" some apples.
  46  * AppleUserImpl creates a new thread for each apple.  These threads
  47  * initiate the two party recursion.
  48  *
  49  * Each recursive call nests to a depth determined by this
  50  * expression: (2 + Math.abs(random.nextInt() % (maxLevel + 1)),
  51  * where maxLevel is a command line parameter.  Thus each recursive
  52  * call nests a random number of levels between 2 and maxLevel.
  53  *
  54  * The test ends when an exception is encountered or the stop time
  55  * has been reached.
  56  *
  57  * @library ../../testlibrary
  58  *
  59  * @build Apple AppleEvent AppleImpl AppleUserImpl
  60  * @build Orange OrangeEcho OrangeEchoImpl OrangeImpl
  61  * @build ApplicationServer

  62  *
  63  * @run main/othervm/policy=security.policy AppleUserImpl -seconds 30
  64  *
  65  * @author Peter Jones, Nigel Daley
  66  */
  67 
  68 import java.rmi.RemoteException;
  69 import java.rmi.NoSuchObjectException;
  70 import java.rmi.server.UnicastRemoteObject;
  71 import java.rmi.registry.LocateRegistry;


  72 import java.util.Random;
  73 import java.util.logging.Logger;
  74 import java.util.logging.Level;

  75 
  76 /**
  77  * The AppleUserImpl class implements the behavior of the remote
  78  * "apple user" objects exported by the server.  The application server
  79  * passes each of its remote "apple" objects to an apple user, and an
  80  * AppleUserThread is created for each apple.
  81  */
  82 public class AppleUserImpl extends UnicastRemoteObject implements AppleUser {
  83 
  84     private static final Logger logger =
  85         Logger.getLogger("reliability.appleuser");
  86     private static int threadNum = 0;
  87     private static long testDuration = 0;
  88     private static int maxLevel = 7;
  89     private static Exception status = null;
  90     private static boolean finished = false;
  91     private static boolean startTestNotified = false;
  92     private static final Random random = new Random();
  93     private static final Object lock = new Object();
  94 
  95     public AppleUserImpl() throws RemoteException {
  96     }
  97 
  98     /**
  99      * Allows the other server process to indicate that it is ready
 100      * to start "juicing".
 101      */
 102     public synchronized void startTest() throws RemoteException {
 103         startTestNotified = true;


 291                     usage();
 292                 }
 293             }
 294             if (durationString == null) {
 295                 durationString = testDuration + " milliseconds";
 296             }
 297         } catch (Throwable t) {
 298             usage();
 299             throw new RuntimeException("TEST FAILED: Bad argument");
 300         }
 301 
 302         AppleUserImpl user = null;
 303         long startTime = 0;
 304         Thread server = null;
 305         int exitValue = 0;
 306         try {
 307             user = new AppleUserImpl();
 308 
 309             synchronized (user) {
 310                 // create new registry and bind new AppleUserImpl in registry
 311                 LocateRegistry.createRegistry(2006);
 312                 LocateRegistry.getRegistry(2006).rebind("AppleUser",user);


 313 
 314                 // start the other server if applicable
 315                 if (othervm) {
 316                     // the other server must be running in a separate process
 317                     logger.log(Level.INFO, "Application server must be " +
 318                         "started in separate process");
 319                 } else {
 320                     Class app = Class.forName("ApplicationServer");
 321                     server = new Thread((Runnable) app.newInstance());


 322                     logger.log(Level.INFO, "Starting application server " +
 323                         "in same process");
 324                     server.start();
 325                 }
 326 
 327                 // wait for other server to call startTest method
 328                 logger.log(Level.INFO, "Waiting for application server " +
 329                     "process to start");
 330                 while (!startTestNotified) {
 331                    user.wait();
 332                 }
 333             }
 334 
 335             startTime = System.currentTimeMillis();
 336             logger.log(Level.INFO, "Test starting");
 337 
 338             // wait for exception to be reported or first thread to complete
 339             logger.log(Level.INFO, "Waiting " + durationString + " for " +
 340                 "test to complete or exception to be thrown");
 341 


   1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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  */


  42  * RMI profile, ApplicationServer must be started by AppleUserImpl
  43  * and the complete juicer runs in a single process.
  44  *
  45  * The second server process instructs the AppleUserImpl to "use" some apples.
  46  * AppleUserImpl creates a new thread for each apple.  These threads
  47  * initiate the two party recursion.
  48  *
  49  * Each recursive call nests to a depth determined by this
  50  * expression: (2 + Math.abs(random.nextInt() % (maxLevel + 1)),
  51  * where maxLevel is a command line parameter.  Thus each recursive
  52  * call nests a random number of levels between 2 and maxLevel.
  53  *
  54  * The test ends when an exception is encountered or the stop time
  55  * has been reached.
  56  *
  57  * @library ../../testlibrary
  58  *
  59  * @build Apple AppleEvent AppleImpl AppleUserImpl
  60  * @build Orange OrangeEcho OrangeEchoImpl OrangeImpl
  61  * @build ApplicationServer
  62  * @build TestLibrary
  63  *
  64  * @run main/othervm/policy=security.policy AppleUserImpl -seconds 30
  65  *
  66  * @author Peter Jones, Nigel Daley
  67  */
  68 

  69 import java.rmi.NoSuchObjectException;
  70 import java.rmi.RemoteException;
  71 import java.rmi.registry.LocateRegistry;
  72 import java.rmi.registry.Registry;
  73 import java.rmi.server.UnicastRemoteObject;
  74 import java.util.Random;

  75 import java.util.logging.Level;
  76 import java.util.logging.Logger;
  77 
  78 /**
  79  * The AppleUserImpl class implements the behavior of the remote
  80  * "apple user" objects exported by the server.  The application server
  81  * passes each of its remote "apple" objects to an apple user, and an
  82  * AppleUserThread is created for each apple.
  83  */
  84 public class AppleUserImpl extends UnicastRemoteObject implements AppleUser {
  85     private static int registryPort = -1;
  86     private static final Logger logger =
  87         Logger.getLogger("reliability.appleuser");
  88     private static int threadNum = 0;
  89     private static long testDuration = 0;
  90     private static int maxLevel = 7;
  91     private static Exception status = null;
  92     private static boolean finished = false;
  93     private static boolean startTestNotified = false;
  94     private static final Random random = new Random();
  95     private static final Object lock = new Object();
  96 
  97     public AppleUserImpl() throws RemoteException {
  98     }
  99 
 100     /**
 101      * Allows the other server process to indicate that it is ready
 102      * to start "juicing".
 103      */
 104     public synchronized void startTest() throws RemoteException {
 105         startTestNotified = true;


 293                     usage();
 294                 }
 295             }
 296             if (durationString == null) {
 297                 durationString = testDuration + " milliseconds";
 298             }
 299         } catch (Throwable t) {
 300             usage();
 301             throw new RuntimeException("TEST FAILED: Bad argument");
 302         }
 303 
 304         AppleUserImpl user = null;
 305         long startTime = 0;
 306         Thread server = null;
 307         int exitValue = 0;
 308         try {
 309             user = new AppleUserImpl();
 310 
 311             synchronized (user) {
 312                 // create new registry and bind new AppleUserImpl in registry
 313                 Registry registry = TestLibrary.createRegistryOnUnusedPort();
 314                 registryPort = TestLibrary.getRegistryPort(registry);
 315                 LocateRegistry.getRegistry(registryPort).rebind("AppleUser",
 316                                                                  user);
 317 
 318                 // start the other server if applicable
 319                 if (othervm) {
 320                     // the other server must be running in a separate process
 321                     logger.log(Level.INFO, "Application server must be " +
 322                         "started in separate process");
 323                 } else {
 324                     Class app = Class.forName("ApplicationServer");
 325                     java.lang.reflect.Constructor appConstructor =
 326                             app.getDeclaredConstructor(new Class[] {Integer.TYPE});
 327                     server = new Thread((Runnable) appConstructor.newInstance(registryPort));
 328                     logger.log(Level.INFO, "Starting application server " +
 329                         "in same process");
 330                     server.start();
 331                 }
 332 
 333                 // wait for other server to call startTest method
 334                 logger.log(Level.INFO, "Waiting for application server " +
 335                     "process to start");
 336                 while (!startTestNotified) {
 337                    user.wait();
 338                 }
 339             }
 340 
 341             startTime = System.currentTimeMillis();
 342             logger.log(Level.INFO, "Test starting");
 343 
 344             // wait for exception to be reported or first thread to complete
 345             logger.log(Level.INFO, "Waiting " + durationString + " for " +
 346                 "test to complete or exception to be thrown");
 347