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  * @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 ActivateMe RestartService_Stub
  35  * @run main/othervm/policy=security.policy/timeout=240 RestartService
  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 RestartService
  47         implements ActivateMe, Runnable
  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 RestartService(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 RestartService() throws RemoteException {
  79         UnicastRemoteObject.exportObject(this, 0);
  80     }
  81 
  82     public void ping(String responder) {
  83         System.err.println("RestartService: 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 ActivateMe getUnicastVersion() throws RemoteException {
  95         return new RestartService();
  96     }
  97 
  98     public ActivationID getID() {
  99         return id;
 100     }
 101 
 102     /**
 103      * Spawns a thread to deactivate the object.
 104      */
 105     public void shutdown() throws Exception
 106     {
 107         (new Thread(this,"RestartService")).start();
 108     }
 109 
 110     /**
 111      * Thread to deactivate object. First attempts to make object
 112      * inactive (via the inactive method).  If that fails (the
 113      * object may still have pending/executing calls), then
 114      * unexport the object forcibly.
 115      */
 116     public void run() {
 117 
 118     }
 119 
 120     public static void main(String[] args) {
 121 
 122         System.out.println("\nRegression test for bug 4095165\n");
 123 
 124         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
 125 
 126         RMID rmid = null;
 127         RestartService unicastObj = null;
 128 
 129         try {
 130             RMID.removeLog();
 131             rmid = RMID.createRMID();
 132             rmid.start();
 133 
 134             /* Cause activation groups to have a security policy that will
 135              * allow security managers to be downloaded and installed
 136              */
 137             Properties p = new Properties();
 138             // this test must always set policies/managers in its
 139             // activation groups
 140             p.put("java.security.policy",
 141                   TestParams.defaultGroupPolicy);
 142             p.put("java.security.manager",  "");
 143 
 144             /*
 145              * Create unicast object to be contacted when service is activated.
 146              */
 147             unicastObj = new RestartService();
 148             /*
 149              * Create and register descriptors for a restartable and
 150              * non-restartable service (respectively) in a group other than
 151              * this VM's group.
 152              */
 153             System.err.println("Creating descriptors");
 154 
 155             Object[] stuff = new Object[] { RESTARTABLE, unicastObj };
 156             MarshalledObject restartMobj = new MarshalledObject(stuff);
 157             ActivationGroupDesc groupDesc =
 158                 new ActivationGroupDesc(p, null);
 159 
 160             stuff[0] = ACTIVATABLE;
 161             MarshalledObject activateMobj = new MarshalledObject(stuff);
 162             ActivationGroupID groupID =
 163                 ActivationGroup.getSystem().registerGroup(groupDesc);
 164             ActivationDesc restartableDesc =
 165                 new ActivationDesc(groupID, "RestartService", null,
 166                                    restartMobj, true);
 167 
 168             ActivationDesc activatableDesc =
 169                 new ActivationDesc(groupID, "RestartService", null,
 170                                    activateMobj, false);
 171 
 172             System.err.println("Registering descriptors");
 173             ActivateMe restartableObj =
 174                 (ActivateMe) Activatable.register(restartableDesc);
 175 
 176             ActivateMe activatableObj =
 177                 (ActivateMe) Activatable.register(activatableDesc);
 178 
 179             /*
 180              * Restart rmid; it should start up the restartable service
 181              */
 182             rmid.restart();
 183 
 184             /*
 185              * Wait for service to be automatically restarted.
 186              */
 187             boolean gotPing = false;
 188             for (int i = 0; i < 15; i++) {
 189                 synchronized (lock) {
 190                     if (unicastObj.receivedPing(RESTARTABLE) != true) {
 191                         lock.wait(5000);
 192                         if (unicastObj.receivedPing(RESTARTABLE) == true) {
 193                             System.err.println("Test1 passed: rmid restarted" +
 194                                                " service");
 195                             gotPing = true;
 196                             break;
 197                         }
 198                     } else {
 199                         gotPing = true;
 200                         break;
 201                     }
 202                 }
 203             }
 204 
 205             if (gotPing == false)
 206                 TestLibrary.bomb("Test1 failed: service not restarted by timeout", null);
 207 
 208             /*
 209              * Make sure activatable services wasn't automatically restarted.
 210              */
 211             synchronized (lock) {
 212                 if (unicastObj.receivedPing(ACTIVATABLE) != true) {
 213                     lock.wait(5000);
 214                     if (unicastObj.receivedPing(ACTIVATABLE) != true) {
 215                         System.err.println("Test2 passed: rmid did not " +
 216                                            "restart activatable service");
 217                         return;
 218                     }
 219                 }
 220 
 221                 TestLibrary.bomb("Test2 failed: activatable service restarted!", null);
 222             }
 223 
 224 
 225         } catch (Exception e) {
 226             TestLibrary.bomb("test failed", e);
 227         } finally {
 228             rmid.cleanup();
 229             TestLibrary.unexport(unicastObj);
 230         }
 231     }
 232 }