blob: fa8d5a2c5cd9824652bb68ecbfe15c0071dabe1b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 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 ServerNotifs.java
26 * @bug 7654321
27 * @summary Tests the reception of the notifications for opened and closed
28 * connections
29 * @author sjiang
30 * @run clean ServerNotifs
31 * @run build ServerNotifs
32 * @run main ServerNotifs
33 */
34
35// JAVA
36import java.io.*;
37import java.net.*;
38import java.util.*;
39
40// JMX
41import javax.management.*;
42
43// RJMX
44import javax.management.remote.*;
45
46public class ServerNotifs {
47
48 private static void echo(String msg) {
49 System.out.println(msg);
50 }
51
52 public static void main(String[] args) {
53
54 try {
55 // Create MBeanServer
56 //
57 echo("---Create the MBeanServer...");
58 MBeanServer mbs = MBeanServerFactory.createMBeanServer();
59
60 // Create RMIConnectorServer
61 //
62 echo("---Instantiate the RMIConnectorServer...");
63 JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
64 JMXConnectorServer cs =
65 JMXConnectorServerFactory.newJMXConnectorServer(url,
66 null,
67 mbs);
68
69 echo("---Register the RMIConnectorServer in the MBeanServer...");
70 ObjectName on =
71 new ObjectName("JMXConnectors:name=RMIConnectorServer");
72 mbs.registerMBean(cs, on);
73
74 echo("---Start the RMIConnectorServer...");
75 cs.start();
76 url = cs.getAddress();
77 echo("---RMIConnectorServer address: " + url);
78
79 echo("---Add a local listener to the RMIConnectorServer...");
80 mbs.addNotificationListener(on, new MyListener(), null, null);
81
82 // Create RMI connector
83 //
84 echo("---Instantiate the RMIConnector...");
85 JMXConnector c = JMXConnectorFactory.newJMXConnector(url, null);
86
87 // Expect to get a "jmx.remote.connection.opened" notification
88 //
89 echo("---Open connection...");
90 c.connect(null);
91 Thread.sleep(100);
92
93 // Expect to get a "jmx.remote.connection.closed" notification
94 //
95 echo("---Close connection...");
96 c.close();
97 Thread.sleep(100);
98
99 // Waiting for all notifications
100 //
101 synchronized(waiting) {
102 if (!succeeded) {
103 final long waitingTime = 10000;
104 long remainingTime = waitingTime;
105 final long startTime = System.currentTimeMillis();
106 while (!succeeded && remainingTime > 0) {
107 waiting.wait(remainingTime);
108 remainingTime = waitingTime -
109 (System.currentTimeMillis() - startTime);
110 }
111 }
112 }
113
114 // Stop the RMIConnectorServer
115 //
116 echo("---Stop the RMIConnectorServer...");
117 cs.stop();
118
119 if (!succeeded) {
120 System.out.println("Timeout, did not get all notifications!");
121 System.exit(1);
122 }
123 } catch (MBeanException mbe) {
124 echo("---Test failed.");
125 echo("---Got exception: " + mbe);
126 mbe.getTargetException().printStackTrace();
127 System.exit(1);
128 } catch (RuntimeOperationsException roe) {
129 echo("---Test failed.");
130 echo("---Got exception: " + roe);
131 roe.getTargetException().printStackTrace();
132 System.exit(1);
133 } catch (Throwable t) {
134 echo("---Test failed.");
135 echo("---Got throwable: " + t);
136 t.printStackTrace();
137 System.exit(1);
138 }
139 }
140
141 private static class MyListener implements NotificationListener {
142 public void handleNotification(Notification n, Object o) {
143 if (index == types.length) {
144 return;
145 }
146 echo("---Got a notification: " + n.getType());
147 echo(n.getMessage());
148 if (n instanceof JMXConnectionNotification) {
149 if (!n.getType().equals(types[index++])) {
150 System.out.println("Waiting to get a notification with " +
151 "type: " + types[index-1] + ", but " +
152 "got one with type: " + n.getType());
153 System.exit(1);
154 }
155 if (index == types.length) {
156 synchronized(waiting) {
157 succeeded = true;
158 waiting.notify();
159 }
160 }
161 }
162 }
163 }
164
165 private static final String[] types =
166 new String[] {JMXConnectionNotification.OPENED,
167 JMXConnectionNotification.CLOSED};
168 private static int index = 0;
169 private static int[] waiting = new int[0];
170 private static boolean succeeded = false;
171}