0N/A/*
5266N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.Naming;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/Aimport java.rmi.registry.LocateRegistry;
5266N/Aimport java.rmi.registry.Registry;
0N/Aimport java.util.Random;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Date;
0N/Aimport java.util.logging.Logger;
0N/Aimport java.util.logging.Level;
0N/A
0N/A/**
0N/A * The AppleUserImpl class implements the behavior of the remote
0N/A * "apple user" objects exported by the server. The application server
0N/A * passes each of its remote "apple" objects to an apple user, and an
0N/A * AppleUserThread is created for each apple.
0N/A */
0N/Apublic class AppleUserImpl
0N/A extends UnicastRemoteObject
0N/A implements AppleUser
0N/A{
0N/A private static Logger logger = Logger.getLogger("reliability.appleuser");
0N/A private static int threadNum = 0;
0N/A private static long testDuration = 0;
0N/A private static int maxLevel = 7;
0N/A private static Thread server = null;
0N/A private static Exception status = null;
0N/A private static Random random = new Random();
0N/A
0N/A public AppleUserImpl() throws RemoteException {
0N/A }
0N/A
0N/A /**
0N/A * Allows the other server process to indicate that it is ready
0N/A * to start "juicing".
0N/A */
0N/A public synchronized void startTest() throws RemoteException {
0N/A this.notifyAll();
0N/A }
0N/A
0N/A /**
0N/A * Allows the other server process to report an exception to this
0N/A * process and thereby terminate the test.
0N/A */
0N/A public void reportException(Exception status) throws RemoteException {
0N/A synchronized (AppleUserImpl.class) {
0N/A this.status = status;
0N/A AppleUserImpl.class.notifyAll();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * "Use" supplied apple object. Create an AppleUserThread to
0N/A * stress it out.
0N/A */
0N/A public synchronized void useApple(Apple apple) throws RemoteException {
0N/A String threadName = Thread.currentThread().getName();
0N/A logger.log(Level.FINEST,
0N/A threadName + ": AppleUserImpl.useApple(): BEGIN");
0N/A
0N/A AppleUserThread t =
0N/A new AppleUserThread("AppleUserThread-" + (++threadNum), apple);
0N/A t.start();
0N/A
0N/A logger.log(Level.FINEST,
0N/A threadName + ": AppleUserImpl.useApple(): END");
0N/A }
0N/A
0N/A /**
0N/A * The AppleUserThread class repeatedly invokes calls on its associated
0N/A * Apple object to stress the RMI system.
0N/A */
0N/A class AppleUserThread extends Thread {
0N/A
0N/A Apple apple;
0N/A
0N/A public AppleUserThread(String name, Apple apple) {
0N/A super(name);
0N/A this.apple = apple;
0N/A }
0N/A
0N/A public void run() {
0N/A int orangeNum = 0;
0N/A long stopTime = System.currentTimeMillis() + testDuration;
0N/A Logger logger = Logger.getLogger("reliability.appleuserthread");
0N/A
0N/A try {
0N/A do { // loop until stopTime is reached
0N/A
0N/A /*
0N/A * Notify apple with some apple events. This tests
0N/A * serialization of arrays.
0N/A */
0N/A int numEvents = Math.abs(random.nextInt() % 5);
0N/A AppleEvent[] events = new AppleEvent[numEvents];
0N/A for (int i = 0; i < events.length; ++ i) {
0N/A events[i] = new AppleEvent(orangeNum % 3);
0N/A }
0N/A apple.notify(events);
0N/A
0N/A /*
0N/A * Request a new orange object be created in
0N/A * the application server.
0N/A */
0N/A Orange orange = apple.newOrange(
0N/A "Orange(" + getName() + ")-" + (++orangeNum));
0N/A
0N/A /*
0N/A * Create a large message of random ints to pass to orange.
0N/A */
0N/A int msgLength = 1000 + Math.abs(random.nextInt() % 3000);
0N/A int[] message = new int[msgLength];
0N/A for (int i = 0; i < message.length; ++ i) {
0N/A message[i] = random.nextInt();
0N/A }
0N/A
0N/A /*
0N/A * Invoke recursive call on the orange. Base case
0N/A * of recursion inverts messgage.
0N/A */
0N/A OrangeEchoImpl echo = new OrangeEchoImpl(
0N/A "OrangeEcho(" + getName() + ")-" + orangeNum);
0N/A int[] response = orange.recurse(echo, message,
0N/A 2 + Math.abs(random.nextInt() % (maxLevel + 1)));
0N/A
0N/A /*
0N/A * Verify message was properly inverted and not corrupted
0N/A * through all the recursive method invocations.
0N/A */
0N/A if (response.length != message.length) {
0N/A throw new RuntimeException(
0N/A "ERROR: CORRUPTED RESPONSE: " +
0N/A "wrong length of returned array " + "(should be " +
0N/A message.length + ", is " + response.length + ")");
0N/A }
0N/A for (int i = 0; i < message.length; ++ i) {
0N/A if (~message[i] != response[i]) {
0N/A throw new RuntimeException(
0N/A "ERROR: CORRUPTED RESPONSE: " +
0N/A "at element " + i + "/" + message.length +
0N/A " of returned array (should be " +
0N/A Integer.toHexString(~message[i]) + ", is " +
0N/A Integer.toHexString(response[i]) + ")");
0N/A }
0N/A }
0N/A
0N/A try {
0N/A Thread.sleep(Math.abs(random.nextInt() % 10) * 1000);
0N/A } catch (InterruptedException e) {
0N/A }
0N/A
0N/A } while (System.currentTimeMillis() < stopTime);
0N/A
0N/A } catch (Exception e) {
0N/A status = e;
0N/A }
0N/A synchronized (AppleUserImpl.class) {
0N/A AppleUserImpl.class.notifyAll();
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void usage() {
0N/A System.out.println("Usage: AppleUserImpl [-hours <hours> | " +
0N/A "-seconds <seconds>]");
0N/A System.out.println(" [-maxLevel <maxLevel>]");
0N/A System.out.println(" hours The number of hours to run the juicer.");
0N/A System.out.println(" The default is 0 hours.");
0N/A System.out.println(" seconds The number of seconds to run the juicer.");
0N/A System.out.println(" The default is 0 seconds.");
0N/A System.out.println(" maxLevel The maximum number of levels to ");
0N/A System.out.println(" recurse on each call.");
0N/A System.out.println(" The default is 7 levels.");
0N/A //TestLibrary.bomb("Bad argument");
0N/A }
0N/A
0N/A /**
0N/A * Entry point for the "juicer" server process. Create and export
0N/A * an apple user implementation in an rmiregistry running on localhost.
0N/A */
0N/A public static void main(String[] args)
0N/A
0N/A {
0N/A //TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
0N/A long startTime = 0;
0N/A String durationString = null;
0N/A
0N/A // parse command line args
0N/A try {
0N/A for (int i = 0; i < args.length ; i++ ) {
0N/A String arg = args[i];
0N/A if (arg.equals("-hours")) {
0N/A if (durationString != null) {
0N/A usage();
0N/A }
0N/A i++;
0N/A int hours = Integer.parseInt(args[i]);
0N/A durationString = hours + " hours";
0N/A testDuration = hours * 60 * 60 * 1000;
0N/A } else if (arg.equals("-seconds")) {
0N/A if (durationString != null) {
0N/A usage();
0N/A }
0N/A i++;
0N/A long seconds = Long.parseLong(args[i]);
0N/A durationString = seconds + " seconds";
0N/A testDuration = seconds * 1000;
0N/A } else if (arg.equals("-maxLevel")) {
0N/A i++;
0N/A maxLevel = Integer.parseInt(args[i]);
0N/A } else {
0N/A usage();
0N/A }
0N/A }
0N/A if (durationString == null) {
0N/A durationString = testDuration + " milliseconds";
0N/A }
0N/A } catch (Throwable t) {
0N/A usage();
0N/A }
0N/A
0N/A AppleUserImpl user = null;
0N/A try {
0N/A user = new AppleUserImpl();
0N/A } catch (RemoteException e) {
0N/A //TestLibrary.bomb("Failed to create AppleUser", e);
0N/A }
0N/A
0N/A synchronized (user) {
5266N/A int port = -1;
0N/A // create new registry and bind new AppleUserImpl in registry
0N/A try {
5266N/A Registry registry = TestLibrary.createRegistryOnUnusedPort();
5266N/A port = TestLibrary.getRegistryPort(registry);
5266N/A Naming.rebind("rmi://localhost:" + port + "/AppleUser",user);
0N/A } catch (RemoteException e) {
0N/A //TestLibrary.bomb("Failed to bind AppleUser", e);
0N/A } catch (java.net.MalformedURLException e) {
0N/A //TestLibrary.bomb("Failed to bind AppleUser", e);
0N/A }
0N/A
0N/A // start the other server if available
0N/A try {
0N/A Class app = Class.forName("ApplicationServer");
5266N/A java.lang.reflect.Constructor appConstructor =
5266N/A app.getDeclaredConstructor(new Class[] {Integer.TYPE});
5266N/A server = new Thread((Runnable) appConstructor.newInstance(port));
0N/A } catch (ClassNotFoundException e) {
0N/A // assume the other server is running in a separate process
0N/A logger.log(Level.INFO, "Application server must be " +
0N/A "started in separate process");
0N/A } catch (Exception ie) {
0N/A //TestLibrary.bomb("Could not instantiate server", ie);
0N/A }
0N/A
0N/A // wait for other server to call startTest method
0N/A try {
0N/A logger.log(Level.INFO, "Waiting for application server " +
0N/A "process to start");
0N/A user.wait();
0N/A } catch (InterruptedException ie) {
0N/A //TestLibrary.bomb("AppleUserImpl interrupted", ie);
0N/A }
0N/A }
0N/A
0N/A startTime = System.currentTimeMillis();
0N/A logger.log(Level.INFO, "Test starting");
0N/A
0N/A // wait for exception to be reported or first thread to complete
0N/A try {
0N/A logger.log(Level.INFO, "Waiting " + durationString + " for " +
0N/A "test to complete or exception to be thrown");
0N/A
0N/A synchronized (AppleUserImpl.class) {
0N/A AppleUserImpl.class.wait();
0N/A }
0N/A
0N/A if (status != null) {
0N/A //TestLibrary.bomb("juicer server reported an exception", status);
0N/A } else {
0N/A logger.log(Level.INFO, "TEST PASSED");
0N/A }
0N/A } catch (Exception e) {
0N/A logger.log(Level.INFO, "TEST FAILED");
0N/A //TestLibrary.bomb("unexpected exception", e);
0N/A } finally {
0N/A logger.log(Level.INFO, "Test finished");
0N/A long actualDuration = System.currentTimeMillis() - startTime;
0N/A logger.log(Level.INFO, "Test duration was " +
0N/A (actualDuration/1000) + " seconds " +
0N/A "(" + (actualDuration/3600000) + " hours)");
0N/A }
0N/A }
0N/A}