blob: 9db715360def81c3d2cf7d14e63ab7954a096bd7 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2001 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
24/* @test
25 * @bug 4095165
26 * @bug 4140736
27 * @summary synopsis: rmid should waitFor restartable objects that crash and restart them
28 * @author Ann Wollrath
29 *
30 * @library ../../../testlibrary
31 * @build TestLibrary RMID JavaVM StreamPipe
32 * @build ActivateMe
33 * @build RestartCrashedService
34 * @build RestartCrashedService_Stub
35 * @run main/othervm/policy=security.policy/timeout=240 RestartCrashedService
36 */
37
38import java.io.*;
39import java.rmi.*;
40import java.rmi.activation.*;
41import java.rmi.server.*;
42import java.rmi.registry.*;
43import java.util.Vector;
44import java.util.Properties;
45
46public class RestartCrashedService
47 implements ActivateMe
48{
49
50 private ActivationID id;
51 private static Object lock = new Object();
52 private Vector responders = new Vector();
53
54 private static final String RESTARTABLE = "restartable";
55 private static final String ACTIVATABLE = "activatable";
56
57
58 public RestartCrashedService(ActivationID id, MarshalledObject mobj)
59 throws ActivationException, RemoteException
60 {
61 this.id = id;
62 Activatable.exportObject(this, id, 0);
63 ActivateMe obj;
64 String responder;
65 try {
66 Object[] stuff = (Object[]) mobj.get();
67 responder = (String) stuff[0];
68 System.err.println(responder + " service started");
69 obj = (ActivateMe) stuff[1];
70 } catch (Exception e) {
71 System.err.println("unable to obtain stub from marshalled object");
72 return;
73 }
74
75 obj.ping(responder);
76 }
77
78 public RestartCrashedService() throws RemoteException {
79 UnicastRemoteObject.exportObject(this, 0);
80 }
81
82 public void ping(String responder) {
83 System.err.println("RestartCrashedService: received ping from " + responder);
84 synchronized (lock) {
85 responders.add(responder);
86 lock.notify();
87 }
88 }
89
90 public boolean receivedPing(String responder) {
91 return responders.contains(responder);
92 }
93
94 public void resetResponders() {
95 responders.clear();
96 }
97
98 public ActivateMe getUnicastVersion() throws RemoteException {
99 return new RestartCrashedService();
100 }
101
102 public void crash() {
103 System.exit(0);
104 }
105
106 public ActivationID getID() {
107 return id;
108 }
109
110 public static void main(String[] args) {
111
112 System.out.println("\nRegression test for bug 4095165, 4140736\n");
113
114 TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
115
116 RMID rmid = null;
117 RestartCrashedService unicastObj = null;
118
119 try {
120 RMID.removeLog();
121 rmid = RMID.createRMID();
122 rmid.start();
123
124 /* Cause activation groups to have a security policy that will
125 * allow security managers to be downloaded and installed
126 */
127 final Properties p = new Properties();
128 // this test must always set policies/managers in its
129 // activation groups
130 p.put("java.security.policy",
131 TestParams.defaultGroupPolicy);
132 p.put("java.security.manager",
133 TestParams.defaultSecurityManager);
134
135 /*
136 * Create unicast object to be contacted when service is activated.
137 */
138 unicastObj = new RestartCrashedService();
139 /*
140 * Create and register descriptors for a restartable and
141 * non-restartable service (respectively) in a group other than
142 * this VM's group.
143 */
144 System.err.println("Creating descriptors");
145
146 Object[] stuff = new Object[] { RESTARTABLE, unicastObj };
147 MarshalledObject restartMobj = new MarshalledObject(stuff);
148 ActivationGroupDesc groupDesc =
149 new ActivationGroupDesc(p, null);
150
151 stuff[0] = ACTIVATABLE;
152 MarshalledObject activateMobj = new MarshalledObject(stuff);
153 ActivationGroupID groupID =
154 ActivationGroup.getSystem().registerGroup(groupDesc);
155 ActivationDesc restartableDesc =
156 new ActivationDesc(groupID, "RestartCrashedService", null,
157 restartMobj, true);
158
159 ActivationDesc activatableDesc =
160 new ActivationDesc(groupID, "RestartCrashedService", null,
161 activateMobj, false);
162
163 System.err.println("Registering descriptors");
164 ActivateMe restartableObj =
165 (ActivateMe) Activatable.register(restartableDesc);
166
167 ActivateMe activatableObj =
168 (ActivateMe) Activatable.register(activatableDesc);
169
170 /*
171 * Restart rmid; it should start up the restartable service
172 */
173 rmid.restart();
174
175 /*
176 * Wait for service to be automatically restarted.
177 */
178 int repeat = 1;
179
180 do {
181
182 for (int i = 0; i < 15; i++) {
183 synchronized (lock) {
184 if (unicastObj.receivedPing(RESTARTABLE) != true) {
185 lock.wait(5000);
186 if (unicastObj.receivedPing(RESTARTABLE) == true) {
187 System.err.println("Test1 passed: rmid " +
188 "restarted service");
189 break;
190 }
191 } else {
192 break;
193 }
194 }
195 }
196
197 if (unicastObj.receivedPing(RESTARTABLE) != true)
198 TestLibrary.bomb("Test1 failed: service not restarted by timeout",
199 null);
200
201 /*
202 * Make sure activatable services wasn't automatically
203 * restarted.
204 */
205 synchronized (lock) {
206 if (unicastObj.receivedPing(ACTIVATABLE) != true) {
207 lock.wait(5000);
208 if (unicastObj.receivedPing(ACTIVATABLE) != true) {
209 System.err.println("Test2 passed: rmid did not " +
210 "restart activatable service");
211 } else {
212 TestLibrary.bomb("Test2 failed: activatable service restarted",
213 null);
214 }
215 } else {
216 TestLibrary.bomb("Test2 failed: activatable service restarted!",
217 null);
218 }
219 }
220
221
222 if (repeat > 0) {
223 try {
224 System.err.println("\nCrash restartable object");
225 unicastObj.resetResponders();
226 restartableObj.crash();
227 } catch (Exception e) {
228 }
229 }
230
231 } while (repeat-- > 0);
232
233
234 } catch (Exception e) {
235 TestLibrary.bomb("test failed", e);
236 } finally {
237 ActivationLibrary.rmidCleanup(rmid);
238 TestLibrary.unexport(unicastObj);
239 }
240 }
241}