169N/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.registry.Registry;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.util.logging.Logger;
0N/Aimport java.util.logging.Level;
0N/A
0N/A/**
0N/A * The ApplicationServer class provides the other server side of the "juicer"
0N/A * stress test of RMI.
0N/A */
0N/Apublic class ApplicationServer implements Runnable {
169N/A
0N/A /** number of remote Apple objects to export */
0N/A private static final Logger logger = Logger.getLogger("reliability.orange");
0N/A private static final int LOOKUP_ATTEMPTS = 5;
0N/A private static final int DEFAULT_NUMAPPLES = 10;
0N/A private static final String DEFAULT_REGISTRYHOST = "localhost";
5266N/A private static final int DEFAULT_REGISTRYPORT = -1;
0N/A private final int numApples;
0N/A private final String registryHost;
5266N/A private final int registryPort;
0N/A private final Apple[] apples;
0N/A private AppleUser user;
0N/A
5266N/A ApplicationServer(int registryPort) {
5266N/A this(DEFAULT_NUMAPPLES, DEFAULT_REGISTRYHOST, registryPort);
0N/A }
0N/A
5266N/A ApplicationServer(int numApples, String registryHost, int registryPort) {
0N/A this.numApples = numApples;
0N/A this.registryHost = registryHost;
5266N/A this.registryPort = registryPort;
0N/A apples = new Apple[numApples];
0N/A }
0N/A
0N/A /*
0N/A * On initialization, export remote objects and register
0N/A * them with server.
0N/A */
0N/A public void run() {
169N/A try {
169N/A int i = 0;
0N/A
169N/A /*
169N/A * Locate apple user object in registry. The lookup will
169N/A * occur until it is successful or fails LOOKUP_ATTEMPTS times.
169N/A * These repeated attempts allow the ApplicationServer
169N/A * to be started before the AppleUserImpl.
169N/A */
169N/A Exception exc = null;
169N/A for (i = 0; i < LOOKUP_ATTEMPTS; i++) {
169N/A try {
169N/A Registry registry = LocateRegistry.getRegistry(
5266N/A registryHost, registryPort);
169N/A user = (AppleUser) registry.lookup("AppleUser");
169N/A user.startTest();
169N/A break; //successfully obtained AppleUser
169N/A } catch (Exception e) {
169N/A exc = e;
169N/A Thread.sleep(10000); //sleep 10 seconds and try again
169N/A }
169N/A }
169N/A if (user == null) {
169N/A logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc);
169N/A return;
169N/A }
0N/A
169N/A /*
169N/A * Create and export apple implementations.
169N/A */
169N/A try {
169N/A for (i = 0; i < numApples; i++) {
169N/A apples[i] = new AppleImpl("AppleImpl #" + (i + 1));
169N/A }
169N/A } catch (RemoteException e) {
169N/A logger.log(Level.SEVERE,
169N/A "Failed to create AppleImpl #" + (i + 1) + ":", e);
169N/A user.reportException(e);
169N/A return;
169N/A }
0N/A
169N/A /*
169N/A * Hand apple objects to apple user.
169N/A */
169N/A try {
169N/A for (i = 0; i < numApples; i++) {
169N/A user.useApple(apples[i]);
0N/A }
169N/A } catch (RemoteException e) {
169N/A logger.log(Level.SEVERE,
169N/A "Failed to register callbacks for " + apples[i] + ":", e);
169N/A user.reportException(e);
169N/A return;
169N/A }
169N/A } catch (Exception e) {
169N/A logger.log(Level.SEVERE, "Unexpected exception:", e);
169N/A }
0N/A }
0N/A
0N/A private static void usage() {
169N/A System.err.println("Usage: ApplicationServer [-numApples <numApples>]");
169N/A System.err.println(" [-registryHost <host>]");
5266N/A System.err.println(" -registryPort <port>");
169N/A System.err.println(" numApples The number of apples (threads) to use.");
169N/A System.err.println(" The default is 10 apples.");
169N/A System.err.println(" host The host running rmiregistry " +
169N/A "which contains AppleUser.");
169N/A System.err.println(" The default is \"localhost\".");
5266N/A System.err.println(" port The port the rmiregistry is running" +
5266N/A "on.");
169N/A System.err.println();
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A int num = DEFAULT_NUMAPPLES;
5266N/A int port = -1;
0N/A String host = DEFAULT_REGISTRYHOST;
0N/A
169N/A // parse command line args
169N/A try {
0N/A for (int i = 0; i < args.length ; i++ ) {
169N/A String arg = args[i];
169N/A if (arg.equals("-numApples")) {
0N/A i++;
0N/A num = Integer.parseInt(args[i]);
169N/A } else if (arg.equals("-registryHost")) {
0N/A i++;
0N/A host = args[i];
5266N/A } else if (arg.equals("-registryPort")) {
5266N/A i++;
5266N/A port = Integer.parseInt(args[i]);
169N/A } else {
0N/A usage();
169N/A }
0N/A }
5266N/A
5266N/A if (port == -1) {
5266N/A usage();
5266N/A throw new RuntimeException("Port must be specified.");
5266N/A }
169N/A } catch (Throwable t) {
0N/A usage();
169N/A throw new RuntimeException("TEST FAILED: Bad argument");
169N/A }
0N/A
169N/A // start the client server
5266N/A Thread server = new Thread(new ApplicationServer(num,host,port));
169N/A server.start();
169N/A // main should exit once all exported remote objects are gc'd
0N/A }
0N/A}