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