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