2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6227124
|
|
27 |
* @summary Test that setting an MBeanServerForwarder on an already
|
|
28 |
* started RMI connector server has the expected behavior.
|
|
29 |
* @author Luis-Miguel Alventosa
|
|
30 |
* @run clean MBSFPreStartPostStartTest
|
|
31 |
* @run build MBSFPreStartPostStartTest
|
|
32 |
* @run main MBSFPreStartPostStartTest
|
|
33 |
*/
|
|
34 |
|
|
35 |
import java.lang.reflect.InvocationHandler;
|
|
36 |
import java.lang.reflect.Method;
|
|
37 |
import java.lang.reflect.Proxy;
|
|
38 |
import javax.management.MBeanServer;
|
|
39 |
import javax.management.MBeanServerConnection;
|
|
40 |
import javax.management.MBeanServerFactory;
|
|
41 |
import javax.management.remote.JMXConnector;
|
|
42 |
import javax.management.remote.JMXConnectorServer;
|
|
43 |
import javax.management.remote.JMXConnectorServerFactory;
|
|
44 |
import javax.management.remote.JMXServiceURL;
|
|
45 |
import javax.management.remote.MBeanServerForwarder;
|
|
46 |
|
|
47 |
public class MBSFPreStartPostStartTest {
|
|
48 |
|
|
49 |
public static class MBSFInvocationHandler implements InvocationHandler {
|
|
50 |
|
|
51 |
public static MBeanServerForwarder newProxyInstance() {
|
|
52 |
|
|
53 |
final InvocationHandler handler = new MBSFInvocationHandler();
|
|
54 |
|
|
55 |
final Class[] interfaces =
|
|
56 |
new Class[] {MBeanServerForwarder.class};
|
|
57 |
|
|
58 |
Object proxy = Proxy.newProxyInstance(
|
|
59 |
MBeanServerForwarder.class.getClassLoader(),
|
|
60 |
interfaces,
|
|
61 |
handler);
|
|
62 |
|
|
63 |
return MBeanServerForwarder.class.cast(proxy);
|
|
64 |
}
|
|
65 |
|
|
66 |
public Object invoke(Object proxy, Method method, Object[] args)
|
|
67 |
throws Throwable {
|
|
68 |
|
|
69 |
final String methodName = method.getName();
|
|
70 |
|
|
71 |
if (methodName.equals("getMBeanServer")) {
|
|
72 |
return mbs;
|
|
73 |
}
|
|
74 |
|
|
75 |
if (methodName.equals("setMBeanServer")) {
|
|
76 |
if (args[0] == null)
|
|
77 |
throw new IllegalArgumentException("Null MBeanServer");
|
|
78 |
if (mbs != null)
|
|
79 |
throw new IllegalArgumentException("MBeanServer object " +
|
|
80 |
"already initialized");
|
|
81 |
mbs = (MBeanServer) args[0];
|
|
82 |
return null;
|
|
83 |
}
|
|
84 |
|
|
85 |
flag = true;
|
|
86 |
|
|
87 |
return method.invoke(mbs, args);
|
|
88 |
}
|
|
89 |
|
|
90 |
public boolean getFlag() {
|
|
91 |
return flag;
|
|
92 |
}
|
|
93 |
|
|
94 |
public void setFlag(boolean flag) {
|
|
95 |
this.flag = flag;
|
|
96 |
}
|
|
97 |
|
|
98 |
private boolean flag;
|
|
99 |
private MBeanServer mbs;
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Run test
|
|
104 |
*/
|
|
105 |
public int runTest(boolean setBeforeStart) throws Exception {
|
|
106 |
|
|
107 |
echo("=-=-= MBSFPreStartPostStartTest: Set MBSF " +
|
|
108 |
(setBeforeStart ? "before" : "after") +
|
|
109 |
" starting the connector server =-=-=");
|
|
110 |
|
|
111 |
JMXConnectorServer server = null;
|
|
112 |
JMXConnector client = null;
|
|
113 |
|
|
114 |
// Create a new MBeanServer
|
|
115 |
//
|
|
116 |
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
|
|
117 |
|
|
118 |
try {
|
|
119 |
// Create the JMXServiceURL
|
|
120 |
//
|
|
121 |
final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
|
|
122 |
|
|
123 |
// Create a JMXConnectorServer
|
|
124 |
//
|
|
125 |
server = JMXConnectorServerFactory.newJMXConnectorServer(url,
|
|
126 |
null,
|
|
127 |
mbs);
|
|
128 |
|
|
129 |
// Create MBeanServerForwarder
|
|
130 |
//
|
|
131 |
MBeanServerForwarder mbsf =
|
|
132 |
MBSFInvocationHandler.newProxyInstance();
|
|
133 |
|
|
134 |
// Set MBeanServerForwarder before start()
|
|
135 |
//
|
|
136 |
if (setBeforeStart)
|
|
137 |
server.setMBeanServerForwarder(mbsf);
|
|
138 |
|
|
139 |
// Start the JMXConnectorServer
|
|
140 |
//
|
|
141 |
server.start();
|
|
142 |
|
|
143 |
// Set MBeanServerForwarder after start()
|
|
144 |
//
|
|
145 |
if (!setBeforeStart)
|
|
146 |
server.setMBeanServerForwarder(mbsf);
|
|
147 |
|
|
148 |
// Create a JMXConnector
|
|
149 |
//
|
|
150 |
client = server.toJMXConnector(null);
|
|
151 |
|
|
152 |
// Connect to the connector server
|
|
153 |
//
|
|
154 |
client.connect(null);
|
|
155 |
|
|
156 |
// Get non-secure MBeanServerConnection
|
|
157 |
//
|
|
158 |
final MBeanServerConnection mbsc =
|
|
159 |
client.getMBeanServerConnection();
|
|
160 |
|
|
161 |
// Run method
|
|
162 |
//
|
|
163 |
mbsc.getDefaultDomain();
|
|
164 |
|
|
165 |
// Check flag in MBeanServerForwarder
|
|
166 |
//
|
|
167 |
MBSFInvocationHandler mbsfih =
|
|
168 |
(MBSFInvocationHandler) Proxy.getInvocationHandler(mbsf);
|
|
169 |
if (mbsfih.getFlag() == true) {
|
|
170 |
echo("OK: Did go into MBeanServerForwarder!");
|
|
171 |
} else {
|
|
172 |
echo("KO: Didn't go into MBeanServerForwarder!");
|
|
173 |
return 1;
|
|
174 |
}
|
|
175 |
} catch (Exception e) {
|
|
176 |
echo("Failed to perform operation: " + e);
|
|
177 |
return 1;
|
|
178 |
} finally {
|
|
179 |
// Close the connection
|
|
180 |
//
|
|
181 |
if (client != null)
|
|
182 |
client.close();
|
|
183 |
|
|
184 |
// Stop the connector server
|
|
185 |
//
|
|
186 |
if (server != null)
|
|
187 |
server.stop();
|
|
188 |
|
|
189 |
// Release the MBeanServer
|
|
190 |
//
|
|
191 |
if (mbs != null)
|
|
192 |
MBeanServerFactory.releaseMBeanServer(mbs);
|
|
193 |
}
|
|
194 |
|
|
195 |
return 0;
|
|
196 |
}
|
|
197 |
|
|
198 |
/*
|
|
199 |
* Print message
|
|
200 |
*/
|
|
201 |
private static void echo(String message) {
|
|
202 |
System.out.println(message);
|
|
203 |
}
|
|
204 |
|
|
205 |
/*
|
|
206 |
* Standalone entry point.
|
|
207 |
*
|
|
208 |
* Run the test and report to stdout.
|
|
209 |
*/
|
|
210 |
public static void main (String args[]) throws Exception {
|
|
211 |
|
|
212 |
int error = 0;
|
|
213 |
|
|
214 |
MBSFPreStartPostStartTest test = new MBSFPreStartPostStartTest();
|
|
215 |
|
|
216 |
// Set MBSF before start()
|
|
217 |
//
|
|
218 |
error += test.runTest(true);
|
|
219 |
// Set MBSF after start()
|
|
220 |
//
|
|
221 |
error += test.runTest(false);
|
|
222 |
|
|
223 |
// Check test results
|
|
224 |
//
|
|
225 |
if (error > 0) {
|
|
226 |
echo(">>> Unhappy Bye, Bye!");
|
|
227 |
throw new IllegalStateException(
|
|
228 |
"Test FAILED: Unexpected error!");
|
|
229 |
} else {
|
|
230 |
echo(">>> Happy Bye, Bye!");
|
|
231 |
}
|
|
232 |
}
|
|
233 |
}
|