Skip to content

Commit 26b642f

Browse files
author
Daniil Titov
committed
8196729: Add jstatd option to specify RMI connector port
Reviewed-by: cjplummer, sspitsyn
1 parent 87031d4 commit 26b642f

File tree

5 files changed

+110
-14
lines changed

5 files changed

+110
-14
lines changed

src/jdk.jstatd/share/classes/sun/tools/jstatd/Jstatd.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -48,7 +48,7 @@ public class Jstatd {
4848
private static RemoteHost remoteHost;
4949

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

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

8081
for ( ; (argc < args.length) && (args[argc].startsWith("-")); argc++) {
@@ -98,6 +99,17 @@ public static void main(String[] args) {
9899
}
99100
port = Integer.parseInt(args[argc]);
100101
}
102+
} else if (arg.startsWith("-r")) {
103+
if (arg.compareTo("-r") != 0) {
104+
rmiPort = Integer.parseInt(arg.substring(2));
105+
} else {
106+
argc++;
107+
if (argc >= args.length) {
108+
printUsage();
109+
System.exit(1);
110+
}
111+
rmiPort = Integer.parseInt(args[argc]);
112+
}
101113
} else if (arg.startsWith("-n")) {
102114
if (arg.compareTo("-n") != 0) {
103115
rminame = arg.substring(2);
@@ -139,9 +151,9 @@ public static void main(String[] args) {
139151
try {
140152
// use 1.5.0 dynamically generated subs.
141153
System.setProperty("java.rmi.server.ignoreSubClasses", "true");
142-
remoteHost = new RemoteHostImpl();
154+
remoteHost = new RemoteHostImpl(rmiPort);
143155
RemoteHost stub = (RemoteHost) UnicastRemoteObject.exportObject(
144-
remoteHost, 0);
156+
remoteHost, rmiPort);
145157
bind(name.toString(), stub);
146158
System.out.println("jstatd started (bound to " + name.toString() + ")");
147159
System.out.flush();

src/jdk.jstatd/share/classes/sun/tools/jstatd/RemoteHostImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -51,8 +51,14 @@ public class RemoteHostImpl implements RemoteHost, HostListener {
5151
private MonitoredHost monitoredHost;
5252
private Set<Integer> activeVms;
5353
private static RemoteVm rvm;
54+
private final int rmiPort;
5455

5556
public RemoteHostImpl() throws MonitorException {
57+
this(0);
58+
}
59+
60+
public RemoteHostImpl(int rmiPort) throws MonitorException {
61+
this.rmiPort = rmiPort;
5662
try {
5763
monitoredHost = MonitoredHost.getMonitoredHost("localhost");
5864
} catch (URISyntaxException e) { }
@@ -78,7 +84,7 @@ public RemoteVm attachVm(int lvmid, String mode)
7884
VmIdentifier vmid = new VmIdentifier(vmidStr);
7985
MonitoredVm mvm = monitoredHost.getMonitoredVm(vmid);
8086
rvm = new RemoteVmImpl((BufferedMonitoredVm)mvm);
81-
stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, 0);
87+
stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, rmiPort);
8288
}
8389
catch (URISyntaxException e) {
8490
throw new RuntimeException("Malformed VmIdentifier URI: "

test/jdk/sun/tools/jstatd/JstatdTest.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -65,10 +65,13 @@ public final class JstatdTest {
6565
private static final String JPS_OUTPUT_REGEX = "^\\d+\\s*.*";
6666

6767
private boolean useDefaultPort = true;
68+
private boolean useDefaultRmiPort = true;
6869
private String port;
70+
private String rmiPort;
6971
private String serverName;
7072
private Long jstatdPid;
7173
private boolean withExternalRegistry = false;
74+
private boolean useShortCommandSyntax = false;
7275

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

84+
public void setUseDefaultRmiPort(boolean useDefaultRmiPort) {
85+
this.useDefaultRmiPort = useDefaultRmiPort;
86+
}
87+
8188
public void setWithExternalRegistry(boolean withExternalRegistry) {
8289
this.withExternalRegistry = withExternalRegistry;
8390
}
@@ -245,6 +252,7 @@ private void cleanUpThread(ProcessThread thread) throws Throwable {
245252
*
246253
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy
247254
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port
255+
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port -r rmiport
248256
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -n serverName
249257
* jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port -n serverName
250258
*/
@@ -257,22 +265,31 @@ private String[] getJstatdCmd() throws Exception {
257265
"Security policy " + policy.getAbsolutePath() + " does not exist or not a file");
258266
launcher.addVMArg("-Djava.security.policy=" + policy.getAbsolutePath());
259267
if (port != null) {
260-
launcher.addToolArg("-p");
261-
launcher.addToolArg(port);
268+
addToolArg(launcher,"-p", port);
269+
}
270+
if (rmiPort != null) {
271+
addToolArg(launcher,"-r", rmiPort);
262272
}
263273
if (serverName != null) {
264-
launcher.addToolArg("-n");
265-
launcher.addToolArg(serverName);
274+
addToolArg(launcher,"-n", serverName);
266275
}
267276
if (withExternalRegistry) {
268277
launcher.addToolArg("-nr");
269278
}
270-
271279
String[] cmd = launcher.getCommand();
272280
log("Start jstatd", cmd);
273281
return cmd;
274282
}
275283

284+
private void addToolArg(JDKToolLauncher launcher, String name, String value) {
285+
if (useShortCommandSyntax) {
286+
launcher.addToolArg(name + value);
287+
} else {
288+
launcher.addToolArg(name);
289+
launcher.addToolArg(value);
290+
}
291+
}
292+
276293
private ProcessThread tryToSetupJstatdProcess() throws Throwable {
277294
ProcessThread jstatdThread = new ProcessThread("Jstatd-Thread",
278295
JstatdTest::isJstadReady, getJstatdCmd());
@@ -300,6 +317,12 @@ private static boolean isJstadReady(String line) {
300317
}
301318

302319
public void doTest() throws Throwable {
320+
runTest(false);
321+
runTest(true);
322+
}
323+
324+
private void runTest(boolean useShortSyntax) throws Throwable {
325+
useShortCommandSyntax = useShortSyntax;
303326
if (useDefaultPort) {
304327
verifyNoRmiRegistryOnDefaultPort();
305328
}
@@ -311,6 +334,10 @@ public void doTest() throws Throwable {
311334
port = String.valueOf(Utils.getFreePort());
312335
}
313336

337+
if (!useDefaultRmiPort) {
338+
rmiPort = String.valueOf(Utils.getFreePort());
339+
}
340+
314341
if (withExternalRegistry) {
315342
Registry registry = startRegistry();
316343
if (registry == null) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
*
27+
* @library /test/lib
28+
*
29+
* @build JstatdTest JstatGCUtilParser
30+
* @run main/timeout=60 TestJstatdRmiPort
31+
*/
32+
public class TestJstatdRmiPort {
33+
34+
public static void main(String[] args) throws Throwable {
35+
testRmiPort();
36+
testRegistryAndRmiPorts();
37+
}
38+
39+
private static void testRmiPort() throws Throwable {
40+
JstatdTest test = new JstatdTest();
41+
test.setUseDefaultRmiPort(false);
42+
test.doTest();
43+
}
44+
45+
private static void testRegistryAndRmiPorts() throws Throwable {
46+
JstatdTest test = new JstatdTest();
47+
test.setUseDefaultPort(false);
48+
test.setUseDefaultRmiPort(false);
49+
test.doTest();
50+
}
51+
}

test/jdk/sun/tools/jstatd/TestJstatdUsage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* 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 {
4646
ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
4747
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
4848

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

0 commit comments

Comments
 (0)