0N/A/*
5266N/A * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A *
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.rmi.Naming;
0N/Aimport java.rmi.NoSuchObjectException;
0N/Aimport java.rmi.NotBoundException;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.activation.Activatable;
0N/Aimport java.rmi.activation.ActivationID;
0N/Aimport java.rmi.activation.ActivationSystem;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/A
0N/A/**
0N/A * Class of test utility/library methods related to Activatable
0N/A * objects.
0N/A */
0N/Apublic class ActivationLibrary {
0N/A /** time safeDestroy should wait before failing on shutdown rmid */
0N/A private static final int SAFE_WAIT_TIME;
0N/A static {
0N/A int slopFactor = 1;
0N/A try {
0N/A slopFactor = Integer.valueOf(
0N/A TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
0N/A } catch (NumberFormatException ignore) {}
0N/A SAFE_WAIT_TIME = 60000 * slopFactor;
0N/A }
0N/A
0N/A private static final String SYSTEM_NAME =
0N/A ActivationSystem.class.getName();
0N/A
0N/A private static void mesg(Object mesg) {
0N/A System.err.println("ACTIVATION_LIBRARY: " + mesg.toString());
0N/A }
0N/A
0N/A /**
0N/A * Deactivate an activated Activatable
0N/A */
0N/A public static void deactivate(Remote remote,
0N/A ActivationID id) {
5325N/A // We do as much as 50 deactivation trials, each separated by
5325N/A // at least 100 milliseconds sleep time (max sleep time of 5 secs).
5325N/A final long deactivateSleepTime = 100;
5325N/A for (int i = 0; i < 50; i ++) {
0N/A try {
0N/A if (Activatable.inactive(id) == true) {
0N/A mesg("inactive successful");
0N/A return;
0N/A } else {
5325N/A mesg("inactive trial failed. Sleeping " +
5325N/A deactivateSleepTime +
5325N/A " milliseconds before next trial");
5325N/A Thread.sleep(deactivateSleepTime);
0N/A }
0N/A } catch (InterruptedException e) {
5325N/A Thread.currentThread().interrupt();
5325N/A mesg("Thread interrupted while trying to deactivate activatable. Exiting deactivation");
5325N/A return;
0N/A } catch (Exception e) {
0N/A try {
0N/A // forcibly unexport the object
5325N/A mesg("Unexpected exception. Have to forcibly unexport the object." +
5325N/A " Exception was :");
5325N/A e.printStackTrace();
0N/A Activatable.unexportObject(remote, true);
0N/A } catch (NoSuchObjectException ex) {
0N/A }
0N/A return;
0N/A }
0N/A }
0N/A
0N/A mesg("unable to inactivate after several attempts");
0N/A mesg("unexporting object forcibly instead");
0N/A
0N/A try {
0N/A Activatable.unexportObject(remote, true);
0N/A } catch (NoSuchObjectException e) {
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Simple method call to see if rmid is running.
0N/A *
0N/A * This method intentionally avoids performing a lookup on the
0N/A * activation system.
0N/A */
0N/A public static boolean rmidRunning(int port) {
5325N/A int allowedNotReady = 50;
0N/A int connectionRefusedExceptions = 0;
0N/A
5325N/A /* We wait as much as a total of 7.5 secs trying to see Rmid running.
5325N/A * We do this by pausing steps of 100 milliseconds (so up to 75 steps),
5325N/A * right after trying to lookup and find RMID running in the other vm.
5325N/A */
5325N/A final long rmidWaitingStepTime = 100;
5325N/A for (int i = 0; i <= 74; i++) {
0N/A
0N/A try {
0N/A LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
5325N/A mesg("Activation System available after " +
5325N/A (i * rmidWaitingStepTime) + " milliseconds");
0N/A return true;
0N/A
0N/A } catch (java.rmi.ConnectException e) {
5325N/A mesg("Remote connection refused after " +
5325N/A (i * rmidWaitingStepTime) + " milliseconds");
5325N/A
0N/A // ignore connect exceptions until we decide rmid is not up
0N/A if ((connectionRefusedExceptions ++) >= allowedNotReady) {
0N/A return false;
0N/A }
0N/A
5325N/A } catch (java.rmi.NoSuchObjectException nsoe) {
5325N/A /* Activation System still unavailable.
5325N/A * Ignore this since we are just waiting for its availibility.
5325N/A * Just signal unavailibility.
5325N/A */
5325N/A mesg("Activation System still unavailable after more than " +
5325N/A (i * rmidWaitingStepTime) + " milliseconds");
5325N/A
0N/A } catch (NotBoundException e) {
0N/A return false;
0N/A
0N/A } catch (Exception e) {
5325N/A /* print out other types of exceptions as an FYI.
5325N/A * test should not fail as rmid is likely to be in an
5325N/A * undetermined state at this point.
5325N/A */
0N/A mesg("caught an exception trying to" +
0N/A " start rmid, last exception was: " +
0N/A e.getMessage());
0N/A e.printStackTrace();
0N/A }
5325N/A
5325N/A // Waiting for another 100 milliseconds.
5325N/A try {
5325N/A Thread.sleep(100);
5325N/A } catch (InterruptedException e) {
5325N/A Thread.currentThread().interrupt();
5325N/A mesg("Thread interrupted while checking if Activation System is running. Exiting check");
5325N/A return false;
5325N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /** cleanup after rmid */
0N/A public static void rmidCleanup(RMID rmid) {
0N/A if (rmid != null) {
5266N/A if (!ActivationLibrary.safeDestroy(rmid, SAFE_WAIT_TIME)) {
0N/A TestLibrary.bomb("rmid not destroyed in: " +
0N/A SAFE_WAIT_TIME +
0N/A " milliseconds");
0N/A }
0N/A }
0N/A RMID.removeLog();
0N/A }
0N/A
0N/A /**
0N/A * Invoke shutdown on rmid in a way that will not cause a test
0N/A * to hang.
0N/A *
0N/A * @return whether or not shutdown completed succesfully in the
0N/A * timeAllowed
0N/A */
5266N/A private static boolean safeDestroy(RMID rmid, long timeAllowed) {
5266N/A DestroyThread destroyThread = new DestroyThread(rmid);
0N/A destroyThread.start();
0N/A
0N/A try {
0N/A destroyThread.join(timeAllowed);
0N/A } catch (InterruptedException ie) {
0N/A Thread.currentThread().interrupt();
0N/A }
0N/A
0N/A return destroyThread.shutdownSucceeded();
0N/A }
0N/A
0N/A /**
0N/A * Thread class to handle the destruction of rmid
0N/A */
0N/A private static class DestroyThread extends Thread {
0N/A private final RMID rmid;
0N/A private final int port;
0N/A private boolean succeeded = false;
0N/A
5266N/A DestroyThread(RMID rmid) {
0N/A this.rmid = rmid;
5266N/A this.port = rmid.getPort();
0N/A this.setDaemon(true);
0N/A }
0N/A
0N/A public void run() {
0N/A if (ActivationLibrary.rmidRunning(port)) {
0N/A rmid.destroy();
0N/A synchronized (this) {
0N/A // flag that the test was able to shutdown rmid
0N/A succeeded = true;
0N/A }
0N/A mesg("finished destroying rmid");
0N/A } else {
0N/A mesg("tried to shutdown when rmid was not running");
0N/A }
0N/A }
0N/A
0N/A public synchronized boolean shutdownSucceeded() {
0N/A return succeeded;
0N/A }
0N/A }
0N/A}