Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/Activatable/restartCrashedService/RestartCrashedService.java
38829 views
/*1* Copyright (c) 1998, 2012, 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 409516525* @bug 414073626* @summary synopsis: rmid should waitFor restartable objects that crash and restart them27* @author Ann Wollrath28*29* @library ../../../testlibrary30* @build TestLibrary RMID ActivateMe RestartCrashedService_Stub31* @run main/othervm/policy=security.policy/timeout=240 RestartCrashedService32*/3334import java.io.*;35import java.rmi.*;36import java.rmi.activation.*;37import java.rmi.server.*;38import java.rmi.registry.*;39import java.util.Vector;40import java.util.Properties;4142public class RestartCrashedService43implements ActivateMe44{4546private ActivationID id;47private static Object lock = new Object();48private Vector responders = new Vector();4950private static final String RESTARTABLE = "restartable";51private static final String ACTIVATABLE = "activatable";525354public RestartCrashedService(ActivationID id, MarshalledObject mobj)55throws ActivationException, RemoteException56{57this.id = id;58Activatable.exportObject(this, id, 0);59ActivateMe obj;60String responder;61try {62Object[] stuff = (Object[]) mobj.get();63responder = (String) stuff[0];64System.err.println(responder + " service started");65obj = (ActivateMe) stuff[1];66} catch (Exception e) {67System.err.println("unable to obtain stub from marshalled object");68return;69}7071obj.ping(responder);72}7374public RestartCrashedService() throws RemoteException {75UnicastRemoteObject.exportObject(this, 0);76}7778public void ping(String responder) {79System.err.println("RestartCrashedService: received ping from " + responder);80synchronized (lock) {81responders.add(responder);82lock.notify();83}84}8586public boolean receivedPing(String responder) {87return responders.contains(responder);88}8990public void resetResponders() {91responders.clear();92}9394public ActivateMe getUnicastVersion() throws RemoteException {95return new RestartCrashedService();96}9798public void crash() {99System.exit(0);100}101102public ActivationID getID() {103return id;104}105106public static void main(String[] args) {107108System.out.println("\nRegression test for bug 4095165, 4140736\n");109110TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");111112RMID rmid = null;113RestartCrashedService unicastObj = null;114115try {116RMID.removeLog();117rmid = RMID.createRMID();118rmid.start();119120/* Cause activation groups to have a security policy that will121* allow security managers to be downloaded and installed122*/123final Properties p = new Properties();124// this test must always set policies/managers in its125// activation groups126p.put("java.security.policy",127TestParams.defaultGroupPolicy);128p.put("java.security.manager",129TestParams.defaultSecurityManager);130131/*132* Create unicast object to be contacted when service is activated.133*/134unicastObj = new RestartCrashedService();135/*136* Create and register descriptors for a restartable and137* non-restartable service (respectively) in a group other than138* this VM's group.139*/140System.err.println("Creating descriptors");141142Object[] stuff = new Object[] { RESTARTABLE, unicastObj };143MarshalledObject restartMobj = new MarshalledObject(stuff);144ActivationGroupDesc groupDesc =145new ActivationGroupDesc(p, null);146147stuff[0] = ACTIVATABLE;148MarshalledObject activateMobj = new MarshalledObject(stuff);149ActivationGroupID groupID =150ActivationGroup.getSystem().registerGroup(groupDesc);151ActivationDesc restartableDesc =152new ActivationDesc(groupID, "RestartCrashedService", null,153restartMobj, true);154155ActivationDesc activatableDesc =156new ActivationDesc(groupID, "RestartCrashedService", null,157activateMobj, false);158159System.err.println("Registering descriptors");160ActivateMe restartableObj =161(ActivateMe) Activatable.register(restartableDesc);162163ActivateMe activatableObj =164(ActivateMe) Activatable.register(activatableDesc);165166/*167* Restart rmid; it should start up the restartable service168*/169rmid.restart();170171/*172* Wait for service to be automatically restarted.173*/174int repeat = 1;175176do {177178for (int i = 0; i < 15; i++) {179synchronized (lock) {180if (unicastObj.receivedPing(RESTARTABLE) != true) {181lock.wait(5000);182if (unicastObj.receivedPing(RESTARTABLE) == true) {183System.err.println("Test1 passed: rmid " +184"restarted service");185break;186}187} else {188break;189}190}191}192193if (unicastObj.receivedPing(RESTARTABLE) != true)194TestLibrary.bomb("Test1 failed: service not restarted by timeout",195null);196197/*198* Make sure activatable services wasn't automatically199* restarted.200*/201synchronized (lock) {202if (unicastObj.receivedPing(ACTIVATABLE) != true) {203lock.wait(5000);204if (unicastObj.receivedPing(ACTIVATABLE) != true) {205System.err.println("Test2 passed: rmid did not " +206"restart activatable service");207} else {208TestLibrary.bomb("Test2 failed: activatable service restarted",209null);210}211} else {212TestLibrary.bomb("Test2 failed: activatable service restarted!",213null);214}215}216217218if (repeat > 0) {219try {220System.err.println("\nCrash restartable object");221unicastObj.resetResponders();222restartableObj.crash();223} catch (Exception e) {224}225}226227} while (repeat-- > 0);228229230} catch (Exception e) {231TestLibrary.bomb("test failed", e);232} finally {233ActivationLibrary.rmidCleanup(rmid);234TestLibrary.unexport(unicastObj);235}236}237}238239240