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
26  * @bug 4838640
27  * @summary test server close in different conditions.
28  * @author Shanliang JIANG
29  *
30  * @run clean CloseServerTest
31  * @run build CloseServerTest
32  * @run main CloseServerTest
33  */
34 
35 import java.net.MalformedURLException;
36 import java.io.IOException;
37 
38 import javax.management.*;
39 import javax.management.remote.*;
40 
41 public class CloseServerTest {
42     private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
43     private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
44 
main(String[] args)45     public static void main(String[] args) {
46         System.out.println(">>> Tests for closing a server.");
47 
48         boolean ok = true;
49         for (int i = 0; i < protocols.length; i++) {
50             try {
51                 if (!test(protocols[i])) {
52                     System.out.println(">>> Test failed for " + protocols[i]);
53                     ok = false;
54                 } else {
55                     System.out.println(">>> Test successed for " + protocols[i]);
56                 }
57             } catch (Exception e) {
58                 System.out.println(">>> Test failed for " + protocols[i]);
59                 e.printStackTrace(System.out);
60                 ok = false;
61             }
62         }
63 
64         if (ok) {
65             System.out.println(">>> Test passed");
66         } else {
67             System.out.println(">>> TEST FAILED");
68             System.exit(1);
69         }
70     }
71 
test(String proto)72     private static boolean test(String proto)
73             throws Exception {
74         System.out.println(">>> Test for protocol " + proto);
75         JMXServiceURL u = new JMXServiceURL(proto, null, 0);
76         JMXConnectorServer server;
77         JMXServiceURL addr;
78         JMXConnector client;
79         MBeanServerConnection mserver;
80 
81         final ObjectName delegateName =
82                     new ObjectName("JMImplementation:type=MBeanServerDelegate");
83         final NotificationListener dummyListener = new NotificationListener() {
84                 public void handleNotification(Notification n, Object o) {
85                     // do nothing
86                     return;
87                 }
88             };
89 
90         try {
91             // open and close
92             System.out.println(">>> Open and close a server.");
93 
94             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
95             server.stop();
96 
97             // open, start then close
98             System.out.println(">>> Open, start and close a server.");
99 
100             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
101             server.start();
102             server.stop();
103 
104             // with a client, but close the server first
105             System.out.println(">>> Open, start a server, create a client, close the server then the client.");
106 
107             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
108             server.start();
109 
110             addr = server.getAddress();
111             client = JMXConnectorFactory.newJMXConnector(addr, null);
112             client.connect(null);
113 
114             server.stop();
115 
116             try {
117                 client.close();
118             } catch (Exception ee) {
119                 // OK, the server has been closed
120             }
121 
122             // with a client, but close the client first
123             System.out.println(">>> Open, start a server, create a client, close the client then server.");
124             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
125             server.start();
126 
127             addr = server.getAddress();
128             client = JMXConnectorFactory.newJMXConnector(addr, null);
129             client.connect(null);
130 
131             client.close();
132 
133             server.stop();
134 
135             // with a client listener, but close the server first
136             System.out.println(">>> Open, start a server, create a client, add a listener, close the server then the client.");
137             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
138             server.start();
139 
140             addr = server.getAddress();
141             client = JMXConnectorFactory.newJMXConnector(addr, null);
142             client.connect(null);
143 
144             mserver = client.getMBeanServerConnection();
145             mserver.addNotificationListener(delegateName, dummyListener, null, null);
146 
147             server.stop();
148 
149             try {
150                 client.close();
151             } catch (Exception e) {
152                 // ok, it is because the server has been closed.
153             }
154 
155             // with a client listener, but close the client first
156             System.out.println(">>> Open, start a server, create a client, add a listener, close the client then the server.");
157             server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
158             server.start();
159 
160             addr = server.getAddress();
161             client = JMXConnectorFactory.newJMXConnector(addr, null);
162             client.connect(null);
163 
164             mserver = client.getMBeanServerConnection();
165             mserver.addNotificationListener(delegateName, dummyListener, null, null);
166 
167             client.close();
168             server.stop();
169         } catch (MalformedURLException e) {
170             System.out.println(">>> Skipping unsupported URL " + u);
171             return true;
172         }
173 
174         return true;
175     }
176 }
177