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