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