Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/testlibrary/RMID.java
47512 views
/*1* Copyright (c) 1998, 2014, 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.*;30import java.rmi.registry.*;3132/**33* Utility class that creates an instance of rmid with a policy34* file of name <code>TestParams.defaultPolicy</code>.35*36* Activation groups should run with the same security manager as the37* test.38*/39public class RMID extends JavaVM {4041private static final String SYSTEM_NAME = ActivationSystem.class.getName();42// "java.rmi.activation.ActivationSystem"4344public static String MANAGER_OPTION="-Djava.security.manager=";4546/** Test port for rmid */47private final int port;4849/** Initial log name */50protected static String log = "log";51/** rmid's logfile directory; currently must be "." */52protected static String LOGDIR = ".";5354private static void mesg(Object mesg) {55System.err.println("RMID: " + mesg.toString());56}5758/** make test options and arguments */59private static String makeOptions(boolean debugExec) {6061String options = " -Dsun.rmi.server.activation.debugExec=" +62debugExec;63// +64//" -Djava.compiler= ";6566// if test params set, want to propagate them67if (!TestParams.testSrc.equals("")) {68options += " -Dtest.src=" + TestParams.testSrc + " ";69}70//if (!TestParams.testClasses.equals("")) {71// options += " -Dtest.classes=" + TestParams.testClasses + " ";72//}73options += " -Dtest.classes=" + TestParams.testClasses //;74+75" -Djava.rmi.server.logLevel=v ";7677// +78// " -Djava.security.debug=all ";7980// Set execTimeout to 60 sec (default is 30 sec)81// to avoid spurious timeouts on slow machines.82options += " -Dsun.rmi.activation.execTimeout=60000";8384return options;85}8687private static String makeArgs(boolean includePortArg, int port) {88String propagateManager = null;8990// rmid will run with a security manager set, but no policy91// file - it should not need one.92if (System.getSecurityManager() == null) {93propagateManager = MANAGER_OPTION +94TestParams.defaultSecurityManager;95} else {96propagateManager = MANAGER_OPTION +97System.getSecurityManager().getClass().getName();98}99100// getAbsolutePath requires permission to read user.dir101String args =102" -log " + (new File(LOGDIR, log)).getAbsolutePath();103104if (includePortArg) {105args += " -port " + port;106}107108// +109// " -C-Djava.compiler= ";110111// if test params set, want to propagate them112if (!TestParams.testSrc.equals("")) {113args += " -C-Dtest.src=" + TestParams.testSrc;114}115if (!TestParams.testClasses.equals("")) {116args += " -C-Dtest.classes=" + TestParams.testClasses;117}118119if (!TestParams.testJavaOpts.equals("")) {120for (String a : TestParams.testJavaOpts.split(" +")) {121args += " -C" + a;122}123}124125if (!TestParams.testVmOpts.equals("")) {126for (String a : TestParams.testVmOpts.split(" +")) {127args += " -C" + a;128}129}130131args += " -C-Djava.rmi.server.useCodebaseOnly=false ";132133args += " " + getCodeCoverageArgs();134return args;135}136137/**138* Routine that creates an rmid that will run with or without a139* policy file.140*/141public static RMID createRMID() {142return createRMID(System.out, System.err, true);143}144145public static RMID createRMID(boolean debugExec) {146return createRMID(System.out, System.err, debugExec);147}148149public static RMID createRMID(OutputStream out, OutputStream err) {150return createRMID(out, err, true);151}152153public static RMID createRMID(OutputStream out, OutputStream err,154boolean debugExec)155{156return createRMID(out, err, debugExec, true,157TestLibrary.getUnusedRandomPort());158}159160public static RMID createRMID(OutputStream out, OutputStream err,161boolean debugExec, boolean includePortArg,162int port)163{164String options = makeOptions(debugExec);165String args = makeArgs(includePortArg, port);166RMID rmid = new RMID("sun.rmi.server.Activation", options, args,167out, err, port);168rmid.setPolicyFile(TestParams.defaultRmidPolicy);169170return rmid;171}172173174/**175* Test RMID should be created with the createRMID method.176*/177protected RMID(String classname, String options, String args,178OutputStream out, OutputStream err, int port)179{180super(classname, options, args, out, err);181this.port = port;182}183184public static void removeLog() {185/*186* Remove previous log file directory before187* starting up rmid.188*/189File f = new File(LOGDIR, log);190191if (f.exists()) {192mesg("removing rmid's old log file...");193String[] files = f.list();194195if (files != null) {196for (int i=0; i<files.length; i++) {197(new File(f, files[i])).delete();198}199}200201if (f.delete() != true) {202mesg("\t" + " unable to delete old log file.");203}204}205}206207/**208* This method is used for adding arguments to rmid (not its VM)209* for passing as VM options to its child group VMs.210* Returns the extra command line arguments required211* to turn on jcov code coverage analysis for rmid child VMs.212*/213protected static String getCodeCoverageArgs() {214return TestLibrary.getExtraProperty("rmid.jcov.args","");215}216217public void start() throws IOException {218start(10000);219}220221public void slowStart() throws IOException {222start(60000);223}224225/**226* Looks up the activation system in the registry on the given port,227* returning its stub, or null if it's not present. This method differs from228* ActivationGroup.getSystem() because this method looks on a specific port229* instead of using the java.rmi.activation.port property like230* ActivationGroup.getSystem() does. This method also returns null instead231* of throwing exceptions.232*/233public static ActivationSystem lookupSystem(int port) {234try {235return (ActivationSystem)LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);236} catch (RemoteException | NotBoundException ex) {237return null;238}239}240241public void start(long waitTime) throws IOException {242243// if rmid is already running, then the test will fail with244// a well recognized exception (port already in use...).245246mesg("starting rmid on port #" + port + "...");247super.start();248249int slopFactor = 1;250try {251slopFactor = Integer.valueOf(252TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));253} catch (NumberFormatException ignore) {}254waitTime = waitTime * slopFactor;255256// We check several times (as many as provides passed waitTime) to257// see if Rmid is currently running. Waiting steps last 100 msecs.258final long rmidStartSleepTime = 100;259do {260// Sleeping for another rmidStartSleepTime time slice.261try {262Thread.sleep(Math.min(waitTime, rmidStartSleepTime));263} catch (InterruptedException ie) {264Thread.currentThread().interrupt();265mesg("Thread interrupted while checking for start of Activation System. Giving up check.");266mesg("Activation System state unknown");267return;268}269waitTime -= rmidStartSleepTime;270271// Checking if rmid is present272if (lookupSystem(port) != null) {273/*274* We need to set the java.rmi.activation.port value as the275* activation system will use the property to determine the276* port #. The activation system will use this value if set.277* If it isn't set, the activation system will set it to an278* incorrect value.279*/280System.setProperty("java.rmi.activation.port", Integer.toString(port));281mesg("finished starting rmid.");282return;283} else {284if (waitTime > 0) {285mesg("rmid not started, will retry for " + waitTime + "ms");286}287}288} while (waitTime > 0);289TestLibrary.bomb("start rmid failed... giving up", null);290}291292public void restart() throws IOException {293destroy();294start();295}296297/**298* Ask rmid to shutdown gracefully using a remote method call.299* catch any errors that might occur from rmid not being present300* at time of shutdown invocation.301*302* Shutdown does not nullify possible references to the rmid303* process object (destroy does though).304*/305public static void shutdown(int port) {306307try {308ActivationSystem system = lookupSystem(port);309310if (system == null) {311TestLibrary.bomb("reference to the activation system was null");312}313314system.shutdown();315} catch (RemoteException re) {316mesg("shutting down the activation daemon failed");317} catch (Exception e) {318mesg("caught exception trying to shutdown rmid");319mesg(e.getMessage());320e.printStackTrace();321}322323mesg("testlibrary finished shutting down rmid");324}325326/**327* Ask rmid to shutdown gracefully but then destroy the rmid328* process if it does not exit by itself. This method only works329* if rmid is a child process of the current VM.330*/331public void destroy() {332// attempt graceful shutdown of the activation system333shutdown(port);334335if (vm != null) {336try {337/* Waiting for distant RMID process to shutdown.338* Waiting is bounded at a hardcoded max of 60 secs (1 min).339* Waiting by steps of 200 msecs, thus at most 300 such attempts340* for termination of distant RMID process. If process is not341* known to be terminated properly after that time,342* we give up for a gracefull termination, and thus go for343* forcibly destroying the process.344*/345boolean vmEnded = false;346int waitingTrials = 0;347final int maxTrials = 300;348final long vmProcessEndWaitInterval = 200;349int vmExitValue;350do {351try {352Thread.sleep(vmProcessEndWaitInterval);353waitingTrials++;354vmExitValue = vm.exitValue();355mesg("rmid exited on shutdown request");356vmEnded = true;357} catch (IllegalThreadStateException illegal) {358mesg("RMID's process still not terminated after more than " +359(waitingTrials * vmProcessEndWaitInterval) + " milliseconds");360}361}362while (!vmEnded &&363(waitingTrials < maxTrials));364365if (waitingTrials >= maxTrials) {366mesg("RMID's process still not terminated after more than " +367(waitingTrials * vmProcessEndWaitInterval) + " milliseconds." +368"Givinp up gracefull termination...");369mesg("destroying RMID's process using Process.destroy()");370super.destroy();371}372373} catch (InterruptedException ie) {374Thread.currentThread().interrupt();375mesg("Thread interrupted while checking for termination of distant rmid vm. Giving up check.");376} catch (Exception e) {377mesg("caught unexpected exception trying to destroy rmid: " +378e.getMessage());379e.printStackTrace();380}381382// rmid will not restart if its process is not null383vm = null;384}385}386387public int getPort() {return port;}388}389390391