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