blob: 050da0e3b7ba200ba47740bec3e62230c13d6f65 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.rmi.RemoteException;
25import java.rmi.registry.Registry;
26import java.rmi.registry.LocateRegistry;
27import java.util.logging.Logger;
28import java.util.logging.Level;
29
30/**
31 * The ApplicationServer class provides the other server side of the "juicer"
32 * stress test of RMI.
33 */
34public class ApplicationServer implements Runnable {
35
36 /** number of remote Apple objects to export */
37 private static final Logger logger = Logger.getLogger("reliability.orange");
38 private static final int LOOKUP_ATTEMPTS = 5;
39 private static final int DEFAULT_NUMAPPLES = 10;
40 private static final String DEFAULT_REGISTRYHOST = "localhost";
41 private final int numApples;
42 private final String registryHost;
43 private final Apple[] apples;
44 private AppleUser user;
45
46 ApplicationServer() {
47 this(DEFAULT_NUMAPPLES, DEFAULT_REGISTRYHOST);
48 }
49
50 ApplicationServer(int numApples, String registryHost) {
51 this.numApples = numApples;
52 this.registryHost = registryHost;
53 apples = new Apple[numApples];
54 }
55
56 /*
57 * On initialization, export remote objects and register
58 * them with server.
59 */
60 public void run() {
61 try {
62 int i = 0;
63
64 /*
65 * Locate apple user object in registry. The lookup will
66 * occur until it is successful or fails LOOKUP_ATTEMPTS times.
67 * These repeated attempts allow the ApplicationServer
68 * to be started before the AppleUserImpl.
69 */
70 Exception exc = null;
71 for (i = 0; i < LOOKUP_ATTEMPTS; i++) {
72 try {
73 Registry registry = LocateRegistry.getRegistry(
74 registryHost, 2006);
75 user = (AppleUser) registry.lookup("AppleUser");
76 user.startTest();
77 break; //successfully obtained AppleUser
78 } catch (Exception e) {
79 exc = e;
80 Thread.sleep(10000); //sleep 10 seconds and try again
81 }
82 }
83 if (user == null) {
84 logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc);
85 return;
86 }
87
88 /*
89 * Create and export apple implementations.
90 */
91 try {
92 for (i = 0; i < numApples; i++) {
93 apples[i] = new AppleImpl("AppleImpl #" + (i + 1));
94 }
95 } catch (RemoteException e) {
96 logger.log(Level.SEVERE,
97 "Failed to create AppleImpl #" + (i + 1) + ":", e);
98 user.reportException(e);
99 return;
100 }
101
102 /*
103 * Hand apple objects to apple user.
104 */
105 try {
106 for (i = 0; i < numApples; i++) {
107 user.useApple(apples[i]);
108 }
109 } catch (RemoteException e) {
110 logger.log(Level.SEVERE,
111 "Failed to register callbacks for " + apples[i] + ":", e);
112 user.reportException(e);
113 return;
114 }
115 } catch (Exception e) {
116 logger.log(Level.SEVERE, "Unexpected exception:", e);
117 }
118 }
119
120 private static void usage() {
121 System.err.println("Usage: ApplicationServer [-numApples <numApples>]");
122 System.err.println(" [-registryHost <host>]");
123 System.err.println(" numApples The number of apples (threads) to use.");
124 System.err.println(" The default is 10 apples.");
125 System.err.println(" host The host running rmiregistry " +
126 "which contains AppleUser.");
127 System.err.println(" The default is \"localhost\".");
128 System.err.println();
129 }
130
131 public static void main(String[] args) {
132 int num = DEFAULT_NUMAPPLES;
133 String host = DEFAULT_REGISTRYHOST;
134
135 // parse command line args
136 try {
137 for (int i = 0; i < args.length ; i++ ) {
138 String arg = args[i];
139 if (arg.equals("-numApples")) {
140 i++;
141 num = Integer.parseInt(args[i]);
142 } else if (arg.equals("-registryHost")) {
143 i++;
144 host = args[i];
145 } else {
146 usage();
147 }
148 }
149 } catch (Throwable t) {
150 usage();
151 throw new RuntimeException("TEST FAILED: Bad argument");
152 }
153
154 // start the client server
155 Thread server = new Thread(new ApplicationServer(num,host));
156 server.start();
157 // main should exit once all exported remote objects are gc'd
158 }
159}