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