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