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