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