1 /*
   2  * Copyright (c) 1999, 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 4183169 8032050
  26  * @summary Minor problem with the way ReliableLog handles IOExceptions.
  27  *
  28  * @author Laird Dornin; code borrowed from Ann Wollrath
  29  *
  30  * @library ../../../testlibrary
  31  * @build TestLibrary RMID
  32  *     TestSecurityManager RegisteringActivatable ShutdownGracefully_Stub
  33  * @run main/othervm/policy=security.policy/timeout=700 ShutdownGracefully
  34  */
  35 
  36 import java.rmi.activation.*;
  37 import java.rmi.*;
  38 import java.util.Properties;
  39 import java.util.concurrent.TimeoutException;
  40 
  41 /**
  42  * The test creates an rmid with a special security manager.  After
  43  * rmid makes two registrations (which is greater than rmid's
  44  * snapshotInterval) the security manager stops allowing rmid to write
  45  * to update and snapshot log files in rmid's log directory.  The Test
  46  * registers an Activatable object twice with different group ids.
  47  * The second registration will cause rmid to have to write to a
  48  * LogFile (it causes a snapshot) and the security manager will not
  49  * allow the file write to happen.  The test makes sure that rmid
  50  * shuts down in a graceful manner without any explicit request to do
  51  * so.  The test will not exit for 400 seconds if rmid does not exit
  52  * (after that time, the test will fail).
  53  */
  54 public class ShutdownGracefully
  55     extends Activatable implements RegisteringActivatable
  56 {
  57     private static RegisteringActivatable registering = null;
  58 
  59     private final static long SHUTDOWN_TIMEOUT = 400 * 1000;
  60 
  61     public static void main(String args[]) {
  62 
  63         RMID rmid = null;
  64 
  65         // Save exception if there is a execption or expected behavior
  66         Exception exception = null;
  67         System.err.println("\nRegression test for bug/rfe 4183169\n");
  68 
  69         try {
  70             TestLibrary.suggestSecurityManager(
  71                 "java.rmi.RMISecurityManager");
  72 
  73             // start an rmid.
  74             RMID.removeLog();
  75             rmid = RMID.createRMID();
  76 
  77             // rmid needs to run with a security manager that
  78             // simulates a log problem; rmid should also snapshot
  79             // quickly.
  80             rmid.addOptions(new String[] {
  81                 "-Djava.security.manager=TestSecurityManager",
  82                 "-Dsun.rmi.activation.snapshotInterval=1"});
  83 
  84             //      rmid.addArguments(new String[] {
  85             //          "-C-Djava.rmi.server.logCalls=true"});
  86 
  87             rmid.start();
  88 
  89             // Ensure that activation groups run with the correct
  90             // security manager.
  91             //
  92             Properties p = new Properties();
  93             p.put("java.security.policy",
  94                   TestParams.defaultGroupPolicy);
  95             p.put("java.security.manager",
  96                   "java.lang.SecurityManager");
  97 
  98             System.err.println("activation group will be created " +
  99                                "in a new VM");
 100             ActivationGroupDesc groupDesc =
 101                 new ActivationGroupDesc(p, null);
 102             ActivationSystem system = ActivationGroup.getSystem();
 103             ActivationGroupID groupID = system.registerGroup(groupDesc);
 104 
 105             System.err.println("registering activatable");
 106             ActivationDesc desc = new ActivationDesc
 107                 (groupID, "ShutdownGracefully", null, null);
 108             registering = (RegisteringActivatable)
 109                 Activatable.register(desc);
 110 
 111             System.err.println("activate and deactivate object " +
 112                                "via method call");
 113             registering.shutdown();
 114 
 115             /*
 116              * the security manager rmid is running with will stop
 117              * rmid from writing to its log files; in 1.2.x this would
 118              * have caused rmid to have thrown a runtime exception and
 119              * continue running in an unstable state.  With the fix
 120              * for 4183169, rmid should shutdown gracefully instead.
 121              */
 122 
 123             /*
 124              * register another activatable with a new group id; rmid
 125              * should not recover from this...  I use two
 126              * registrations to more closely simulate the environment
 127              * in which the bug was found.  In java versions with out
 128              * the appropriate bug fix, rmid would hide a
 129              * NullPointerException in this circumstance.
 130              */
 131             p.put("dummyname", "dummyvalue");
 132             groupDesc = new ActivationGroupDesc(p, null);
 133             ActivationGroupID secondGroupID =
 134                 system.registerGroup(groupDesc);
 135             desc = new ActivationDesc(secondGroupID,
 136                 "ShutdownGracefully", null, null);
 137 
 138             /*
 139              * registration request is expected to be failed. succeeded case
 140              * should be recorded. And raise error after clean up  rmid.
 141              */
 142             try {
 143                 registering = (RegisteringActivatable)
 144                     Activatable.register(desc);
 145                 System.err.println("The registration request succeeded unexpectedly");
 146                 exception = new RuntimeException("The registration request succeeded unexpectedly");
 147             } catch (ActivationException e) {
 148                 System.err.println("received exception from registration " +
 149                                    "call that should have failed...");
 150                 // Need wait rmid process terminates.
 151                 try {
 152                     int exitCode = rmid.waitFor(SHUTDOWN_TIMEOUT);
 153                     System.err.println("RMID has exited gracefully with exitcode:" + exitCode);
 154                     rmid = null;
 155                 } catch (TimeoutException te) {
 156                     System.err.println("RMID process has not exited in given time");
 157                     exception = te;
 158                 }
 159             }
 160         } catch (Exception e) {
 161             System.err.println("Exception thrown:" + e);
 162             exception = e;
 163         } finally {
 164             if (rmid != null)
 165                 rmid.destroy();
 166         }
 167         if (exception != null)
 168             TestLibrary.bomb("\nexception thrown in test: ", exception);
 169     }
 170 
 171     /**
 172      * implementation of RegisteringActivatable
 173      */
 174     public ShutdownGracefully
 175         (ActivationID id, MarshalledObject mo) throws RemoteException
 176     {
 177         // register/export anonymously
 178         super(id, 0);
 179     }
 180 
 181     /**
 182      * Deactivates the object. We need to unexport forcibly because this call
 183      * in-progress on this object, which is the same object that we are trying
 184      * to deactivate.
 185      */
 186     public void shutdown() throws Exception {
 187         Activatable.unexportObject(this, true);
 188         ActivationLibrary.deactivate(this, getID());
 189     }
 190 }