Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/rmi/testlibrary/ActivationLibrary.java
83402 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/**24*25*/2627import java.io.File;28import java.rmi.Naming;29import java.rmi.NoSuchObjectException;30import java.rmi.NotBoundException;31import java.rmi.Remote;32import java.rmi.activation.Activatable;33import java.rmi.activation.ActivationID;34import java.rmi.activation.ActivationSystem;35import java.rmi.registry.LocateRegistry;3637/**38* Class of test utility/library methods related to Activatable39* objects.40*/41public class ActivationLibrary {42/** time safeDestroy should wait before failing on shutdown rmid */43private static final int SAFE_WAIT_TIME;44static {45int slopFactor = 1;46try {47slopFactor = Integer.valueOf(48TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));49} catch (NumberFormatException ignore) {}50SAFE_WAIT_TIME = 60000 * slopFactor;51}5253private static final String SYSTEM_NAME =54ActivationSystem.class.getName();5556private static void mesg(Object mesg) {57System.err.println("ACTIVATION_LIBRARY: " + mesg.toString());58}5960/**61* Deactivate an activated Activatable62*/63public static void deactivate(Remote remote,64ActivationID id) {65// We do as much as 50 deactivation trials, each separated by66// at least 100 milliseconds sleep time (max sleep time of 5 secs).67final long deactivateSleepTime = 100;68for (int i = 0; i < 50; i ++) {69try {70if (Activatable.inactive(id) == true) {71mesg("inactive successful");72return;73} else {74mesg("inactive trial failed. Sleeping " +75deactivateSleepTime +76" milliseconds before next trial");77Thread.sleep(deactivateSleepTime);78}79} catch (InterruptedException e) {80Thread.currentThread().interrupt();81mesg("Thread interrupted while trying to deactivate activatable. Exiting deactivation");82return;83} catch (Exception e) {84try {85// forcibly unexport the object86mesg("Unexpected exception. Have to forcibly unexport the object." +87" Exception was :");88e.printStackTrace();89Activatable.unexportObject(remote, true);90} catch (NoSuchObjectException ex) {91}92return;93}94}9596mesg("unable to inactivate after several attempts");97mesg("unexporting object forcibly instead");9899try {100Activatable.unexportObject(remote, true);101} catch (NoSuchObjectException e) {102}103}104105/**106* Simple method call to see if rmid is running.107*108* This method intentionally avoids performing a lookup on the109* activation system.110*/111public static boolean rmidRunning(int port) {112int allowedNotReady = 50;113int connectionRefusedExceptions = 0;114115/* We wait as much as a total of 7.5 secs trying to see Rmid running.116* We do this by pausing steps of 100 milliseconds (so up to 75 steps),117* right after trying to lookup and find RMID running in the other vm.118*/119final long rmidWaitingStepTime = 100;120for (int i = 0; i <= 74; i++) {121122try {123LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);124mesg("Activation System available after " +125(i * rmidWaitingStepTime) + " milliseconds");126return true;127128} catch (java.rmi.ConnectException e) {129mesg("Remote connection refused after " +130(i * rmidWaitingStepTime) + " milliseconds");131132// ignore connect exceptions until we decide rmid is not up133if ((connectionRefusedExceptions ++) >= allowedNotReady) {134return false;135}136137} catch (java.rmi.NoSuchObjectException nsoe) {138/* Activation System still unavailable.139* Ignore this since we are just waiting for its availibility.140* Just signal unavailibility.141*/142mesg("Activation System still unavailable after more than " +143(i * rmidWaitingStepTime) + " milliseconds");144145} catch (NotBoundException e) {146return false;147148} catch (Exception e) {149/* print out other types of exceptions as an FYI.150* test should not fail as rmid is likely to be in an151* undetermined state at this point.152*/153mesg("caught an exception trying to" +154" start rmid, last exception was: " +155e.getMessage());156e.printStackTrace();157}158159// Waiting for another 100 milliseconds.160try {161Thread.sleep(100);162} catch (InterruptedException e) {163Thread.currentThread().interrupt();164mesg("Thread interrupted while checking if Activation System is running. Exiting check");165return false;166}167}168return false;169}170171/** cleanup after rmid */172public static void rmidCleanup(RMID rmid) {173if (rmid != null) {174if (!ActivationLibrary.safeDestroy(rmid, SAFE_WAIT_TIME)) {175TestLibrary.bomb("rmid not destroyed in: " +176SAFE_WAIT_TIME +177" milliseconds");178}179}180RMID.removeLog();181}182183/**184* Invoke shutdown on rmid in a way that will not cause a test185* to hang.186*187* @return whether or not shutdown completed succesfully in the188* timeAllowed189*/190private static boolean safeDestroy(RMID rmid, long timeAllowed) {191DestroyThread destroyThread = new DestroyThread(rmid);192destroyThread.start();193194try {195destroyThread.join(timeAllowed);196} catch (InterruptedException ie) {197Thread.currentThread().interrupt();198}199200return destroyThread.shutdownSucceeded();201}202203/**204* Thread class to handle the destruction of rmid205*/206private static class DestroyThread extends Thread {207private final RMID rmid;208private final int port;209private boolean succeeded = false;210211DestroyThread(RMID rmid) {212this.rmid = rmid;213this.port = rmid.getPort();214this.setDaemon(true);215}216217public void run() {218if (ActivationLibrary.rmidRunning(port)) {219rmid.destroy();220synchronized (this) {221// flag that the test was able to shutdown rmid222succeeded = true;223}224mesg("finished destroying rmid");225} else {226mesg("tried to shutdown when rmid was not running");227}228}229230public synchronized boolean shutdownSucceeded() {231return succeeded;232}233}234}235236237