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