Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/Activatable/restartService/RestartService.java
38918 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 4095165 432115125* @summary synopsis: activator should restart daemon services26* @author Ann Wollrath27*28* @library ../../../testlibrary29* @build TestLibrary RMID ActivationLibrary ActivateMe RestartService_Stub30* @run main/othervm/policy=security.policy/timeout=240 RestartService31*/3233import java.io.*;34import java.rmi.*;35import java.rmi.activation.*;36import java.rmi.server.*;37import java.rmi.registry.*;38import java.util.Vector;39import java.util.Properties;4041public class RestartService42implements ActivateMe, Runnable43{4445private ActivationID id;46private static Object lock = new Object();47private Vector responders = new Vector();4849private static final String RESTARTABLE = "restartable";50private static final String ACTIVATABLE = "activatable";515253public RestartService(ActivationID id, MarshalledObject mobj)54throws ActivationException, RemoteException55{56this.id = id;57Activatable.exportObject(this, id, 0);58ActivateMe obj;59String responder;60try {61Object[] stuff = (Object[]) mobj.get();62responder = (String) stuff[0];63System.err.println(responder + " service started");64obj = (ActivateMe) stuff[1];65} catch (Exception e) {66System.err.println("unable to obtain stub from marshalled object");67return;68}6970obj.ping(responder);71}7273public RestartService() throws RemoteException {74UnicastRemoteObject.exportObject(this, 0);75}7677public void ping(String responder) {78System.err.println("RestartService: received ping from " + responder);79synchronized (lock) {80responders.add(responder);81lock.notify();82}83}8485public boolean receivedPing(String responder) {86return responders.contains(responder);87}8889public ActivateMe getUnicastVersion() throws RemoteException {90return new RestartService();91}9293public ActivationID getID() {94return id;95}9697/**98* Spawns a thread to deactivate the object.99*/100public void shutdown() throws Exception101{102(new Thread(this,"RestartService")).start();103}104105/**106* Thread to deactivate object. First attempts to make object107* inactive (via the inactive method). If that fails (the108* object may still have pending/executing calls), then109* unexport the object forcibly.110*/111public void run() {112113}114115public static void main(String[] args) {116117System.out.println("\nRegression test for bug 4095165\n");118119TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");120121RMID rmid = null;122RestartService unicastObj = null;123124try {125RMID.removeLog();126rmid = RMID.createRMID();127rmid.start();128129/* Cause activation groups to have a security policy that will130* allow security managers to be downloaded and installed131*/132Properties p = new Properties();133// this test must always set policies/managers in its134// activation groups135p.put("java.security.policy",136TestParams.defaultGroupPolicy);137p.put("java.security.manager", "");138139/*140* Create unicast object to be contacted when service is activated.141*/142unicastObj = new RestartService();143/*144* Create and register descriptors for a restartable and145* non-restartable service (respectively) in a group other than146* this VM's group.147*/148System.err.println("Creating descriptors");149150Object[] stuff = new Object[] { RESTARTABLE, unicastObj };151MarshalledObject restartMobj = new MarshalledObject(stuff);152ActivationGroupDesc groupDesc =153new ActivationGroupDesc(p, null);154155stuff[0] = ACTIVATABLE;156MarshalledObject activateMobj = new MarshalledObject(stuff);157ActivationGroupID groupID =158ActivationGroup.getSystem().registerGroup(groupDesc);159ActivationDesc restartableDesc =160new ActivationDesc(groupID, "RestartService", null,161restartMobj, true);162163ActivationDesc activatableDesc =164new ActivationDesc(groupID, "RestartService", null,165activateMobj, false);166167System.err.println("Registering descriptors");168ActivateMe restartableObj =169(ActivateMe) Activatable.register(restartableDesc);170171ActivateMe activatableObj =172(ActivateMe) Activatable.register(activatableDesc);173174/*175* Restart rmid; it should start up the restartable service176*/177rmid.restart();178179/*180* Wait for service to be automatically restarted.181*/182boolean gotPing = false;183for (int i = 0; i < 15; i++) {184synchronized (lock) {185if (unicastObj.receivedPing(RESTARTABLE) != true) {186lock.wait(5000);187if (unicastObj.receivedPing(RESTARTABLE) == true) {188System.err.println("Test1 passed: rmid restarted" +189" service");190gotPing = true;191break;192}193} else {194gotPing = true;195break;196}197}198}199200if (gotPing == false)201TestLibrary.bomb("Test1 failed: service not restarted by timeout", null);202203/*204* Make sure activatable services wasn't automatically restarted.205*/206synchronized (lock) {207if (unicastObj.receivedPing(ACTIVATABLE) != true) {208lock.wait(5000);209if (unicastObj.receivedPing(ACTIVATABLE) != true) {210System.err.println("Test2 passed: rmid did not " +211"restart activatable service");212return;213}214}215216TestLibrary.bomb("Test2 failed: activatable service restarted!", null);217}218219220} catch (Exception e) {221TestLibrary.bomb("test failed", e);222} finally {223ActivationLibrary.rmidCleanup(rmid);224TestLibrary.unexport(unicastObj);225}226}227}228229230