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