Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/rmi/testlibrary/RMID.java
83402 views
/*1* Copyright (c) 1998, 2013, 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.*;28import java.rmi.*;29import java.rmi.activation.*;3031/**32* Utility class that creates an instance of rmid with a policy33* file of name <code>TestParams.defaultPolicy</code>.34*35* Activation groups should run with the same security manager as the36* test.37*/38public class RMID extends JavaVM {3940public static String MANAGER_OPTION="-Djava.security.manager=";4142/** Test port for rmid */43private final int port;4445/** Initial log name */46protected static String log = "log";47/** rmid's logfile directory; currently must be "." */48protected static String LOGDIR = ".";4950private static void mesg(Object mesg) {51System.err.println("RMID: " + mesg.toString());52}5354/** make test options and arguments */55private static String makeOptions(boolean debugExec) {5657String options = " -Dsun.rmi.server.activation.debugExec=" +58debugExec;59// +60//" -Djava.compiler= ";6162// if test params set, want to propagate them63if (!TestParams.testSrc.equals("")) {64options += " -Dtest.src=" + TestParams.testSrc + " ";65}66//if (!TestParams.testClasses.equals("")) {67// options += " -Dtest.classes=" + TestParams.testClasses + " ";68//}69options += " -Dtest.classes=" + TestParams.testClasses //;70+71" -Djava.rmi.server.logLevel=v ";7273// +74// " -Djava.security.debug=all ";7576// Set execTimeout to 60 sec (default is 30 sec)77// to avoid spurious timeouts on slow machines.78options += " -Dsun.rmi.activation.execTimeout=60000";7980return options;81}8283private static String makeArgs(boolean includePortArg, int port) {84String propagateManager = null;8586// rmid will run with a security manager set, but no policy87// file - it should not need one.88if (System.getSecurityManager() == null) {89propagateManager = MANAGER_OPTION +90TestParams.defaultSecurityManager;91} else {92propagateManager = MANAGER_OPTION +93System.getSecurityManager().getClass().getName();94}9596// getAbsolutePath requires permission to read user.dir97String args =98" -log " + (new File(LOGDIR, log)).getAbsolutePath();99100if (includePortArg) {101args += " -port " + port;102}103104// +105// " -C-Djava.compiler= ";106107// if test params set, want to propagate them108if (!TestParams.testSrc.equals("")) {109args += " -C-Dtest.src=" + TestParams.testSrc;110}111if (!TestParams.testClasses.equals("")) {112args += " -C-Dtest.classes=" + TestParams.testClasses;113}114115args += " -C-Djava.rmi.server.useCodebaseOnly=false ";116117args += " " + getCodeCoverageArgs();118return args;119}120121/**122* Routine that creates an rmid that will run with or without a123* policy file.124*/125public static RMID createRMID() {126return createRMID(System.out, System.err, true);127}128129public static RMID createRMID(boolean debugExec) {130return createRMID(System.out, System.err, debugExec);131}132133public static RMID createRMID(OutputStream out, OutputStream err) {134return createRMID(out, err, true);135}136137public static RMID createRMID(OutputStream out, OutputStream err,138boolean debugExec)139{140return createRMID(out, err, debugExec, true,141TestLibrary.getUnusedRandomPort());142}143144public static RMID createRMID(OutputStream out, OutputStream err,145boolean debugExec, boolean includePortArg,146int port)147{148String options = makeOptions(debugExec);149String args = makeArgs(includePortArg, port);150RMID rmid = new RMID("sun.rmi.server.Activation", options, args,151out, err, port);152rmid.setPolicyFile(TestParams.defaultRmidPolicy);153154return rmid;155}156157158/**159* Test RMID should be created with the createRMID method.160*/161protected RMID(String classname, String options, String args,162OutputStream out, OutputStream err, int port)163{164super(classname, options, args, out, err);165this.port = port;166}167168public static void removeLog() {169/*170* Remove previous log file directory before171* starting up rmid.172*/173File f = new File(LOGDIR, log);174175if (f.exists()) {176mesg("removing rmid's old log file...");177String[] files = f.list();178179if (files != null) {180for (int i=0; i<files.length; i++) {181(new File(f, files[i])).delete();182}183}184185if (f.delete() != true) {186mesg("\t" + " unable to delete old log file.");187}188}189}190191/**192* This method is used for adding arguments to rmid (not its VM)193* for passing as VM options to its child group VMs.194* Returns the extra command line arguments required195* to turn on jcov code coverage analysis for rmid child VMs.196*/197protected static String getCodeCoverageArgs() {198return TestLibrary.getExtraProperty("rmid.jcov.args","");199}200201public void start() throws IOException {202start(10000);203}204205public void slowStart() throws IOException {206start(60000);207}208209public void start(long waitTime) throws IOException {210211// if rmid is already running, then the test will fail with212// a well recognized exception (port already in use...).213214mesg("starting rmid on port #" + port + "...");215super.start();216217int slopFactor = 1;218try {219slopFactor = Integer.valueOf(220TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));221} catch (NumberFormatException ignore) {}222waitTime = waitTime * slopFactor;223224// We check several times (as many as provides passed waitTime) to225// see if Rmid is currently running. Waiting steps last 100 msecs.226final long rmidStartSleepTime = 100;227do {228// Sleeping for another rmidStartSleepTime time slice.229try {230Thread.sleep(Math.min(waitTime, rmidStartSleepTime));231} catch (InterruptedException ie) {232Thread.currentThread().interrupt();233mesg("Thread interrupted while checking for start of Activation System. Giving up check.");234mesg("Activation System state unknown");235return;236}237waitTime -= rmidStartSleepTime;238239// Checking if rmid is present240if (ActivationLibrary.rmidRunning(port)) {241/**242* We need to set the java.rmi.activation.port value as the243* activation system will use the property to determine the244* port #. The activation system will use this value if set.245* If it isn't set, the activation system will set it to an246* incorrect value.247*/248System.setProperty("java.rmi.activation.port", Integer.toString(port));249mesg("finished starting rmid.");250return;251}252else {253mesg("rmid still not started");254}255256} while (waitTime > 0);257TestLibrary.bomb("start rmid failed... giving up", null);258}259260public void restart() throws IOException {261destroy();262start();263}264265/**266* Ask rmid to shutdown gracefully using a remote method call.267* catch any errors that might occur from rmid not being present268* at time of shutdown invocation.269*270* Shutdown does not nullify possible references to the rmid271* process object (destroy does though).272*/273public static void shutdown(int port) {274275try {276ActivationSystem system = null;277278try {279mesg("getting a reference to the activation system");280system = (ActivationSystem) Naming.lookup("//:" +281port +282"/java.rmi.activation.ActivationSystem");283mesg("obtained a reference to the activation system");284} catch (RemoteException re) {285mesg("could not contact registry while trying to shutdown activation system");286} catch (java.net.MalformedURLException mue) {287}288289if (system == null) {290TestLibrary.bomb("reference to the activation system was null");291}292system.shutdown();293294} catch (RemoteException re) {295mesg("shutting down the activation daemon failed");296} catch (Exception e) {297mesg("caught exception trying to shutdown rmid");298mesg(e.getMessage());299e.printStackTrace();300}301302mesg("testlibrary finished shutting down rmid");303}304305/**306* Ask rmid to shutdown gracefully but then destroy the rmid307* process if it does not exit by itself. This method only works308* if rmid is a child process of the current VM.309*/310public void destroy() {311// attempt graceful shutdown of the activation system312shutdown(port);313314if (vm != null) {315try {316/* Waiting for distant RMID process to shutdown.317* Waiting is bounded at a hardcoded max of 60 secs (1 min).318* Waiting by steps of 200 msecs, thus at most 300 such attempts319* for termination of distant RMID process. If process is not320* known to be terminated properly after that time,321* we give up for a gracefull termination, and thus go for322* forcibly destroying the process.323*/324boolean vmEnded = false;325int waitingTrials = 0;326final int maxTrials = 300;327final long vmProcessEndWaitInterval = 200;328int vmExitValue;329do {330try {331Thread.sleep(vmProcessEndWaitInterval);332waitingTrials++;333vmExitValue = vm.exitValue();334mesg("rmid exited on shutdown request");335vmEnded = true;336} catch (IllegalThreadStateException illegal) {337mesg("RMID's process still not terminated after more than " +338(waitingTrials * vmProcessEndWaitInterval) + " milliseconds");339}340}341while (!vmEnded &&342(waitingTrials < maxTrials));343344if (waitingTrials >= maxTrials) {345mesg("RMID's process still not terminated after more than " +346(waitingTrials * vmProcessEndWaitInterval) + " milliseconds." +347"Givinp up gracefull termination...");348mesg("destroying RMID's process using Process.destroy()");349super.destroy();350}351352} catch (InterruptedException ie) {353Thread.currentThread().interrupt();354mesg("Thread interrupted while checking for termination of distant rmid vm. Giving up check.");355} catch (Exception e) {356mesg("caught unexpected exception trying to destroy rmid: " +357e.getMessage());358e.printStackTrace();359}360361// rmid will not restart if its process is not null362vm = null;363}364}365366public int getPort() {return port;}367}368369370