Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/Activatable/shutdownGracefully/ShutdownGracefully.java
38829 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 418316925* @summary Minor problem with the way ReliableLog handles IOExceptions.26*27* @author Laird Dornin; code borrowed from Ann Wollrath28*29* @library ../../../testlibrary30* @build TestLibrary RMID31* TestSecurityManager RegisteringActivatable ShutdownGracefully_Stub32* @run main/othervm/policy=security.policy/timeout=700 ShutdownGracefully33*/3435import java.rmi.activation.*;36import java.rmi.*;37import java.util.Properties;3839/**40* The test creates an rmid with a special security manager. After41* rmid makes two registrations (which is greater than rmid's42* snapshotInterval) the security manager stops allowing rmid to write43* to update and snapshot log files in rmid's log directory. The Test44* registers an Activatable object twice with different group ids.45* The second registration will cause rmid to have to write to a46* LogFile (it causes a snapshot) and the security manager will not47* allow the file write to happen. The test makes sure that rmid48* shuts down in a graceful manner without any explicit request to do49* so. The test will not exit for 400 seconds if rmid does not exit50* (after that time, the test will fail).51*/52public class ShutdownGracefully53extends Activatable implements Runnable, RegisteringActivatable54{55private static RegisteringActivatable registering = null;5657private final static long SHUTDOWN_TIMEOUT = 400 * 1000;5859public static void main(String args[]) {6061RMID rmid = null;6263System.err.println("\nRegression test for bug/rfe 4183169\n");6465try {66TestLibrary.suggestSecurityManager(67"java.rmi.RMISecurityManager");6869// start an rmid.70RMID.removeLog();71rmid = RMID.createRMID();7273// rmid needs to run with a security manager that74// simulates a log problem; rmid should also snapshot75// quickly.76rmid.addOptions(new String[] {77"-Djava.security.manager=TestSecurityManager",78"-Dsun.rmi.activation.snapshotInterval=1"});7980// rmid.addArguments(new String[] {81// "-C-Djava.rmi.server.logCalls=true"});8283rmid.start();8485// Ensure that activation groups run with the correct86// security manager.87//88Properties p = new Properties();89p.put("java.security.policy",90TestParams.defaultGroupPolicy);91p.put("java.security.manager",92"java.lang.SecurityManager");9394System.err.println("activation group will be created " +95"in a new VM");96ActivationGroupDesc groupDesc =97new ActivationGroupDesc(p, null);98ActivationSystem system = ActivationGroup.getSystem();99ActivationGroupID groupID = system.registerGroup(groupDesc);100101System.err.println("registering activatable");102ActivationDesc desc = new ActivationDesc103(groupID, "ShutdownGracefully", null, null);104registering = (RegisteringActivatable)105Activatable.register(desc);106107System.err.println("activate and deactivate object " +108"via method call");109registering.shutdown();110111/*112* the security manager rmid is running with will stop113* rmid from writing to its log files; in 1.2.x this would114* have caused rmid to have thrown a runtime exception and115* continue running in an unstable state. With the fix116* for 4183169, rmid should shutdown gracefully instead.117*/118119/*120* register another activatable with a new group id; rmid121* should not recover from this... I use two122* registrations to more closely simulate the environment123* in which the bug was found. In java versions with out124* the appropriate bug fix, rmid would hide a125* NullPointerException in this circumstance.126*/127p.put("dummyname", "dummyvalue");128groupDesc = new ActivationGroupDesc(p, null);129ActivationGroupID secondGroupID =130system.registerGroup(groupDesc);131desc = new ActivationDesc(secondGroupID,132"ShutdownGracefully", null, null);133134try {135registering = (RegisteringActivatable)136Activatable.register(desc);137138System.err.println("second activate and deactivate " +139"object via method call");140} catch (ActivationException e) {141System.err.println("received exception from registration " +142"call that should have failed...");143}144145/*146* no longer needed because the security manager147* throws an exception during snapshot148*/149/*150try {151registering.shutdown();152153System.err.println("received exception from remote " +154"call that should have failed...");155} catch (RemoteException e) {156}157*/158159} catch (Exception e) {160TestLibrary.bomb("\nfailure: unexpected exception ", e);161} finally {162try {163Thread.sleep(4000);164} catch (InterruptedException e) {165}166167registering = null;168169// Need to make sure that rmid goes away by itself170JavaVM rmidProcess = rmid;171if (rmidProcess != null) {172try {173Runnable waitThread =174new ShutdownDetectThread(rmidProcess);175176synchronized (waitThread) {177(new Thread(waitThread)).start();178waitThread.wait(SHUTDOWN_TIMEOUT);179System.err.println("rmid has shutdown");180181if (!rmidDone) {182// ensure that this rmid does not infect183// other tests.184rmidProcess.destroy();185TestLibrary.bomb("rmid did not shutdown " +186"gracefully in time");187}188}189} catch (Exception e) {190TestLibrary.bomb("exception waiting for rmid " +191"to shut down");192}193}194// else rmid should be down195}196197System.err.println198("\nsuccess: ShutdownGracefully test passed ");199}200201private static boolean rmidDone = false;202203/**204* class that waits for rmid to exit205*/206private static class ShutdownDetectThread implements Runnable {207private JavaVM rmidProcess = null;208209ShutdownDetectThread(JavaVM rmidProcess) {210this.rmidProcess = rmidProcess;211}212public void run() {213System.err.println("waiting for rmid to shutdown");214215try {216rmidProcess.waitFor();217} catch (InterruptedException e) {218// should not happen219}220221synchronized (this) {222// notify parent thread when rmid has exited223this.notify();224rmidDone = true;225}226227RMID.removeLog();228}229}230231/**232* implementation of RegisteringActivatable233*/234public ShutdownGracefully235(ActivationID id, MarshalledObject mo) throws RemoteException236{237// register/export anonymously238super(id, 0);239}240241/**242* Spawns a thread to deactivate the object.243*/244public void shutdown() throws Exception {245(new Thread(this, "ShutdownGracefully")).start();246}247248/**249* Thread to deactivate object. First attempts to make object250* inactive (via the inactive method). If that fails (the251* object may still have pending/executing calls), then252* unexport the object forcibly.253*/254public void run() {255try {256Thread.sleep(50 * 1000);257} catch (InterruptedException e) {258}259ActivationLibrary.deactivate(this, getID());260}261}262263264