Skip to content
Permalink
Browse files

8196729: Add jstatd option to specify RMI connector port

Reviewed-by: cjplummer, sspitsyn
  • Loading branch information
Daniil Titov
Daniil Titov committed Feb 6, 2020
1 parent 87031d4 commit 26b642f92fa8f2d4826c4034e5b5827ea09d1889
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -48,7 +48,7 @@
private static RemoteHost remoteHost;

private static void printUsage() {
System.err.println("usage: jstatd [-nr] [-p port] [-n rminame]\n" +
System.err.println("usage: jstatd [-nr] [-p port] [-r rmiport] [-n rminame]\n" +
" jstatd -?|-h|--help");
}

@@ -75,6 +75,7 @@ static void bind(String name, RemoteHost remoteHost)
@SuppressWarnings("deprecation") // Use of RMISecurityManager
public static void main(String[] args) {
String rminame = null;
int rmiPort = 0;
int argc = 0;

for ( ; (argc < args.length) && (args[argc].startsWith("-")); argc++) {
@@ -98,6 +99,17 @@ public static void main(String[] args) {
}
port = Integer.parseInt(args[argc]);
}
} else if (arg.startsWith("-r")) {
if (arg.compareTo("-r") != 0) {
rmiPort = Integer.parseInt(arg.substring(2));
} else {
argc++;
if (argc >= args.length) {
printUsage();
System.exit(1);
}
rmiPort = Integer.parseInt(args[argc]);
}
} else if (arg.startsWith("-n")) {
if (arg.compareTo("-n") != 0) {
rminame = arg.substring(2);
@@ -139,9 +151,9 @@ public static void main(String[] args) {
try {
// use 1.5.0 dynamically generated subs.
System.setProperty("java.rmi.server.ignoreSubClasses", "true");
remoteHost = new RemoteHostImpl();
remoteHost = new RemoteHostImpl(rmiPort);
RemoteHost stub = (RemoteHost) UnicastRemoteObject.exportObject(
remoteHost, 0);
remoteHost, rmiPort);
bind(name.toString(), stub);
System.out.println("jstatd started (bound to " + name.toString() + ")");
System.out.flush();
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -51,8 +51,14 @@
private MonitoredHost monitoredHost;
private Set<Integer> activeVms;
private static RemoteVm rvm;
private final int rmiPort;

public RemoteHostImpl() throws MonitorException {
this(0);
}

public RemoteHostImpl(int rmiPort) throws MonitorException {
this.rmiPort = rmiPort;
try {
monitoredHost = MonitoredHost.getMonitoredHost("localhost");
} catch (URISyntaxException e) { }
@@ -78,7 +84,7 @@ public RemoteVm attachVm(int lvmid, String mode)
VmIdentifier vmid = new VmIdentifier(vmidStr);
MonitoredVm mvm = monitoredHost.getMonitoredVm(vmid);
rvm = new RemoteVmImpl((BufferedMonitoredVm)mvm);
stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, 0);
stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, rmiPort);
}
catch (URISyntaxException e) {
throw new RuntimeException("Malformed VmIdentifier URI: "
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -65,10 +65,13 @@
private static final String JPS_OUTPUT_REGEX = "^\\d+\\s*.*";

private boolean useDefaultPort = true;
private boolean useDefaultRmiPort = true;
private String port;
private String rmiPort;
private String serverName;
private Long jstatdPid;
private boolean withExternalRegistry = false;
private boolean useShortCommandSyntax = false;

public void setServerName(String serverName) {
this.serverName = serverName;
@@ -78,6 +81,10 @@ public void setUseDefaultPort(boolean useDefaultPort) {
this.useDefaultPort = useDefaultPort;
}

public void setUseDefaultRmiPort(boolean useDefaultRmiPort) {
this.useDefaultRmiPort = useDefaultRmiPort;
}

public void setWithExternalRegistry(boolean withExternalRegistry) {
this.withExternalRegistry = withExternalRegistry;
}
@@ -245,6 +252,7 @@ private void cleanUpThread(ProcessThread thread) throws Throwable {
*
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port -r rmiport
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -n serverName
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port -n serverName
*/
@@ -257,22 +265,31 @@ private void cleanUpThread(ProcessThread thread) throws Throwable {
"Security policy " + policy.getAbsolutePath() + " does not exist or not a file");
launcher.addVMArg("-Djava.security.policy=" + policy.getAbsolutePath());
if (port != null) {
launcher.addToolArg("-p");
launcher.addToolArg(port);
addToolArg(launcher,"-p", port);
}
if (rmiPort != null) {
addToolArg(launcher,"-r", rmiPort);
}
if (serverName != null) {
launcher.addToolArg("-n");
launcher.addToolArg(serverName);
addToolArg(launcher,"-n", serverName);
}
if (withExternalRegistry) {
launcher.addToolArg("-nr");
}

String[] cmd = launcher.getCommand();
log("Start jstatd", cmd);
return cmd;
}

private void addToolArg(JDKToolLauncher launcher, String name, String value) {
if (useShortCommandSyntax) {
launcher.addToolArg(name + value);
} else {
launcher.addToolArg(name);
launcher.addToolArg(value);
}
}

private ProcessThread tryToSetupJstatdProcess() throws Throwable {
ProcessThread jstatdThread = new ProcessThread("Jstatd-Thread",
JstatdTest::isJstadReady, getJstatdCmd());
@@ -300,6 +317,12 @@ private static boolean isJstadReady(String line) {
}

public void doTest() throws Throwable {
runTest(false);
runTest(true);
}

private void runTest(boolean useShortSyntax) throws Throwable {
useShortCommandSyntax = useShortSyntax;
if (useDefaultPort) {
verifyNoRmiRegistryOnDefaultPort();
}
@@ -311,6 +334,10 @@ public void doTest() throws Throwable {
port = String.valueOf(Utils.getFreePort());
}

if (!useDefaultRmiPort) {
rmiPort = String.valueOf(Utils.getFreePort());
}

if (withExternalRegistry) {
Registry registry = startRegistry();
if (registry == null) {
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* 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.
*
* 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.
*/

/*
* @test
*
* @library /test/lib
*
* @build JstatdTest JstatGCUtilParser
* @run main/timeout=60 TestJstatdRmiPort
*/
public class TestJstatdRmiPort {

public static void main(String[] args) throws Throwable {
testRmiPort();
testRegistryAndRmiPorts();
}

private static void testRmiPort() throws Throwable {
JstatdTest test = new JstatdTest();
test.setUseDefaultRmiPort(false);
test.doTest();
}

private static void testRegistryAndRmiPorts() throws Throwable {
JstatdTest test = new JstatdTest();
test.setUseDefaultPort(false);
test.setUseDefaultRmiPort(false);
test.doTest();
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -46,7 +46,7 @@ private static void testUsage(String option) throws Exception {
ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);

output.shouldContain("usage: jstatd [-nr] [-p port] [-n rminame]");
output.shouldContain("usage: jstatd [-nr] [-p port] [-r rmiport] [-n rminame]");
output.shouldHaveExitValue(0);
}

0 comments on commit 26b642f

Please sign in to comment.