Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/rmi/reliability/juicer/ApplicationServer.java
41153 views
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
*/
23
24
import java.rmi.NotBoundException;
25
import java.rmi.RemoteException;
26
import java.rmi.registry.Registry;
27
import java.rmi.registry.LocateRegistry;
28
import java.util.logging.Logger;
29
import java.util.logging.Level;
30
31
/**
32
* The ApplicationServer class provides the other server side of the "juicer"
33
* stress test of RMI.
34
*/
35
public class ApplicationServer implements Runnable {
36
37
/** number of remote Apple objects to export */
38
private static final Logger logger = Logger.getLogger("reliability.orange");
39
private static final int LOOKUP_ATTEMPTS = 5;
40
private static final int DEFAULT_NUMAPPLES = 10;
41
private static final String DEFAULT_REGISTRYHOST = "localhost";
42
private static final int DEFAULT_REGISTRYPORT = -1;
43
private final int numApples;
44
private final String registryHost;
45
private final int registryPort;
46
private final Apple[] apples;
47
private AppleUser user;
48
49
ApplicationServer(int registryPort) {
50
this(DEFAULT_NUMAPPLES, DEFAULT_REGISTRYHOST, registryPort);
51
}
52
53
ApplicationServer(int numApples, String registryHost, int registryPort) {
54
this.numApples = numApples;
55
this.registryHost = registryHost;
56
this.registryPort = registryPort;
57
apples = new Apple[numApples];
58
}
59
60
/*
61
* On initialization, export remote objects and register
62
* them with server.
63
*/
64
@Override
65
public void run() {
66
try {
67
int i = 0;
68
69
/*
70
* Locate apple user object in registry. The lookup will occur
71
* every 5 seconds until it is successful or timeout 50 seconds.
72
* These repeated attempts allow the ApplicationServer
73
* to be started before the AppleUserImpl.
74
*/
75
Exception exc = null;
76
long stopTime = System.currentTimeMillis() + LOOKUP_ATTEMPTS * 10000;
77
while (System.currentTimeMillis() < stopTime) {
78
try {
79
Registry registry = LocateRegistry.getRegistry(
80
registryHost, registryPort);
81
user = (AppleUser) registry.lookup("AppleUser");
82
user.startTest();
83
break; //successfully obtained AppleUser
84
} catch (RemoteException | NotBoundException e) {
85
exc = e;
86
Thread.sleep(5000); //sleep 5 seconds and try again
87
}
88
}
89
if (user == null) {
90
logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc);
91
return;
92
}
93
94
/*
95
* Create and export apple implementations.
96
*/
97
try {
98
for (i = 0; i < numApples; i++) {
99
apples[i] = new AppleImpl("AppleImpl #" + (i + 1));
100
}
101
} catch (RemoteException e) {
102
logger.log(Level.SEVERE,
103
"Failed to create AppleImpl #" + (i + 1) + ":", e);
104
user.reportException(e);
105
return;
106
}
107
108
/*
109
* Hand apple objects to apple user.
110
*/
111
try {
112
for (i = 0; i < numApples; i++) {
113
user.useApple(apples[i]);
114
}
115
} catch (RemoteException e) {
116
logger.log(Level.SEVERE,
117
"Failed to register callbacks for " + apples[i] + ":", e);
118
user.reportException(e);
119
}
120
} catch (InterruptedException | RemoteException e) {
121
logger.log(Level.SEVERE, "Unexpected exception:", e);
122
}
123
}
124
125
private static void usage() {
126
System.err.println("Usage: ApplicationServer [-numApples <numApples>]");
127
System.err.println(" [-registryHost <host>]");
128
System.err.println(" -registryPort <port>");
129
System.err.println(" numApples The number of apples (threads) to use.");
130
System.err.println(" The default is 10 apples.");
131
System.err.println(" host The host running rmiregistry " +
132
"which contains AppleUser.");
133
System.err.println(" The default is \"localhost\".");
134
System.err.println(" port The port the rmiregistry is running" +
135
"on.");
136
System.err.println();
137
}
138
139
public static void main(String[] args) {
140
int num = DEFAULT_NUMAPPLES;
141
int port = -1;
142
String host = DEFAULT_REGISTRYHOST;
143
144
// parse command line args
145
try {
146
for (int i = 0; i < args.length ; i++ ) {
147
String arg = args[i];
148
switch (arg) {
149
case "-numApples":
150
i++;
151
num = Integer.parseInt(args[i]);
152
break;
153
case "-registryHost":
154
i++;
155
host = args[i];
156
break;
157
case "-registryPort":
158
i++;
159
port = Integer.parseInt(args[i]);
160
break;
161
default:
162
usage();
163
break;
164
}
165
}
166
167
if (port == -1) {
168
usage();
169
throw new RuntimeException("Port must be specified.");
170
}
171
} catch (Throwable t) {
172
usage();
173
throw new RuntimeException("TEST FAILED: Bad argument");
174
}
175
176
// start the client server
177
Thread server = new Thread(new ApplicationServer(num,host,port));
178
server.start();
179
// main should exit once all exported remote objects are gc'd
180
}
181
}
182
183