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 4110548 8085192
  26  * @summary activate fails if rmid is restarted
  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
  35  *     ActivateMe CheckRegisterInLog_Stub
  36  * @run main/othervm/policy=security.policy/timeout=240 CheckRegisterInLog
  37  */
  38 
  39 import java.io.*;
  40 import java.rmi.*;
  41 import java.rmi.server.*;
  42 import java.rmi.activation.*;
  43 import sun.rmi.server.ActivatableRef;
  44 import java.lang.reflect.*;
  45 import java.util.Properties;
  46 
  47 public class CheckRegisterInLog
  48         extends Activatable
  49         implements ActivateMe, Runnable
  50 {
  51 
  52     public CheckRegisterInLog(ActivationID id, MarshalledObject obj)
  53         throws ActivationException, RemoteException
  54     {
  55         super(id, 0);
  56     }
  57 
  58     public void ping()
  59     {}
  60 
  61     /**
  62      * Spawns a thread to deactivate the object.
  63      */
  64     public void shutdown() throws Exception
  65     {
  66         (new Thread(this,"CheckRegisterInLog")).start();
  67     }
  68 
  69     /**
  70      * Thread to deactivate object. First attempts to make object
  71      * inactive (via the inactive method).  If that fails (the
  72      * object may still have pending/executing calls), then
  73      * unexport the object forcibly.
  74      */
  75     public void run() {
  76         ActivationLibrary.deactivate(this, getID());
  77     }
  78 
  79     public static void main(String[] args)  {
  80         /*
  81          * The following line is required with the JDK 1.2 VM so that the
  82          * VM can exit gracefully when this test completes.  Otherwise, the
  83          * conservative garbage collector will find a handle to the server
  84          * object on the native stack and not clear the weak reference to
  85          * it in the RMI runtime's object table.
  86          */
  87         Object dummy = new Object();
  88         RMID rmid = null;
  89         ActivateMe obj;
  90 
  91         System.out.println("\nRegression test for bug 4110548\n");
  92 
  93         CheckRegisterInLog server;
  94 
  95         try {
  96             TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");
  97 
  98             /*
  99              * Start up activation system daemon "rmid".
 100              */
 101             rmid = RMID.launch();
 102 
 103             /* Cause activation groups to have a security policy that will
 104              * allow security managers to be downloaded and installed
 105              */
 106             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             /*
 115              * Register an activation group and an object
 116              * in that group.
 117              */
 118             System.err.println("Creating group descriptor");
 119             ActivationGroupDesc groupDesc =
 120                 new ActivationGroupDesc(p, null);
 121             System.err.println("Registering group");
 122             ActivationSystem system = ActivationGroup.getSystem();
 123             ActivationGroupID groupID = system.registerGroup(groupDesc);
 124 
 125             System.err.println("Creating descriptor");
 126             ActivationDesc desc =
 127                 new ActivationDesc(groupID, "CheckRegisterInLog",
 128                                    null, null);
 129             System.err.println("Registering descriptor");
 130             obj = (ActivateMe)Activatable.register(desc);
 131 
 132             /*
 133              * Restart rmid to force it to read the log file
 134              */
 135             rmid.restart();
 136 
 137 
 138             /*
 139              * 4212096: Give rmid time to go away - we want to make
 140              * sure that an attempt to activate the test object is not made
 141              * on the ActivationSystem that is about to be shutdown.
 142              */
 143             try {
 144                 Thread.sleep(5000);
 145             } catch (InterruptedException ie) {
 146             }
 147 
 148             /*
 149              * Activate the object via a method call.
 150              */
 151             System.err.println("Activate the object via method call");
 152             obj.ping();
 153 
 154             /*
 155              * Clean up object too.
 156              */
 157             System.err.println("Deactivate object via method call");
 158             obj.shutdown();
 159 
 160             System.err.println("\nsuccess: CheckRegisterInLog test passed ");
 161 
 162         } catch (Exception e) {
 163             System.err.println("\nfailure: unexpected exception " +
 164                                e.getClass().getName() + ": " + e.getMessage());
 165             e.printStackTrace(System.err);
 166             throw new RuntimeException("CheckRegisterInLog got exception " +
 167                                        e.getMessage());
 168         } finally {
 169             rmid.cleanup();
 170         }
 171     }
 172 }