001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.remote.rmi;
010:
011: import java.lang.reflect.Constructor;
012: import java.lang.reflect.Method;
013: import java.util.Map;
014: import javax.management.MBeanInfo;
015: import javax.management.MBeanServer;
016: import javax.management.MBeanServerConnection;
017: import javax.management.MBeanServerFactory;
018: import javax.management.Notification;
019: import javax.management.NotificationFilter;
020: import javax.management.NotificationListener;
021: import javax.management.ObjectName;
022: import javax.management.loading.MLet;
023: import javax.management.remote.JMXConnector;
024: import javax.management.remote.JMXConnectorFactory;
025: import javax.management.remote.JMXConnectorServer;
026: import javax.management.remote.JMXConnectorServerFactory;
027: import javax.management.remote.JMXServiceURL;
028:
029: import test.MX4JTestCase;
030: import test.MutableObject;
031:
032: /**
033: * @version $Revision: 1.4 $
034: */
035: public class RMIInteroperabilityTest extends MX4JTestCase {
036: public RMIInteroperabilityTest(String s) {
037: super (s);
038: }
039:
040: private Object startJMXRIRMIConnectorServer() throws Exception {
041: ClassLoader ri = createRemoteJMXRIWithTestsClassLoader();
042:
043: Class mbeanServerFactoryClass = ri
044: .loadClass(MBeanServerFactory.class.getName());
045: Method method = mbeanServerFactoryClass.getMethod(
046: "newMBeanServer", new Class[0]);
047: Object mbeanServer = method.invoke(null, new Object[0]);
048: if (!mbeanServer.getClass().getName().startsWith("com.sun"))
049: fail();
050: Class mbeanServerClass = ri.loadClass(MBeanServer.class
051: .getName());
052:
053: Class serviceURLClass = ri.loadClass(JMXServiceURL.class
054: .getName());
055: Constructor constructor = serviceURLClass
056: .getConstructor(new Class[] { String.class });
057: Object serviceURL = constructor
058: .newInstance(new Object[] { "service:jmx:rmi://localhost" });
059: Class cntorServerFactoryClass = ri
060: .loadClass(JMXConnectorServerFactory.class.getName());
061: method = cntorServerFactoryClass.getMethod(
062: "newJMXConnectorServer", new Class[] { serviceURLClass,
063: Map.class, mbeanServerClass });
064: Object cntorServer = method.invoke(null, new Object[] {
065: serviceURL, null, mbeanServer });
066:
067: method = cntorServer.getClass()
068: .getMethod("start", new Class[0]);
069: method.invoke(cntorServer, new Object[0]);
070:
071: return cntorServer;
072: }
073:
074: public void testJMXRIOnServerMX4JOnClientGetMBeanInfo()
075: throws Exception {
076: Object cntorServer = startJMXRIRMIConnectorServer();
077: Method method = cntorServer.getClass().getMethod("getAddress",
078: new Class[0]);
079: Object address = method.invoke(cntorServer, new Object[0]);
080: String url = address.toString();
081:
082: JMXConnector cntor = JMXConnectorFactory
083: .connect(new JMXServiceURL(url));
084: MBeanServerConnection cntion = cntor.getMBeanServerConnection();
085:
086: MBeanInfo info = cntion.getMBeanInfo(ObjectName.getInstance(
087: "JMImplementation", "type", "MBeanServerDelegate"));
088: assertNotNull(info);
089:
090: cntor.close();
091:
092: method = cntorServer.getClass().getMethod("stop", new Class[0]);
093: method.invoke(cntorServer, new Object[0]);
094: sleep(2000);
095: }
096:
097: public void testJMXRIOnServerMX4JOnClientNotificationReceiving()
098: throws Exception {
099: Object cntorServer = startJMXRIRMIConnectorServer();
100: Method method = cntorServer.getClass().getMethod("getAddress",
101: new Class[0]);
102: Object address = method.invoke(cntorServer, new Object[0]);
103: String url = address.toString();
104:
105: JMXConnector cntor = JMXConnectorFactory
106: .connect(new JMXServiceURL(url));
107: MBeanServerConnection cntion = cntor.getMBeanServerConnection();
108:
109: InteroperabilityListener listener = new InteroperabilityListener();
110: ObjectName delegateName = ObjectName
111: .getInstance("JMImplementation:type=MBeanServerDelegate");
112: cntion.addNotificationListener(delegateName, listener, null,
113: null);
114: sleep(1000);
115:
116: ObjectName name = ObjectName.getInstance(":mbean=mlet");
117: cntion.createMBean(MLet.class.getName(), name, null);
118: sleep(1000);
119: Notification notification = (Notification) listener.get();
120: assertNotNull(notification);
121: listener.reset();
122:
123: cntion.removeNotificationListener(delegateName, listener);
124:
125: cntion.unregisterMBean(name);
126: sleep(1000);
127:
128: notification = (Notification) listener.get();
129: assertNull(notification);
130:
131: cntor.close();
132:
133: method = cntorServer.getClass().getMethod("stop", new Class[0]);
134: method.invoke(cntorServer, new Object[0]);
135: sleep(2000);
136: }
137:
138: private Object connectJMXRIConnector(ClassLoader ri, String url)
139: throws Exception {
140: Class serviceURLClass = ri.loadClass(JMXServiceURL.class
141: .getName());
142: Constructor constructor = serviceURLClass
143: .getConstructor(new Class[] { String.class });
144: Object serviceURL = constructor
145: .newInstance(new Object[] { url });
146: Class cntorFactoryClass = ri
147: .loadClass(JMXConnectorFactory.class.getName());
148: Method method = cntorFactoryClass.getMethod("connect",
149: new Class[] { serviceURLClass });
150: return method.invoke(null, new Object[] { serviceURL });
151: }
152:
153: public void testMX4JOnServerJMXRIOnClientGetMBeanInfo()
154: throws Exception {
155: MBeanServer server = newMBeanServer();
156: JMXServiceURL url = new JMXServiceURL(
157: "service:jmx:rmi://localhost");
158: JMXConnectorServer cntorServer = JMXConnectorServerFactory
159: .newJMXConnectorServer(url, null, server);
160: cntorServer.start();
161: url = cntorServer.getAddress();
162:
163: ClassLoader ri = createRemoteJMXRIWithTestsClassLoader();
164: ClassLoader tccl = Thread.currentThread()
165: .getContextClassLoader();
166: Thread.currentThread().setContextClassLoader(ri);
167: Object cntor = connectJMXRIConnector(ri, url.toString());
168:
169: Method method = cntor.getClass().getMethod(
170: "getMBeanServerConnection", new Class[0]);
171: Object cntion = method.invoke(cntor, new Object[0]);
172: Class cntionClass = ri.loadClass(MBeanServerConnection.class
173: .getName());
174:
175: Class objectNameClass = ri
176: .loadClass(ObjectName.class.getName());
177: method = objectNameClass.getMethod("getInstance",
178: new Class[] { String.class });
179: Object objectName = method
180: .invoke(
181: null,
182: new Object[] { "JMImplementation:type=MBeanServerDelegate" });
183: method = cntionClass.getMethod("getMBeanInfo",
184: new Class[] { objectNameClass });
185: Object info = method
186: .invoke(cntion, new Object[] { objectName });
187: assertNotNull(info);
188:
189: method = cntor.getClass().getMethod("close", new Class[0]);
190: method.invoke(cntor, new Object[0]);
191:
192: Thread.currentThread().setContextClassLoader(tccl);
193: cntorServer.stop();
194: sleep(2000);
195: }
196:
197: public void testMX4JOnServerJMXRIOnClientNotificationReceiving()
198: throws Exception {
199: MBeanServer server = newMBeanServer();
200: JMXServiceURL url = new JMXServiceURL(
201: "service:jmx:rmi://localhost");
202: JMXConnectorServer cntorServer = JMXConnectorServerFactory
203: .newJMXConnectorServer(url, null, server);
204: cntorServer.start();
205: url = cntorServer.getAddress();
206:
207: ClassLoader ri = createRemoteJMXRIWithTestsClassLoader();
208: ClassLoader tccl = Thread.currentThread()
209: .getContextClassLoader();
210: Thread.currentThread().setContextClassLoader(ri);
211: Object cntor = connectJMXRIConnector(ri, url.toString());
212:
213: Method method = cntor.getClass().getMethod(
214: "getMBeanServerConnection", new Class[0]);
215: Object cntion = method.invoke(cntor, new Object[0]);
216: Class cntionClass = ri.loadClass(MBeanServerConnection.class
217: .getName());
218:
219: Class objectNameClass = ri
220: .loadClass(ObjectName.class.getName());
221: method = objectNameClass.getMethod("getInstance",
222: new Class[] { String.class });
223: Object delegateName = method
224: .invoke(
225: null,
226: new Object[] { "JMImplementation:type=MBeanServerDelegate" });
227: Class notificationListenerClass = ri
228: .loadClass(NotificationListener.class.getName());
229: Class notificationFilterClass = ri
230: .loadClass(NotificationFilter.class.getName());
231: Object listener = ri.loadClass(
232: InteroperabilityListener.class.getName()).newInstance();
233: method = cntionClass.getMethod("addNotificationListener",
234: new Class[] { objectNameClass,
235: notificationListenerClass,
236: notificationFilterClass, Object.class });
237: method.invoke(cntion, new Object[] { delegateName, listener,
238: null, null });
239: sleep(1000);
240:
241: method = objectNameClass.getMethod("getInstance",
242: new Class[] { String.class });
243: Object mletName = method.invoke(null,
244: new Object[] { ":mbean=mlet" });
245:
246: method = cntionClass.getMethod("createMBean", new Class[] {
247: String.class, objectNameClass, objectNameClass });
248: method.invoke(cntion, new Object[] { MLet.class.getName(),
249: mletName, null });
250: sleep(1000);
251:
252: method = listener.getClass().getMethod("get", new Class[0]);
253: Object notification = method.invoke(listener, new Object[0]);
254: assertNotNull(notification);
255:
256: method = listener.getClass().getMethod("reset", new Class[0]);
257: method.invoke(listener, new Object[0]);
258:
259: method = cntionClass.getMethod("removeNotificationListener",
260: new Class[] { objectNameClass,
261: notificationListenerClass });
262: method.invoke(cntion, new Object[] { delegateName, listener });
263:
264: method = cntionClass.getMethod("unregisterMBean",
265: new Class[] { objectNameClass });
266: method.invoke(cntion, new Object[] { mletName });
267: sleep(1000);
268:
269: method = listener.getClass().getMethod("get", new Class[0]);
270: notification = method.invoke(listener, new Object[0]);
271: assertNull(notification);
272:
273: method = cntor.getClass().getMethod("close", new Class[0]);
274: method.invoke(cntor, new Object[0]);
275:
276: Thread.currentThread().setContextClassLoader(tccl);
277: cntorServer.stop();
278: sleep(2000);
279: }
280:
281: public static class InteroperabilityListener implements
282: NotificationListener {
283: private final MutableObject holder;
284:
285: public InteroperabilityListener() {
286: this .holder = new MutableObject(null);
287: }
288:
289: public void handleNotification(Notification notification,
290: Object handback) {
291: holder.set(notification);
292: }
293:
294: public void reset() {
295: holder.set(null);
296: }
297:
298: public Object get() {
299: return holder.get();
300: }
301: }
302: }
|