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