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 4116082 8085192
  26  *
  27  * @summary synopsis: rmid should not destroy group when it reports
  28  * inactiveGroup
  29  * @author Ann Wollrath
  30  *
  31  * @library ../../../testlibrary
  32  * @modules java.rmi/sun.rmi.registry
  33  *          java.rmi/sun.rmi.server
  34  *          java.rmi/sun.rmi.transport
  35  *          java.rmi/sun.rmi.transport.tcp
  36  * @build TestLibrary RMID ActivationLibrary ActivateMe InactiveGroup_Stub
  37  * @run main/othervm/policy=security.policy/timeout=240 InactiveGroup
  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.Properties;
  46 
  47 public class InactiveGroup
  48         implements ActivateMe, Runnable
  49 {
  50 
  51     private ActivationID id;
  52 
  53     public InactiveGroup(ActivationID id, MarshalledObject obj)
  54         throws ActivationException, RemoteException
  55     {
  56         this.id = id;
  57         Activatable.exportObject(this, id, 0);
  58     }
  59 
  60     public InactiveGroup() throws RemoteException {
  61         UnicastRemoteObject.exportObject(this, 0);
  62     }
  63 
  64     public void ping()
  65     {}
  66 
  67     public ActivateMe getUnicastVersion() throws RemoteException {
  68         return new InactiveGroup();
  69     }
  70 
  71     public ActivationID getID() {
  72         return id;
  73     }
  74 
  75     /**
  76      * Spawns a thread to deactivate the object.
  77      */
  78     public void shutdown() throws Exception
  79     {
  80         (new Thread(this,"InactiveGroup")).start();
  81     }
  82 
  83     /**
  84      * Thread to deactivate object. First attempts to make object
  85      * inactive (via the inactive method).  If that fails (the
  86      * object may still have pending/executing calls), then
  87      * unexport the object forcibly.
  88      */
  89     public void run()
  90     {
  91         ActivationLibrary.deactivate(this, getID());
  92     }
  93 
  94     public static void main(String[] args) {
  95 
  96         System.out.println("\nRegression test for bug 4116082\n");
  97 
  98         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  99 
 100         RMID rmid = null;
 101 
 102         try {
 103             rmid = RMID.launch();


 104 
 105             /* Cause activation groups to have a security policy that will
 106              * allow security managers to be downloaded and installed
 107              */
 108             Properties p = new Properties();
 109             // this test must always set policies/managers in its
 110             // activation groups
 111             p.put("java.security.policy",
 112                   TestParams.defaultGroupPolicy);
 113             p.put("java.security.manager",
 114                   TestParams.defaultSecurityManager);
 115 
 116             /*
 117              * Create descriptor and activate object in a separate VM.
 118              */
 119             System.err.println("Creating descriptor");
 120             ActivationGroupDesc groupDesc =
 121                 new ActivationGroupDesc(p, null);
 122             ActivationGroupID groupID =
 123                 ActivationGroup.getSystem().registerGroup(groupDesc);
 124             ActivationDesc desc =
 125                 new ActivationDesc(groupID, "InactiveGroup", null, null);
 126 
 127             System.err.println("Registering descriptor");
 128             ActivateMe activatableObj = (ActivateMe) Activatable.register(desc);
 129 
 130             System.err.println("Activate object via method call");
 131             activatableObj.ping();
 132 
 133             /*
 134              * Create a unicast object in the activatable object's VM.
 135              */
 136             System.err.println("Obtain unicast object");
 137             ActivateMe unicastObj = activatableObj.getUnicastVersion();
 138 
 139             /*
 140              * Make activatable object (and therefore group) inactive.
 141              */
 142             System.err.println("Make activatable object inactive");
 143             activatableObj.shutdown();
 144 
 145             /*
 146              * Ping the unicast object a few times to make sure that the
 147              * activation group's process hasn't gone away.
 148              */
 149             System.err.println("Ping unicast object for existence");
 150             // set timeout 5 seconds
 151             final long stopTime = System.currentTimeMillis() + 5000;
 152             while (System.currentTimeMillis() < stopTime) {
 153                 unicastObj.ping();
 154                 Thread.sleep(500);
 155             }
 156 
 157             /*
 158              * Now, reactivate the activatable object; the unicast object
 159              * should no longer be accessible, since reactivating the
 160              * activatable object should kill the previous group's VM
 161              * and the unicast object along with it.
 162              */
 163             System.err.println("Reactivate activatable obj");
 164             activatableObj.ping();
 165 
 166             try {
 167                 System.err.println("Ping unicast object again");
 168                 unicastObj.ping();
 169             } catch (Exception thisShouldFail) {
 170                 System.err.println("Test passed: couldn't reach unicast obj: " +
 171                                    thisShouldFail.getMessage());
 172                 return;
 173             }
 174 
 175             TestLibrary.bomb("Test failed: unicast obj accessible after group reactivates",
 176                  null);
 177 
 178         } catch (Exception e) {
 179             TestLibrary.bomb("test failed", e);
 180         } finally {
 181             rmid.cleanup();
 182         }
 183     }
 184 }
--- EOF ---