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 4173960 26 * @summary synopsis: Activatable objects cannot be restarted. 27 * @author Laird Dornin 28 * 29 * @library ../../../testlibrary 30 * @build TestLibrary RMID ActivationLibrary 31 * ActivateMe ForceLogSnapshot_Stub 32 * @run main/othervm/policy=security.policy/timeout=640 ForceLogSnapshot 33 */ 34 35 import java.io.*; 36 import java.rmi.*; 37 import java.rmi.activation.*; 38 import java.rmi.server.*; 39 import java.rmi.registry.*; 40 import java.util.*; 41 42 public class ForceLogSnapshot 43 implements ActivateMe 44 { 45 /** how many activatable remote objects to create to test rmid */ 46 final public static int HOW_MANY = 50; 47 final public static int NUM_GROUPS = 4; 48 /** cause RMID to generate a snapshot every 10th activated object */ 49 final public static int SNAPSHOT_INTERVAL = 10; 50 51 private ActivationID id; 52 private Vector responders = new Vector(); 53 54 private static final String RESTARTABLE = "restartable"; 55 private static final String ACTIVATABLE = "activatable"; 56 57 private static Object lock = new Object(); 58 private static boolean[] restartedObjects = new boolean[HOW_MANY]; 59 private static boolean[] activatedObjects = new boolean[HOW_MANY]; 60 61 public ForceLogSnapshot(ActivationID id, MarshalledObject mobj) 62 throws ActivationException, RemoteException 63 { 64 this.id = id; 65 int intId = 0; 66 67 Activatable.exportObject(this, id, 0); 68 ActivateMe obj; 69 String responder; 70 try { 71 Object[] stuff = (Object[]) mobj.get(); 72 73 intId = ((Integer) stuff[0]).intValue(); 74 responder = (String) stuff[1]; 75 obj = (ActivateMe) stuff[2]; 76 77 System.err.println(responder + " service started"); 78 } catch (Exception e) { 79 System.err.println("unable to obtain stub from marshalled object"); 80 System.err.println(e.getMessage()); 81 e.printStackTrace(); 82 return; 83 } 84 85 obj.ping(intId, responder); 86 } 87 88 public ForceLogSnapshot() throws RemoteException { 89 UnicastRemoteObject.exportObject(this, 0); 90 } 91 92 public void ping(int intId, String responder) { 93 System.err.println("ForceLogSnapshot: received ping from " + 94 responder); 95 if (responder.equals(RESTARTABLE)) { 96 synchronized (lock) { 97 restartedObjects[intId] = true; 98 } 99 } else if (responder.equals(ACTIVATABLE)) { 100 synchronized (lock) { 101 activatedObjects[intId] = true; 102 } 103 } 104 } 105 106 public void crash() { 107 System.exit(0); 108 } 109 110 public ActivationID getID() { 111 return id; 112 } 113 114 public static void main(String[] args) { 115 116 System.out.println("\nRegression test for bug 4173960\n"); 117 118 TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager"); 119 120 RMID rmid = null; 121 ForceLogSnapshot[] unicastObjs = new ForceLogSnapshot[HOW_MANY]; 122 123 try { 124 String option = " -Dsun.rmi.activation.snapshotInterval=" + 125 SNAPSHOT_INTERVAL; 126 127 RMID.removeLog(); 128 rmid = RMID.createRMID(); 129 rmid.addOptions(new String[] {option, "-Djava.compiler="}); 130 rmid.start(); 131 132 /* Cause activation groups to have a security policy that will 133 * allow security managers to be downloaded and installed 134 */ 135 Properties p = new Properties(); 136 // this test must always set policies/managers in its 137 // activation groups 138 p.put("java.security.policy", 139 TestParams.defaultGroupPolicy); 140 p.put("java.security.manager", 141 TestParams.defaultSecurityManager); 142 143 Object[][] stuff = new Object[HOW_MANY][]; 144 MarshalledObject restartMobj = null; 145 ActivationGroupDesc groupDesc = null; 146 MarshalledObject activateMobj = null; 147 ActivationGroupID[] groupIDs = new ActivationGroupID[NUM_GROUPS]; 148 ActivationDesc restartableDesc = null; 149 ActivationDesc activatableDesc = null; 150 ActivateMe[] restartableObj = new ActivateMe[HOW_MANY]; 151 ActivateMe[] activatableObj = new ActivateMe[HOW_MANY]; 152 153 /* 154 * Create unicast object to be contacted when service is activated. 155 */ 156 int group = 0; 157 int groupNo = 0; 158 for (int i = 0 ; i < HOW_MANY ; i ++ ) { 159 160 System.err.println("Creating descriptors and remote objects"); 161 162 unicastObjs[i] = new ForceLogSnapshot(); 163 164 /* 165 * Create and register descriptors for a restartable and 166 * non-restartable service (respectively) in a group other than 167 * this VM's group. 168 */ 169 stuff[i] = new Object[] { new Integer(i), 197 198 System.err.println("Registering descriptors"); 199 restartableObj[i] = 200 (ActivateMe) Activatable.register(restartableDesc); 201 202 activatableObj[i] = 203 (ActivateMe) Activatable.register(activatableDesc); 204 System.err.println("registered activatable #: " + i); 205 206 // start reusing groups if we need to do so. 207 } 208 209 int repeatOnce = 1; 210 do { 211 212 /* 213 * Restart rmid; it should start up the restartable service 214 */ 215 rmid.restart(); 216 217 if (howManyRestarted(restartedObjects, 10) < HOW_MANY) { 218 TestLibrary.bomb("Test1 failed: a service would not " + 219 "restart"); 220 } 221 System.err.println("Test1 passed: rmid " + 222 "all service(s) restarted. Performing next test."); 223 224 /* 225 * Make sure no activatable services were automatically 226 * restarted. 227 */ 228 if (howManyRestarted(activatedObjects, 2) != 0) { 229 TestLibrary.bomb("Test2 failed: activatable service restarted!", 230 null); 231 } 232 System.err.println("Test2 passed: rmid did not " + 233 "restart activatable service(s)"); 234 235 if (repeatOnce > 0) { 236 try { 237 System.err.println("\nCrash restartable object"); 238 for (int i = 0 ; i < HOW_MANY ; i ++) { 239 restartableObj[i].crash(); 240 } 241 } catch (Exception e) { 242 } 243 } 244 245 } while (repeatOnce-- > 0); 246 247 248 } catch (Exception e) { 249 TestLibrary.bomb("test failed", e); 250 } finally { 251 ActivationLibrary.rmidCleanup(rmid); 252 for (int i = 0 ; i < HOW_MANY ; i ++) { 253 TestLibrary.unexport(unicastObjs[i]); 254 } 255 } 256 } 257 258 /** 259 * Check to see how many services have been automatically 260 * restarted. 261 */ 262 private static int howManyRestarted(boolean[] startedObjects, int retries) { 263 int succeeded = 0; 264 int restarted = 0; 265 int atry = 0; 266 267 while ((restarted < HOW_MANY) && (atry < retries)) { 268 restarted = 0; 269 for (int j = 0 ; j < HOW_MANY ; j ++ ) { 270 synchronized(lock) { 271 if (startedObjects[j]) { 272 restarted ++; 273 } 274 } 275 } 276 System.err.println("not all objects restarted, retrying..."); 277 try { 278 Thread.sleep(10000); 279 } catch (InterruptedException ie) { 280 } 281 atry ++; 282 } 283 return restarted; 284 } 285 } | 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 4173960 26 * @summary synopsis: Activatable objects cannot be restarted. 27 * @author Laird Dornin 28 * 29 * @library ../../../testlibrary 30 * @build TestLibrary RMID ActivationLibrary 31 * ActivateMe ForceLogSnapshot_Stub 32 * @run main/othervm/policy=security.policy/timeout=640 ForceLogSnapshot 33 */ 34 35 import java.io.IOException; 36 import java.rmi.*; 37 import java.rmi.activation.*; 38 import java.rmi.server.*; 39 import java.util.*; 40 import java.util.concurrent.CountDownLatch; 41 import java.util.concurrent.TimeUnit; 42 43 public class ForceLogSnapshot 44 implements ActivateMe 45 { 46 /** how many activatable remote objects to create to test rmid */ 47 final public static int HOW_MANY = 50; 48 final public static int NUM_GROUPS = 4; 49 /** cause RMID to generate a snapshot every 10th activated object */ 50 final public static int SNAPSHOT_INTERVAL = 10; 51 52 private ActivationID id; 53 54 private static final String RESTARTABLE = "restartable"; 55 private static final String ACTIVATABLE = "activatable"; 56 57 private static final CountDownLatch restartedLatch = new CountDownLatch(HOW_MANY); 58 private static final CountDownLatch activatedLatch = new CountDownLatch(HOW_MANY); 59 60 public ForceLogSnapshot(ActivationID id, MarshalledObject mobj) 61 throws ActivationException, RemoteException 62 { 63 this.id = id; 64 65 Activatable.exportObject(this, id, 0); 66 ActivateMe obj; 67 String responder; 68 try { 69 Object[] stuff = (Object[]) mobj.get(); 70 71 int intId = ((Integer) stuff[0]).intValue(); 72 responder = (String) stuff[1]; 73 obj = (ActivateMe) stuff[2]; 74 75 System.err.println(responder + " service started"); 76 obj.ping(intId, responder); 77 } catch (IOException | ClassNotFoundException e) { 78 System.err.println("unable to obtain stub from marshalled object"); 79 System.err.println(e.getMessage()); 80 } 81 } 82 83 public ForceLogSnapshot() throws RemoteException { 84 UnicastRemoteObject.exportObject(this, 0); 85 } 86 87 @Override 88 public void ping(int intId, String responder) { 89 System.err.println("ForceLogSnapshot: received ping from " + 90 responder); 91 switch (responder) { 92 case RESTARTABLE: 93 restartedLatch.countDown(); 94 break; 95 case ACTIVATABLE: 96 activatedLatch.countDown(); 97 break; 98 } 99 } 100 101 @Override 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 4173960\n"); 113 114 TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager"); 115 116 RMID rmid = null; 117 ForceLogSnapshot[] unicastObjs = new ForceLogSnapshot[HOW_MANY]; 118 119 try { 120 String option = " -Dsun.rmi.activation.snapshotInterval=" + 121 SNAPSHOT_INTERVAL; 122 123 RMID.removeLog(); 124 rmid = RMID.createRMID(); 125 rmid.addOptions(new String[] {option, "-Djava.compiler="}); 126 rmid.start(); 127 128 /* Cause activation groups to have a security policy that will 129 * allow security managers to be downloaded and installed 130 */ 131 Properties p = new Properties(); 132 // this test must always set policies/managers in its 133 // activation groups 134 p.put("java.security.policy", 135 TestParams.defaultGroupPolicy); 136 p.put("java.security.manager", 137 TestParams.defaultSecurityManager); 138 139 Object[][] stuff = new Object[HOW_MANY][]; 140 MarshalledObject restartMobj; 141 ActivationGroupDesc groupDesc; 142 MarshalledObject activateMobj; 143 ActivationGroupID[] groupIDs = new ActivationGroupID[NUM_GROUPS]; 144 ActivationDesc restartableDesc; 145 ActivationDesc activatableDesc; 146 ActivateMe[] restartableObj = new ActivateMe[HOW_MANY]; 147 ActivateMe[] activatableObj = new ActivateMe[HOW_MANY]; 148 149 /* 150 * Create unicast object to be contacted when service is activated. 151 */ 152 int group = 0; 153 int groupNo = 0; 154 for (int i = 0 ; i < HOW_MANY ; i ++ ) { 155 156 System.err.println("Creating descriptors and remote objects"); 157 158 unicastObjs[i] = new ForceLogSnapshot(); 159 160 /* 161 * Create and register descriptors for a restartable and 162 * non-restartable service (respectively) in a group other than 163 * this VM's group. 164 */ 165 stuff[i] = new Object[] { new Integer(i), 193 194 System.err.println("Registering descriptors"); 195 restartableObj[i] = 196 (ActivateMe) Activatable.register(restartableDesc); 197 198 activatableObj[i] = 199 (ActivateMe) Activatable.register(activatableDesc); 200 System.err.println("registered activatable #: " + i); 201 202 // start reusing groups if we need to do so. 203 } 204 205 int repeatOnce = 1; 206 do { 207 208 /* 209 * Restart rmid; it should start up the restartable service 210 */ 211 rmid.restart(); 212 213 if (waitAllStarted(restartedLatch, 10) != HOW_MANY) { 214 TestLibrary.bomb("Test1 failed: a service would not " + 215 "restart"); 216 } 217 System.err.println("Test1 passed: rmid " + 218 "all service(s) restarted. Performing next test."); 219 220 /* 221 * Make sure no activatable services were automatically 222 * restarted. 223 */ 224 if (waitAllStarted(activatedLatch, 2) != 0) { 225 TestLibrary.bomb("Test2 failed: activatable service restarted!", 226 null); 227 } 228 System.err.println("Test2 passed: rmid did not " + 229 "restart activatable service(s)"); 230 231 if (repeatOnce > 0) { 232 try { 233 System.err.println("\nCrash restartable object"); 234 for (int i = 0 ; i < HOW_MANY ; i ++) { 235 restartableObj[i].crash(); 236 } 237 } catch (Exception e) { 238 } 239 } 240 241 } while (repeatOnce-- > 0); 242 243 244 } catch (IOException | ActivationException e) { 245 TestLibrary.bomb("test failed", e); 246 } finally { 247 ActivationLibrary.rmidCleanup(rmid); 248 for (int i = 0 ; i < HOW_MANY ; i ++) { 249 TestLibrary.unexport(unicastObjs[i]); 250 } 251 } 252 } 253 254 /** 255 * Check to see if all services have been automatically restarted. 256 * 257 * @waitObjects a CountDownLatch that used to sync all services. 258 * @retries retries times 259 * @return with retry retries times, how many services has been started 260 */ 261 private static int waitAllStarted(CountDownLatch waitObjects, int retries) { 262 int atry = 0; 263 264 while (atry++ < retries) { 265 try { 266 if(waitObjects.await(10, TimeUnit.SECONDS)) 267 return HOW_MANY; 268 } catch (InterruptedException ex) { 269 throw new RuntimeException(ex); 270 } 271 } 272 return HOW_MANY - (int)waitObjects.getCount(); 273 } 274 } |