Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/Activatable/restartLatecomer/RestartLatecomer.java
38829 views
/*1* Copyright (c) 2002, 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 452651425* @summary rmid does not handle group restart for latecomer objects26* @author Ann Wollrath27*28* @library ../../../testlibrary29* @build TestLibrary RMID ActivationLibrary30* RestartLatecomer RestartLatecomer_Stub31* @run main/othervm/policy=security.policy/timeout=240 RestartLatecomer32*/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 RestartLatecomer43implements ActivateMe, Runnable44{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 RestartLatecomer(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}7071/*72* Call back object in the test VM to notify it that73* this object has been activated or restarted.74*/75obj.callback(responder);76}7778public RestartLatecomer() throws RemoteException {79UnicastRemoteObject.exportObject(this, 0);80}8182private void waitFor(String responder) throws Exception {83synchronized (lock) {84for (int i = 0; i < 15; i++) {85if (responders.contains(responder) != true) {86lock.wait(5000);87if (responders.contains(responder) == true) {88return;89}90} else {91return;92}93}94}9596throw new RuntimeException(97"TEST FAILED: service not restarted by timeout");98}99100private void clearResponders() {101synchronized (lock) {102responders.clear();103}104}105106107/**108* Notifies the receiver that the object denoted by "responder"109* has activated or restarted.110*/111public void callback(String responder) {112System.err.println(113"RestartLatecomer: received callback from " + responder);114/*115* Notify waiter that callback has been received and116* test can proceed.117*/118synchronized (lock) {119responders.add(responder);120lock.notifyAll();121}122}123124/**125* Pings object (to activate it).126*/127public void ping() {128System.err.println("RestartLatecomer: recevied ping");129}130131/**132* Spawns a thread to deactivate the object.133*/134public void shutdown() {135System.err.println("RestartLatecomer: received shutdown request");136(new Thread(this,"RestartLatecomer")).start();137}138139public ActivationID getID() {140return id;141}142143/**144* Thread to deactivate object. First attempts to make object145* inactive (via the inactive method). If that fails (the146* object may still have pending/executing calls), then147* unexport the object forcibly.148*/149public void run() {150System.exit(0);151}152153public static void main(String[] args) {154155System.out.println("\nRegression test for bug 4526514\n");156157TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");158159RMID rmid = null;160RestartLatecomer callbackObj = null;161162try {163RMID.removeLog();164rmid = RMID.createRMID();165rmid.start();166167/* Cause activation groups to have a security policy that will168* allow security managers to be downloaded and installed169*/170Properties p = new Properties();171// this test must always set policies/managers in its172// activation groups173p.put("java.security.policy",174TestParams.defaultGroupPolicy);175p.put("java.security.manager",176TestParams.defaultSecurityManager);177178/*179* Create unicast object to be contacted when service is activated.180*/181callbackObj = new RestartLatecomer();182/*183* Create and register descriptors for a restartable and184* non-restartable service (respectively) in a group other than185* this VM's group.186*/187System.err.println("Creating descriptors");188189Object[] stuff = new Object[] { RESTARTABLE, callbackObj };190MarshalledObject restartMobj = new MarshalledObject(stuff);191ActivationGroupDesc groupDesc =192new ActivationGroupDesc(p, null);193194stuff[0] = ACTIVATABLE;195MarshalledObject activateMobj = new MarshalledObject(stuff);196ActivationGroupID groupID =197ActivationGroup.getSystem().registerGroup(groupDesc);198199ActivationDesc activatableDesc =200new ActivationDesc(groupID, "RestartLatecomer", null,201activateMobj, false);202ActivationDesc restartableDesc =203new ActivationDesc(groupID, "RestartLatecomer", null,204restartMobj, true);205206207System.err.println("Register activatable object's descriptor");208ActivateMe activatableObj =209(ActivateMe) Activatable.register(activatableDesc);210211System.err.println("Activate object (starts group VM)");212activatableObj.ping();213214callbackObj.waitFor(ACTIVATABLE);215callbackObj.clearResponders();216System.err.println("Callback from activatable object received");217218System.err.println("Register restartable object's descriptor");219ActivateMe restartableObj =220(ActivateMe) Activatable.register(restartableDesc);221222System.err.println("Shutdown object (exits group VM)");223try {224activatableObj.shutdown();225} catch (RemoteException ignore) {226/*227* Since the shutdown method spawns a thread to call228* System.exit, the group's VM may exit, closing all229* connections, before this call had returned. If that230* happens, then a RemoteException will be caught231* here.232*/233}234235System.err.println("Pause for shutdown to happen...");236Thread.sleep(5000);237238/*239* Wait for "latecomer" restartable service to be240* automatically restarted.241*/242callbackObj.waitFor(RESTARTABLE);243System.err.println(244"TEST PASSED: rmid restarted latecomer service");245246} catch (Exception e) {247TestLibrary.bomb(e);248} finally {249ActivationLibrary.rmidCleanup(rmid);250TestLibrary.unexport(callbackObj);251}252}253254255}256257258interface ActivateMe extends Remote {259public void ping() throws RemoteException;260public void callback(String responder) throws RemoteException;261public void shutdown() throws RemoteException;262}263264265