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