|
1 /* |
|
2 * Copyright 2008 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 * |
|
26 * @test EventWithNamespaceTest.java 1.8 |
|
27 * @bug 6539857 |
|
28 * @summary General Namespace & Notifications test. |
|
29 * @author Daniel Fuchs |
|
30 * @run clean EventWithNamespaceTest Wombat WombatMBean |
|
31 * JMXRemoteTargetNamespace |
|
32 * NamespaceController NamespaceControllerMBean |
|
33 * @compile -XDignore.symbol.file=true EventWithNamespaceTest.java |
|
34 * Wombat.java WombatMBean.java JMXRemoteTargetNamespace.java |
|
35 * NamespaceController.java NamespaceControllerMBean.java |
|
36 * @run main EventWithNamespaceTest |
|
37 */ |
|
38 |
|
39 import java.lang.management.ManagementFactory; |
|
40 import java.util.HashMap; |
|
41 import java.util.Map; |
|
42 import java.util.logging.Logger; |
|
43 |
|
44 import javax.management.JMX; |
|
45 import javax.management.MBeanServer; |
|
46 import javax.management.MBeanServerConnection; |
|
47 import javax.management.MBeanServerFactory; |
|
48 import javax.management.Notification; |
|
49 import javax.management.NotificationEmitter; |
|
50 import javax.management.NotificationListener; |
|
51 import javax.management.ObjectInstance; |
|
52 import javax.management.ObjectName; |
|
53 import javax.management.namespace.JMXNamespaces; |
|
54 import javax.management.remote.JMXConnector; |
|
55 import javax.management.remote.JMXConnectorFactory; |
|
56 import javax.management.remote.JMXConnectorServer; |
|
57 import javax.management.remote.JMXConnectorServerFactory; |
|
58 import javax.management.remote.JMXServiceURL; |
|
59 |
|
60 /** |
|
61 * |
|
62 * @author Sun Microsystems, Inc. |
|
63 */ |
|
64 public class EventWithNamespaceTest { |
|
65 |
|
66 /** |
|
67 * A logger for this class. |
|
68 **/ |
|
69 private static final Logger LOG = |
|
70 Logger.getLogger(EventWithNamespaceTest.class.getName()); |
|
71 |
|
72 /** Creates a new instance of EventWithNamespaceTest */ |
|
73 public EventWithNamespaceTest() { |
|
74 } |
|
75 |
|
76 private static Map<String,?> singletonMap(String key, Object value) { |
|
77 final Map<String,Object> map = new HashMap<String,Object>(); |
|
78 map.put(key,value); |
|
79 return map; |
|
80 } |
|
81 |
|
82 public Map<String,?> getServerMap() { |
|
83 return singletonMap(JMXConnectorServer.DELEGATE_TO_EVENT_SERVICE,"true"); |
|
84 } |
|
85 |
|
86 public JMXServiceURL export(MBeanServer server) |
|
87 throws Exception { |
|
88 final JMXServiceURL in = new JMXServiceURL("rmi",null,0); |
|
89 final Map<String,?> env = getServerMap(); |
|
90 |
|
91 final JMXConnectorServer cs = |
|
92 JMXConnectorServerFactory.newJMXConnectorServer(in,env,null); |
|
93 final ObjectName csname = ObjectName. |
|
94 getInstance(cs.getClass().getPackage().getName()+ |
|
95 ":type="+cs.getClass().getSimpleName()); |
|
96 server.registerMBean(cs,csname); |
|
97 cs.start(); |
|
98 return cs.getAddress(); |
|
99 } |
|
100 |
|
101 public static class Counter { |
|
102 int count; |
|
103 public synchronized int count() { |
|
104 count++; |
|
105 notifyAll(); |
|
106 return count; |
|
107 } |
|
108 public synchronized int peek() { |
|
109 return count; |
|
110 } |
|
111 public synchronized int waitfor(int max, long timeout) |
|
112 throws InterruptedException { |
|
113 final long start = System.currentTimeMillis(); |
|
114 while (count < max && timeout > 0) { |
|
115 final long rest = timeout - |
|
116 (System.currentTimeMillis() - start); |
|
117 if (rest <= 0) break; |
|
118 wait(rest); |
|
119 } |
|
120 return count; |
|
121 } |
|
122 } |
|
123 |
|
124 public static class CounterListener |
|
125 implements NotificationListener { |
|
126 final private Counter counter; |
|
127 public CounterListener(Counter counter) { |
|
128 this.counter = counter; |
|
129 } |
|
130 public void handleNotification(Notification notification, |
|
131 Object handback) { |
|
132 System.out.println("Received notif from " + handback + |
|
133 ":\n\t" + notification); |
|
134 if (!notification.getSource().equals(handback)) { |
|
135 System.err.println("OhOh... Unexpected source: \n\t"+ |
|
136 notification.getSource()+"\n\twas expecting:\n\t"+ |
|
137 handback); |
|
138 } |
|
139 counter.count(); |
|
140 } |
|
141 } |
|
142 |
|
143 public void simpleTest(String[] args) { |
|
144 try { |
|
145 final MBeanServer server1 = |
|
146 ManagementFactory.getPlatformMBeanServer(); |
|
147 final JMXServiceURL url1 = export(server1); |
|
148 |
|
149 final MBeanServer server2 = |
|
150 MBeanServerFactory.createMBeanServer("server2"); |
|
151 final JMXServiceURL url2 = export(server2); |
|
152 |
|
153 final MBeanServer server3 = |
|
154 MBeanServerFactory.createMBeanServer("server3"); |
|
155 final JMXServiceURL url3 = export(server3); |
|
156 |
|
157 final ObjectInstance ncinst = |
|
158 NamespaceController.createInstance(server1); |
|
159 |
|
160 final NamespaceControllerMBean nc = |
|
161 JMX.newMBeanProxy(server1,ncinst.getObjectName(), |
|
162 NamespaceControllerMBean.class); |
|
163 |
|
164 final String mount2 = nc.mount(url2,"server2",null); |
|
165 final String mount3 = nc.mount(url3,"server2//server3", |
|
166 null); |
|
167 |
|
168 final ObjectName deep = |
|
169 new ObjectName("server2//server3//bush:type=Wombat,name=kanga"); |
|
170 server1.createMBean(Wombat.class.getName(),deep); |
|
171 |
|
172 System.err.println("There's a wombat in the bush!"); |
|
173 |
|
174 final Counter counter = new Counter(); |
|
175 |
|
176 final NotificationListener listener = |
|
177 new CounterListener(counter); |
|
178 |
|
179 final JMXConnector jc = JMXConnectorFactory.connect(url1); |
|
180 final MBeanServerConnection conn1 = |
|
181 jc.getMBeanServerConnection(); |
|
182 final ObjectName shallow = |
|
183 new ObjectName("bush:"+ |
|
184 deep.getKeyPropertyListString()); |
|
185 final MBeanServerConnection conn2 = |
|
186 JMXNamespaces.narrowToNamespace(conn1,"server2//server3"); |
|
187 |
|
188 final WombatMBean proxy1 = |
|
189 JMX.newMBeanProxy(conn1,deep,WombatMBean.class,true); |
|
190 final WombatMBean proxy2 = |
|
191 JMX.newMBeanProxy(conn2,shallow,WombatMBean.class,true); |
|
192 |
|
193 |
|
194 System.err.println("Adding first Notification Listener"); |
|
195 conn1.addNotificationListener(deep,listener,null,deep); |
|
196 System.err.println("Adding second Notification Listener"); |
|
197 ((NotificationEmitter)proxy2). |
|
198 addNotificationListener(listener,null,shallow); |
|
199 final JMXConnector c3 = JMXConnectorFactory.connect(url3, |
|
200 singletonMap(JMXConnector.USE_EVENT_SERVICE,"false")); |
|
201 System.err.println("Adding third Notification Listener"); |
|
202 c3.getMBeanServerConnection(). |
|
203 addNotificationListener(shallow,listener,null,shallow); |
|
204 System.err.println("Set attribute to trigger notif"); |
|
205 proxy1.setCaption("I am a new Wombat!"); |
|
206 System.err.println("Get attribute"); |
|
207 System.err.println("New caption: "+proxy2.getCaption()); |
|
208 System.err.println("Wait for Notifs..."); |
|
209 final int rcvcount = counter.waitfor(3,3000); |
|
210 if (rcvcount != 3) |
|
211 throw new RuntimeException("simpleTest failed: "+ |
|
212 "received count is " +rcvcount); |
|
213 System.err.println("simpleTest: got expected "+rcvcount+ |
|
214 " notifs"); |
|
215 |
|
216 System.err.println("removing all listeners"); |
|
217 conn1.removeNotificationListener(deep,listener,null,deep); |
|
218 ((NotificationEmitter)proxy2) |
|
219 .removeNotificationListener(listener,null,shallow); |
|
220 c3.getMBeanServerConnection(). |
|
221 removeNotificationListener(shallow,listener,null,shallow); |
|
222 |
|
223 System.err.println("simpleTest passed: got "+rcvcount+ |
|
224 " notifs"); |
|
225 |
|
226 } catch (RuntimeException x) { |
|
227 throw x; |
|
228 } catch (Exception x) { |
|
229 throw new RuntimeException("simpleTest failed: " + x,x); |
|
230 } |
|
231 } |
|
232 |
|
233 public void run(String[] args) { |
|
234 simpleTest(args); |
|
235 } |
|
236 |
|
237 public static void main(String[] args) { |
|
238 new EventWithNamespaceTest().run(args); |
|
239 } |
|
240 |
|
241 } |