blob: 0e7a69d345cf4b5aa99df8d7837692d6dbb03ac8 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6238731
27 * @summary Check that the expected notification is received by the JMX
28 * client even when the domain in the ObjectName is not specified
29 * @author Shanliang JIANG
30 * @run clean EmptyDomainNotificationTest
31 * @run build EmptyDomainNotificationTest
32 * @run main EmptyDomainNotificationTest
33 */
34
35import java.util.ArrayList;
36import java.util.List;
37import javax.management.MBeanServer;
38import javax.management.MBeanServerConnection;
39import javax.management.MBeanServerFactory;
40import javax.management.Notification;
41import javax.management.NotificationBroadcasterSupport;
42import javax.management.NotificationListener;
43import javax.management.ObjectName;
44import javax.management.remote.JMXConnector;
45import javax.management.remote.JMXConnectorFactory;
46import javax.management.remote.JMXConnectorServer;
47import javax.management.remote.JMXConnectorServerFactory;
48import javax.management.remote.JMXServiceURL;
49
50public class EmptyDomainNotificationTest {
51
52 public static interface SimpleMBean {
53 public void emitNotification();
54 }
55
56 public static class Simple
57 extends NotificationBroadcasterSupport
58 implements SimpleMBean {
59 public void emitNotification() {
60 sendNotification(new Notification("simple", this, 0));
61 }
62 }
63
64 public static class Listener implements NotificationListener {
65 public void handleNotification(Notification n, Object h) {
66 System.out.println(
67 "EmptyDomainNotificationTest-Listener-handleNotification: receives:" + n);
68
69 if (n.getType().equals("simple")) {
70 synchronized(this) {
71 received++;
72
73 this.notifyAll();
74 }
75 }
76 }
77
78 public int received;
79 }
80
81 public static void main(String[] args) throws Exception {
82
83 final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
84
85 final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
86
87 JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
88 server.start();
89
90 JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
91
92 final MBeanServerConnection mbsc = client.getMBeanServerConnection();
93
94 final ObjectName mbean = ObjectName.getInstance(":type=Simple");
95 mbsc.createMBean(Simple.class.getName(), mbean);
96
97 System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
98 final Listener li = new Listener();
99 mbsc.addNotificationListener(mbean, li, null, null);
100
101 System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
102 mbsc.invoke(mbean, "emitNotification", null, null);
103
104 System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
105 final long stopTime = System.currentTimeMillis() + 2000;
106 synchronized(li) {
107 long toWait = stopTime - System.currentTimeMillis();
108
109 while (li.received < 1 && toWait > 0) {
110 li.wait(toWait);
111
112 toWait = stopTime - System.currentTimeMillis();
113 }
114 }
115
116 if (li.received < 1) {
117 throw new RuntimeException("No notif received!");
118 } else if (li.received > 1) {
119 throw new RuntimeException("Wait one notif but got: "+li.received);
120 }
121
122 System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
123
124 System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
125 mbsc.removeNotificationListener(mbean, li);
126
127 // clean
128 client.close();
129 server.stop();
130
131 System.out.println("EmptyDomainNotificationTest-main: Bye.");
132 }
133}