Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jmxremote/startstop/JMXStartStopTest.java
38867 views
1
/*
2
* Copyright (c) 2012, 2014, 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
import java.io.File;
25
import java.io.IOException;
26
import java.lang.reflect.Method;
27
import java.net.ConnectException;
28
import java.net.ServerSocket;
29
import java.rmi.NoSuchObjectException;
30
import java.rmi.registry.LocateRegistry;
31
import java.rmi.registry.Registry;
32
import java.util.ArrayList;
33
import java.util.Arrays;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Objects;
37
import java.util.Set;
38
import java.util.concurrent.TimeUnit;
39
import java.util.concurrent.TimeoutException;
40
import java.util.concurrent.atomic.AtomicBoolean;
41
import java.util.function.Consumer;
42
43
import javax.management.*;
44
import javax.management.remote.*;
45
import javax.net.ssl.SSLHandshakeException;
46
47
import jdk.testlibrary.ProcessTools;
48
import jdk.testlibrary.JDKToolLauncher;
49
50
/**
51
* @test
52
* @bug 7110104
53
* @library /lib/testlibrary
54
* @build jdk.testlibrary.* JMXStartStopTest JMXStartStopDoSomething
55
* @run main/othervm JMXStartStopTest
56
* @summary Makes sure that enabling/disabling the management agent through
57
* JCMD achieves the desired results
58
* @key randomness
59
*/
60
public class JMXStartStopTest {
61
private static final String TEST_SRC = System.getProperty("test.src");
62
63
private static final boolean verbose = false;
64
65
private static void dbg_print(String msg){
66
if (verbose) {
67
System.out.println("DBG: " +msg);
68
}
69
}
70
71
private static int listMBeans(MBeanServerConnection server,
72
ObjectName pattern,
73
QueryExp query)
74
throws Exception {
75
76
Set<ObjectName> names = server.queryNames(pattern,query);
77
for (Iterator<ObjectName> i = names.iterator(); i.hasNext(); ) {
78
ObjectName name = (ObjectName)i.next();
79
MBeanInfo info = server.getMBeanInfo(name);
80
dbg_print("Got MBean: " + name);
81
82
MBeanAttributeInfo[] attrs = info.getAttributes();
83
if (attrs == null)
84
continue;
85
for (MBeanAttributeInfo attr : attrs) {
86
if (attr.isReadable()) {
87
server.getAttribute(name, attr.getName());
88
}
89
}
90
}
91
return names.size();
92
}
93
94
95
private static void testConnectLocal(int pid)
96
throws Exception {
97
98
String jmxUrlStr = null;
99
100
try {
101
jmxUrlStr = sun.management.ConnectorAddressLink.importFrom(pid);
102
dbg_print("Local Service URL: " +jmxUrlStr);
103
if ( jmxUrlStr == null ) {
104
throw new Exception("No Service URL. Local agent not started?");
105
}
106
107
JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
108
109
JMXConnector c = JMXConnectorFactory.connect(url, null);
110
111
MBeanServerConnection conn = c.getMBeanServerConnection();
112
ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
113
114
int count = listMBeans(conn,pattern,null);
115
if (count == 0)
116
throw new Exception("Expected at least one matching "+
117
"MBean for "+pattern);
118
119
} catch (IOException e) {
120
dbg_print("Cannot find process : " + pid);
121
throw e;
122
}
123
}
124
125
private static void testNoConnect(int port) throws Exception {
126
testNoConnect(port, 0);
127
}
128
129
private static void testNoConnect(int port, int rmiPort) throws Exception {
130
try {
131
testConnect(port, rmiPort);
132
throw new Exception("Didn't expect the management agent running");
133
} catch (Exception e) {
134
Throwable t = e;
135
while (t != null) {
136
if (t instanceof NoSuchObjectException ||
137
t instanceof ConnectException ||
138
t instanceof SSLHandshakeException) {
139
break;
140
}
141
t = t.getCause();
142
}
143
if (t == null) {
144
throw new Exception("Unexpected exception", e);
145
}
146
}
147
}
148
149
private static void testConnect(int port) throws Exception {
150
testConnect(port, 0);
151
}
152
153
private static void testConnect(int port, int rmiPort) throws Exception {
154
155
dbg_print("RmiRegistry lookup...");
156
157
dbg_print("Using port: " + port);
158
159
dbg_print("Using rmi port: " + rmiPort);
160
161
Registry registry = LocateRegistry.getRegistry(port);
162
163
// "jmxrmi"
164
String[] relist = registry.list();
165
for (int i = 0; i < relist.length; ++i) {
166
dbg_print("Got registry: " + relist[i]);
167
}
168
169
String jmxUrlStr = (rmiPort != 0) ?
170
String.format(
171
"service:jmx:rmi://localhost:%d/jndi/rmi://localhost:%d/jmxrmi",
172
rmiPort,
173
port) :
174
String.format(
175
"service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
176
port);
177
178
JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
179
180
JMXConnector c = JMXConnectorFactory.connect(url, null);
181
182
MBeanServerConnection conn = c.getMBeanServerConnection();
183
ObjectName pattern = new ObjectName("java.lang:type=Memory,*");
184
185
int count = listMBeans(conn,pattern,null);
186
if (count == 0)
187
throw new Exception("Expected at least one matching " +
188
"MBean for " + pattern);
189
}
190
191
private static class Failure {
192
private final Throwable cause;
193
private final String msg;
194
195
public Failure(Throwable cause, String msg) {
196
this.cause = cause;
197
this.msg = msg;
198
}
199
200
public Failure(String msg) {
201
this(null, msg);
202
}
203
204
public Throwable getCause() {
205
return cause;
206
}
207
208
public String getMsg() {
209
return msg;
210
}
211
212
@Override
213
public int hashCode() {
214
int hash = 7;
215
hash = 97 * hash + Objects.hashCode(this.cause);
216
hash = 97 * hash + Objects.hashCode(this.msg);
217
return hash;
218
}
219
220
@Override
221
public boolean equals(Object obj) {
222
if (obj == null) {
223
return false;
224
}
225
if (getClass() != obj.getClass()) {
226
return false;
227
}
228
final Failure other = (Failure) obj;
229
if (!Objects.equals(this.cause, other.cause)) {
230
return false;
231
}
232
if (!Objects.equals(this.msg, other.msg)) {
233
return false;
234
}
235
return true;
236
}
237
238
@Override
239
public String toString() {
240
if (cause != null) {
241
return msg + "\n" + cause;
242
} else {
243
return msg;
244
}
245
}
246
}
247
248
private static List<Failure> failures = new ArrayList<>();
249
250
public static void main(String args[]) throws Exception {
251
for (Method m : JMXStartStopTest.class.getDeclaredMethods()) {
252
if (m.getName().startsWith("test_")) {
253
try {
254
m.invoke(null);
255
System.out.println("=== PASSED\n");
256
} catch (Throwable e) {
257
failures.add(new Failure(e, m.getName() + " failed"));
258
}
259
}
260
}
261
262
if (!failures.isEmpty()) {
263
for(Failure f : failures) {
264
System.err.println(f.getMsg());
265
f.getCause().printStackTrace(System.err);
266
}
267
throw new Error();
268
}
269
}
270
271
private static class Something {
272
private Process p;
273
private final ProcessBuilder pb;
274
private final String name;
275
private final AtomicBoolean started = new AtomicBoolean(false);
276
private volatile int pid = -1;
277
278
public Something(ProcessBuilder pb, String name) {
279
this.pb = pb;
280
this.name = name;
281
}
282
283
public synchronized void start() throws InterruptedException, IOException, TimeoutException {
284
if (started.compareAndSet(false, true)) {
285
try {
286
p = ProcessTools.startProcess(
287
"JMXStartStopDoSomething",
288
pb,
289
(line) -> {
290
if (line.toLowerCase().startsWith("pid:")) {
291
pid = Integer.parseInt(line.split("\\:")[1]);
292
}
293
return line.equals("main enter");
294
},
295
5,
296
TimeUnit.SECONDS
297
);
298
} catch (TimeoutException e) {
299
p.destroy();
300
p.waitFor();
301
throw e;
302
}
303
}
304
}
305
306
public int getPid() {
307
return pid;
308
}
309
310
public synchronized void stop()
311
throws IOException, InterruptedException {
312
if (started.compareAndSet(true, false)) {
313
p.getOutputStream().write(0);
314
p.getOutputStream().flush();
315
int ec = p.waitFor();
316
if (ec != 0) {
317
StringBuilder msg = new StringBuilder();
318
msg.append("Test application '").append(name);
319
msg.append("' failed with exit code: ");
320
msg.append(ec);
321
322
failures.add(new Failure(msg.toString()));
323
}
324
}
325
}
326
}
327
328
/**
329
* Runs the test application "JMXStartStopDoSomething"
330
* @param name Test run name
331
* @param args Additional arguments
332
* @return Returns a {@linkplain Something} instance representing the run
333
* @throws IOException
334
* @throws InterruptedException
335
* @throws TimeoutException
336
*/
337
private static Something doSomething(String name, String ... args)
338
throws Exception {
339
List<String> pbArgs = new ArrayList<>(Arrays.asList(
340
"-cp",
341
System.getProperty("test.class.path")
342
));
343
pbArgs.addAll(Arrays.asList(args));
344
pbArgs.add("JMXStartStopDoSomething");
345
346
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
347
pbArgs.toArray(new String[pbArgs.size()])
348
);
349
Something s = new Something(pb, name);
350
s.start();
351
return s;
352
}
353
354
/**
355
* Run the "jcmd" command
356
*
357
* @param command Command with parameters; space separated string
358
* @throws IOException
359
* @throws InterruptedException
360
*/
361
private static void jcmd(String ... command) throws IOException, InterruptedException {
362
if (command.length == 0) {
363
jcmd(null, (Consumer<String>)null);
364
} else {
365
jcmd(null, command);
366
}
367
}
368
369
/**
370
* Run the "jcmd" command
371
*
372
* @param c {@linkplain Consumer} instance; may be null
373
* @param command Command with parameters; space separated string
374
* @throws IOException
375
* @throws InterruptedException
376
*/
377
private static void jcmd(Consumer<String> c, String ... command) throws IOException, InterruptedException {
378
jcmd("JMXStartStopDoSomething", c, command);
379
}
380
381
/**
382
* Run the "jcmd" command
383
* @param target The target application name (or PID)
384
* @param c {@linkplain Consumer} instance; may be null
385
* @param command Command with parameters; space separated string
386
* @throws IOException
387
* @throws InterruptedException
388
*/
389
private static void jcmd(String target, final Consumer<String> c, String ... command) throws IOException, InterruptedException {
390
dbg_print("[jcmd] " + (command.length > 0 ? command[0] : "list"));
391
392
JDKToolLauncher l = JDKToolLauncher.createUsingTestJDK("jcmd");
393
l.addToolArg(target);
394
for(String cmd : command) {
395
l.addToolArg(cmd);
396
}
397
Process p = ProcessTools.startProcess(
398
"jcmd",
399
new ProcessBuilder(l.getCommand()),
400
c
401
);
402
403
p.waitFor();
404
dbg_print("[jcmd] --------");
405
}
406
407
private static final String CMD_STOP = "ManagementAgent.stop";
408
private static final String CMD_START= "ManagementAgent.start";
409
private static final String CMD_START_LOCAL = "ManagementAgent.start_local";
410
private static final int port1 = 50234;
411
private static final int port2 = 50235;
412
413
static void test_01() throws Exception {
414
// Run an app with JMX enabled stop it and
415
// restart on other port
416
417
System.out.println("**** Test one ****");
418
419
Something s = doSomething(
420
"test_01",
421
"-Dcom.sun.management.jmxremote.port=" + port1,
422
"-Dcom.sun.management.jmxremote.authenticate=false",
423
"-Dcom.sun.management.jmxremote.ssl=false");
424
425
try {
426
testConnect(port1);
427
428
jcmd(CMD_STOP);
429
testNoConnect(port1);
430
431
jcmd(CMD_START, "jmxremote.port=" + port2);
432
testConnect(port2);
433
} finally {
434
s.stop();
435
}
436
}
437
438
static void test_02() throws Exception {
439
// Run an app without JMX enabled
440
// start JMX by jcmd
441
442
System.out.println("**** Test two ****");
443
444
Something s = doSomething("test_02");
445
try {
446
jcmd(CMD_START,
447
"jmxremote.port=" + port1,
448
"jmxremote.authenticate=false",
449
"jmxremote.ssl=false");
450
451
testConnect(port1);
452
} finally {
453
s.stop();
454
}
455
}
456
457
static void test_03() throws Exception {
458
// Run an app without JMX enabled
459
// start JMX by jcmd on one port than on other one
460
461
System.out.println("**** Test three ****");
462
463
Something s = doSomething("test_03");
464
try {
465
jcmd(CMD_START,
466
"jmxremote.port=" + port1,
467
"jmxremote.authenticate=false",
468
"jmxremote.ssl=false");
469
470
// Second agent shouldn't start
471
jcmd(CMD_START,
472
"jmxremote.port=" + port2,
473
"jmxremote.authenticate=false",
474
"jmxremote.ssl=false");
475
476
// First agent should connect
477
testConnect(port1);
478
479
// Second agent should not connect
480
testNoConnect(port2);
481
} finally {
482
s.stop();
483
}
484
}
485
486
static void test_04() throws Exception {
487
// Run an app without JMX enabled
488
// start JMX by jcmd on one port, specify rmi port explicitly
489
490
System.out.println("**** Test four ****");
491
492
Something s = doSomething("test_04");
493
494
try {
495
jcmd(CMD_START,
496
"jmxremote.port=" + port1,
497
"jmxremote.rmi.port=" + port2,
498
"jmxremote.authenticate=false",
499
"jmxremote.ssl=false");
500
501
testConnect(port1, port2);
502
} finally {
503
s.stop();
504
}
505
}
506
507
static void test_05() throws Exception {
508
// Run an app without JMX enabled, it will enable local server
509
// but should leave remote server disabled
510
511
System.out.println("**** Test five ****");
512
513
Something s = doSomething("test_05");
514
try {
515
jcmd(CMD_START_LOCAL);
516
517
testNoConnect(port1);
518
testConnectLocal(s.getPid());
519
} finally {
520
s.stop();
521
}
522
}
523
524
static void test_06() throws Exception {
525
// Run an app without JMX enabled
526
// start JMX by jcmd on one port, specify rmi port explicitly
527
// attempt to start it again
528
// 1) with the same port
529
// 2) with other port
530
// 3) attempt to stop it twice
531
// Check for valid messages in the output
532
533
System.out.println("**** Test six ****");
534
535
Something s = doSomething("test_06");
536
537
try {
538
jcmd(CMD_START,
539
"jmxremote.port=" + port1,
540
"jmxremote.authenticate=false",
541
"jmxremote.ssl=false");
542
543
testConnect(port1, port2);
544
545
final boolean[] checks = new boolean[3];
546
jcmd(
547
line -> {
548
if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
549
checks[0] = true;
550
}
551
},
552
CMD_START,
553
"jmxremote.port=" + port1,
554
"jmxremote.authenticate=false",
555
"jmxremote.ssl=false");
556
557
jcmd(
558
line -> {
559
if (line.contains("java.lang.RuntimeException: Invalid agent state")) {
560
checks[1] = true;
561
}
562
},
563
CMD_START,
564
"jmxremote.port=" + port2,
565
"jmxremote.authenticate=false",
566
"jmxremote.ssl=false");
567
568
jcmd(CMD_STOP);
569
jcmd(CMD_STOP);
570
571
ServerSocket ss = new ServerSocket(0);
572
573
jcmd(
574
line -> {
575
if (line.contains("Port already in use: " + ss.getLocalPort())) {
576
checks[2] = true;
577
}
578
},
579
CMD_START,
580
"jmxremote.port=" + ss.getLocalPort(),
581
"jmxremote.rmi.port=" + port2,
582
"jmxremote.authenticate=false",
583
"jmxremote.ssl=false");
584
if (!checks[0]) {
585
throw new Exception("Starting agent on port " + port1 + " should " +
586
"report an invalid agent state");
587
}
588
if (!checks[1]) {
589
throw new Exception("Starting agent on poprt " + port2 + " should " +
590
"report an invalid agent state");
591
}
592
if (!checks[2]) {
593
throw new Exception("Starting agent on port " + ss.getLocalPort() + " should " +
594
"report port in use");
595
}
596
} finally {
597
s.stop();
598
}
599
}
600
601
private static void test_07() throws Exception {
602
// Run an app without JMX enabled, but with some properties set
603
// in command line.
604
// make sure these properties overridden corectly
605
606
System.out.println("**** Test seven ****");
607
608
Something s = doSomething(
609
"test_07",
610
"-Dcom.sun.management.jmxremote.authenticate=false",
611
"-Dcom.sun.management.jmxremote.ssl=true");
612
613
try {
614
testNoConnect(port1);
615
jcmd(
616
CMD_START,
617
"jmxremote.port=" + port2,
618
"jmxremote.authenticate=false",
619
"jmxremote.ssl=false"
620
);
621
testConnect(port2);
622
} finally {
623
s.stop();
624
}
625
}
626
627
static void test_08() throws Exception {
628
// Run an app with JMX enabled and with some properties set
629
// in command line.
630
// stop JMX agent and then start it again with different property values
631
// make sure these properties overridden corectly
632
633
System.out.println("**** Test eight ****");
634
635
Something s = doSomething(
636
"test_08",
637
"-Dcom.sun.management.jmxremote.port=" + port1,
638
"-Dcom.sun.management.jmxremote.authenticate=false",
639
"-Dcom.sun.management.jmxremote.ssl=true");
640
641
try {
642
testNoConnect(port1);
643
644
jcmd(CMD_STOP);
645
646
testNoConnect(port1);
647
648
jcmd(
649
CMD_START,
650
"jmxremote.port=" + port2,
651
"jmxremote.authenticate=false",
652
"jmxremote.ssl=false"
653
);
654
655
testConnect(port2);
656
} finally {
657
s.stop();
658
}
659
}
660
661
static void test_09() throws Exception {
662
// Run an app with JMX enabled and with some properties set
663
// in command line.
664
// stop JMX agent and then start it again with different property values
665
// specifing some property in management config file and some of them
666
// in command line
667
// make sure these properties overridden corectly
668
669
System.out.println("**** Test nine ****");
670
671
Something s = doSomething("test_09",
672
"-Dcom.sun.management.config.file=" +
673
TEST_SRC + File.separator + "management_cl.properties",
674
"-Dcom.sun.management.jmxremote.authenticate=false"
675
);
676
677
try {
678
testNoConnect(port1);
679
680
jcmd(CMD_STOP);
681
682
testNoConnect(port1);
683
684
jcmd(CMD_START,
685
"config.file=" + TEST_SRC + File.separator +
686
"management_jcmd.properties",
687
"jmxremote.authenticate=false",
688
"jmxremote.port=" + port2
689
);
690
691
testConnect(port2);
692
} finally {
693
s.stop();
694
}
695
}
696
697
static void test_10() throws Exception {
698
// Run an app with JMX enabled and with some properties set
699
// in command line.
700
// stop JMX agent and then start it again with different property values
701
// stop JMX agent again and then start it without property value
702
// make sure these properties overridden corectly
703
704
System.out.println("**** Test ten ****");
705
706
Something s = doSomething(
707
"test_10",
708
"-Dcom.sun.management.jmxremote.port=" + port1,
709
"-Dcom.sun.management.jmxremote.authenticate=false",
710
"-Dcom.sun.management.jmxremote.ssl=true");
711
712
try {
713
testNoConnect(port1);
714
715
jcmd(CMD_STOP);
716
jcmd(CMD_START,
717
"jmxremote.ssl=false",
718
"jmxremote.port=" + port1
719
);
720
testConnect(port1);
721
722
jcmd(CMD_STOP);
723
jcmd(CMD_START,
724
"jmxremote.port=" + port1
725
);
726
727
testNoConnect(port1);
728
} finally {
729
s.stop();
730
}
731
}
732
733
static void test_11() throws Exception {
734
// Run an app with JMX enabled
735
// stop remote agent
736
// make sure local agent is not affected
737
738
System.out.println("**** Test eleven ****");
739
740
Something s = doSomething(
741
"test_11",
742
"-Dcom.sun.management.jmxremote.port=" + port1,
743
"-Dcom.sun.management.jmxremote.authenticate=false",
744
"-Dcom.sun.management.jmxremote.ssl=false");
745
try {
746
testConnect(port1);
747
jcmd(CMD_STOP);
748
testConnectLocal(s.getPid());
749
} finally {
750
s.stop();
751
}
752
}
753
754
static void test_12() throws Exception {
755
// Run an app with JMX disabled
756
// start local agent only
757
758
System.out.println("**** Test twelve ****");
759
760
Something s = doSomething("test_12");
761
762
try {
763
testNoConnect(port1);
764
jcmd(CMD_START + "_local");
765
766
testConnectLocal(s.getPid());
767
768
} finally {
769
s.stop();
770
}
771
}
772
}
773
774