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 4138056
  26  * @summary synopsis: Activating objects from an Activatable constructor causes deadlock
  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  *          java.base/sun.nio.ch
  35  * @build TestLibrary RMID ActivationLibrary ActivateMe NestedActivate_Stub
  36  * @run main/othervm/policy=security.policy/timeout=240 NestedActivate
  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.Properties;
  45 
  46 public class NestedActivate
  47         extends Activatable
  48         implements ActivateMe, Runnable
  49 {
  50 
  51     private static Exception exception = null;
  52     private static boolean done = false;
  53     private ActivateMe obj = null;
  54 
  55     public NestedActivate(ActivationID id, MarshalledObject mobj)
  56         throws Exception
  57     {
  58         super(id, 0);
  59         System.err.println("NestedActivate<>: activating object");
  60         if (mobj != null) {
  61             System.err.println("NestedActivate<>: ping obj to activate");
  62             obj = (ActivateMe) mobj.get();
  63             obj.ping();
  64             System.err.println("NestedActivate<>: ping completed");
  65         }
  66     }
  67 
  68     public void ping()
  69     {}
  70 
  71     public void unregister() throws Exception {
  72         super.unregister(super.getID());
  73     }
  74 
  75     /**
  76      * Spawns a thread to deactivate the object.
  77      */
  78     public void shutdown() throws Exception
  79     {
  80         (new Thread(this,"NestedActivate")).start();
  81         if (obj != null)
  82             obj.shutdown();
  83     }
  84 
  85     /**
  86      * Thread to deactivate object. First attempts to make object
  87      * inactive (via the inactive method).  If that fails (the
  88      * object may still have pending/executing calls), then
  89      * unexport the object forcibly.
  90      */
  91     public void run() {
  92         ActivationLibrary.deactivate(this, getID());
  93     }
  94 
  95     public static void main(String[] args) {
  96 
  97         System.err.println("\nRegression test for bug 4138056\n");
  98 
  99         TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
 100 
 101         RMID rmid = null;
 102 
 103         try {
 104             RMID.removeLog();
 105             rmid = RMID.createRMIDOnEphemeralPort();
 106             rmid.start();
 107 
 108             /* Cause activation groups to have a security policy that will
 109              * allow security managers to be downloaded and installed
 110              */
 111             final Properties p = new Properties();
 112             // this test must always set policies/managers in its
 113             // activation groups
 114             p.put("java.security.policy",
 115                   TestParams.defaultGroupPolicy);
 116             p.put("java.security.manager",
 117                   TestParams.defaultSecurityManager);
 118 
 119             Thread t = new Thread() {
 120                 public void run () {
 121                     try {
 122                         System.err.println("Creating group descriptor");
 123                         ActivationGroupDesc groupDesc =
 124                             new ActivationGroupDesc(p, null);
 125                         ActivationGroupID groupID =
 126                             ActivationGroup.getSystem().
 127                             registerGroup(groupDesc);
 128 
 129                         System.err.println("Creating descriptor: object 1");
 130                         ActivationDesc desc1 =
 131                             new ActivationDesc(groupID, "NestedActivate",
 132                                                null, null);
 133 
 134                         System.err.println("Registering descriptor: object 1");
 135                         ActivateMe obj1 =
 136                             (ActivateMe) Activatable.register(desc1);
 137 
 138                         System.err.println("Creating descriptor: object 2");
 139                         ActivationDesc desc2 =
 140                             new ActivationDesc(groupID, "NestedActivate", null,
 141                                                new MarshalledObject(obj1));
 142 
 143                         System.err.println("Registering descriptor: object 2");
 144                         ActivateMe obj2 =
 145                             (ActivateMe) Activatable.register(desc2);
 146 
 147                         System.err.println("Activating object 2");
 148                         obj2.ping();
 149 
 150                         System.err.println("Deactivating objects");
 151                         obj2.shutdown();
 152                     } catch (Exception e) {
 153                         exception = e;
 154                     }
 155                     done = true;
 156                 }
 157             };
 158 
 159             t.start();
 160             t.join(35000);
 161 
 162             if (exception != null) {
 163                 TestLibrary.bomb("test failed", exception);
 164             } else if (!done) {
 165                 TestLibrary.bomb("test failed: not completed before timeout", null);
 166             } else {
 167                 System.err.println("Test passed");
 168             }
 169 
 170         } catch (Exception e) {
 171             TestLibrary.bomb("test failed", e);
 172         } finally {
 173             rmid.cleanup();
 174         }
 175     }
 176 }