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