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