1004
|
1 |
/*
|
|
2 |
* Copyright 2007 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 5108776
|
|
27 |
* @summary Basic test for EventClient.
|
|
28 |
* @author Shanliang JIANG
|
|
29 |
* @run clean FetchingTest MyFetchingEventForwarder
|
|
30 |
* @run build FetchingTest MyFetchingEventForwarder
|
|
31 |
* @run main FetchingTest MyFetchingEventForwarder
|
|
32 |
*/
|
|
33 |
|
|
34 |
import javax.management.MBeanServer;
|
|
35 |
import javax.management.MBeanServerConnection;
|
|
36 |
import javax.management.MBeanServerFactory;
|
|
37 |
import javax.management.Notification;
|
|
38 |
import javax.management.NotificationBroadcasterSupport;
|
|
39 |
import javax.management.NotificationListener;
|
|
40 |
import javax.management.ObjectName;
|
|
41 |
import javax.management.event.EventClient;
|
|
42 |
import javax.management.event.EventClientDelegate;
|
|
43 |
import javax.management.event.EventClientDelegateMBean;
|
|
44 |
import javax.management.event.FetchingEventRelay;
|
|
45 |
import javax.management.event.RMIPushEventForwarder;
|
|
46 |
import javax.management.event.RMIPushServer;
|
|
47 |
import javax.management.remote.JMXConnector;
|
|
48 |
import javax.management.remote.JMXConnectorFactory;
|
|
49 |
import javax.management.remote.JMXConnectorServer;
|
|
50 |
import javax.management.remote.JMXConnectorServerFactory;
|
|
51 |
import javax.management.remote.JMXServiceURL;
|
|
52 |
|
|
53 |
public class FetchingTest {
|
|
54 |
private static MBeanServer mbeanServer;
|
|
55 |
private static ObjectName emitter;
|
|
56 |
private static JMXServiceURL url;
|
|
57 |
private static JMXConnectorServer server;
|
|
58 |
private static JMXConnector conn;
|
|
59 |
private static MBeanServerConnection client;
|
|
60 |
private static long WAITING_TIME = 6000;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* @param args the command line arguments
|
|
64 |
*/
|
|
65 |
public static void main(String[] args) throws Exception {
|
|
66 |
|
|
67 |
System.out.println(">>> FetchingTest-main basic tests ...");
|
|
68 |
mbeanServer = MBeanServerFactory.createMBeanServer();
|
|
69 |
|
|
70 |
// for 1.5
|
|
71 |
if (System.getProperty("java.version").startsWith("1.5") &&
|
|
72 |
!mbeanServer.isRegistered(EventClientDelegateMBean.OBJECT_NAME)) {
|
|
73 |
System.out.print("Working on "+System.getProperty("java.version")+
|
|
74 |
" register "+EventClientDelegateMBean.OBJECT_NAME);
|
|
75 |
|
|
76 |
mbeanServer.registerMBean(EventClientDelegate.
|
|
77 |
getEventClientDelegate(mbeanServer),
|
|
78 |
EventClientDelegateMBean.OBJECT_NAME);
|
|
79 |
}
|
|
80 |
|
|
81 |
emitter = new ObjectName("Default:name=NotificationEmitter");
|
|
82 |
mbeanServer.registerMBean(new NotificationEmitter(), emitter);
|
|
83 |
boolean succeed = true;
|
|
84 |
|
|
85 |
final String[] protos = new String[] {"rmi", "iiop", "jmxmp"};
|
|
86 |
for (String proto : protos) {
|
|
87 |
System.out.println(">>> FetchingTest-main: testing on "+proto);
|
|
88 |
|
|
89 |
try {
|
|
90 |
url = new JMXServiceURL(proto, null, 0) ;
|
|
91 |
server = JMXConnectorServerFactory.
|
|
92 |
newJMXConnectorServer(url, null, mbeanServer);
|
|
93 |
server.start();
|
|
94 |
} catch (Exception e) {
|
|
95 |
// OK
|
|
96 |
System.out.println(">>> FetchingTest-main: skip the proto "+proto);
|
|
97 |
continue;
|
|
98 |
}
|
|
99 |
|
|
100 |
url = server.getAddress();
|
|
101 |
conn = JMXConnectorFactory.connect(url, null);
|
|
102 |
client = conn.getMBeanServerConnection();
|
|
103 |
|
|
104 |
succeed &= test();
|
|
105 |
|
|
106 |
conn.close();
|
|
107 |
server.stop();
|
|
108 |
|
|
109 |
System.out.println(
|
|
110 |
">>> FetchingTest-main: testing on "+proto+" done.");
|
|
111 |
}
|
|
112 |
|
|
113 |
if (succeed) {
|
|
114 |
System.out.println(">>> FetchingTest-main: PASSED!");
|
|
115 |
} else {
|
|
116 |
System.out.println("\n>>> FetchingTest-main: FAILED!");
|
|
117 |
System.exit(1);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
public static boolean test() throws Exception {
|
|
122 |
System.out.println(">>> FetchingTest-test: " +
|
|
123 |
"using the default fetching forwarder ...");
|
|
124 |
EventClient eventClient =
|
|
125 |
new EventClient(client);
|
|
126 |
|
|
127 |
Listener listener = new Listener();
|
|
128 |
eventClient.addNotificationListener(emitter, listener, null, null);
|
|
129 |
|
|
130 |
// ask to send notifs
|
|
131 |
Object[] params = new Object[] {new Integer(sendNB)};
|
|
132 |
String[] signatures = new String[] {"java.lang.Integer"};
|
|
133 |
conn.getMBeanServerConnection().invoke(emitter,
|
|
134 |
"sendNotifications", params, signatures);
|
|
135 |
|
|
136 |
if (listener.waitNotif(WAITING_TIME) != sendNB) {
|
|
137 |
System.out.println(
|
|
138 |
">>> FetchingTest-test: FAILED! Expected to receive "+
|
|
139 |
sendNB+", but got "+listener.received);
|
|
140 |
|
|
141 |
return false;
|
|
142 |
}
|
|
143 |
|
|
144 |
System.out.println(
|
|
145 |
">>> ListenerTest-test: got all expected "+listener.received);
|
|
146 |
//eventClient.removeNotificationListener(emitter, listener);
|
|
147 |
eventClient.close();
|
|
148 |
|
|
149 |
System.out.println(">>> FetchingTest-test: " +
|
|
150 |
"using a user specific List ...");
|
|
151 |
|
|
152 |
FetchingEventRelay fer = new FetchingEventRelay(
|
|
153 |
EventClientDelegate.getProxy(client),
|
|
154 |
1000, 1000L, 1000, null,
|
|
155 |
MyFetchingEventForwarder.class.getName(),
|
|
156 |
null, null);
|
|
157 |
|
|
158 |
eventClient = new EventClient(
|
|
159 |
EventClientDelegate.getProxy(client), fer, null, null, 10000);
|
|
160 |
|
|
161 |
eventClient.addNotificationListener(emitter, listener, null, null);
|
|
162 |
listener.received = 0;
|
|
163 |
|
|
164 |
conn.getMBeanServerConnection().invoke(emitter,
|
|
165 |
"sendNotifications", params, signatures);
|
|
166 |
|
|
167 |
if (listener.waitNotif(WAITING_TIME) != sendNB) {
|
|
168 |
System.out.println(
|
|
169 |
">>> FetchingTest-test: FAILED! Expected to receive "+
|
|
170 |
sendNB+", but got "+listener.received);
|
|
171 |
|
|
172 |
return false;
|
|
173 |
}
|
|
174 |
|
|
175 |
System.out.println(
|
|
176 |
">>> FetchingTest-test: got all expected "+listener.received);
|
|
177 |
|
|
178 |
if (!MyFetchingEventForwarder.shared.isUsed()) {
|
|
179 |
System.out.println(
|
|
180 |
">>> FetchingTest-test: FAILED! The user specific list" +
|
|
181 |
"is not used!");
|
|
182 |
|
|
183 |
return false;
|
|
184 |
}
|
|
185 |
|
|
186 |
System.out.println(">>> Negative test to add an EventClient" +
|
|
187 |
" with a non EventForwarder object.");
|
|
188 |
try {
|
|
189 |
MyFetchingEventForwarder.shared.setAgain();
|
|
190 |
|
|
191 |
System.out.println(
|
|
192 |
">>> FetchingTest-test: FAILED! No expected exception" +
|
|
193 |
"when setting the list after the forwarder started.");
|
|
194 |
|
|
195 |
return false;
|
|
196 |
} catch (IllegalStateException ise) {
|
|
197 |
// OK
|
|
198 |
System.out.println(
|
|
199 |
">>> FetchingTest-test: Got expected exception: " + ise);
|
|
200 |
}
|
|
201 |
|
|
202 |
eventClient.close();
|
|
203 |
|
|
204 |
try {
|
|
205 |
fer = new FetchingEventRelay(
|
|
206 |
EventClientDelegate.getProxy(client),
|
|
207 |
1000, 1000L, 1000, null,
|
|
208 |
Object.class.getName(),
|
|
209 |
null, null);
|
|
210 |
|
|
211 |
eventClient = new EventClient(
|
|
212 |
EventClientDelegate.getProxy(client), fer, null, null, 10000);
|
|
213 |
|
|
214 |
System.out.println(
|
|
215 |
">>> FetchingTest-test: FAILED! No expected exception" +
|
|
216 |
"when creating an illegal EventForwarder");
|
|
217 |
} catch (IllegalArgumentException iae) {
|
|
218 |
// OK
|
|
219 |
// iae.printStackTrace();
|
|
220 |
}
|
|
221 |
|
|
222 |
return true;
|
|
223 |
}
|
|
224 |
|
|
225 |
private static class Listener implements NotificationListener {
|
|
226 |
public void handleNotification(Notification notif, Object handback) {
|
|
227 |
synchronized(this) {
|
|
228 |
if (++received >= sendNB) {
|
|
229 |
this.notify();
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
//System.out.println(">>> FetchingTest-Listener: received = "+received);
|
|
234 |
}
|
|
235 |
|
|
236 |
public int waitNotif(long timeout) throws Exception {
|
|
237 |
synchronized(this) {
|
|
238 |
long stopTime = System.currentTimeMillis() + timeout;
|
|
239 |
long toWait = timeout;
|
|
240 |
while (toWait > 0 && received < sendNB) {
|
|
241 |
this.wait(toWait);
|
|
242 |
toWait = stopTime - System.currentTimeMillis();
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
return received;
|
|
247 |
}
|
|
248 |
|
|
249 |
public static int received = 0;
|
|
250 |
}
|
|
251 |
|
|
252 |
public static class NotificationEmitter extends NotificationBroadcasterSupport
|
|
253 |
implements NotificationEmitterMBean {
|
|
254 |
|
|
255 |
public void sendNotifications(Integer nb) {
|
|
256 |
System.out.println(
|
|
257 |
">>> FetchingTest-NotificationEmitter-sendNotifications: "+nb);
|
|
258 |
Notification notif;
|
|
259 |
for (int i=1; i<=nb.intValue(); i++) {
|
|
260 |
notif = new Notification(myType, this, count++);
|
|
261 |
sendNotification(notif);
|
|
262 |
}
|
|
263 |
}
|
|
264 |
}
|
|
265 |
|
|
266 |
public interface NotificationEmitterMBean {
|
|
267 |
public void sendNotifications(Integer nb);
|
|
268 |
}
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
private static int sendNB = 20;
|
|
273 |
private static int count = 0;
|
|
274 |
|
|
275 |
private static final String myType = "notification.my_notification";
|
|
276 |
}
|