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;
010:
011: import java.io.ByteArrayInputStream;
012: import java.io.ByteArrayOutputStream;
013: import java.io.IOException;
014: import java.io.ObjectInputStream;
015: import java.io.ObjectOutputStream;
016: import java.net.MalformedURLException;
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021: import javax.management.ListenerNotFoundException;
022: import javax.management.MBeanNotificationInfo;
023: import javax.management.MBeanServer;
024: import javax.management.MBeanServerConnection;
025: import javax.management.Notification;
026: import javax.management.NotificationEmitter;
027: import javax.management.NotificationFilter;
028: import javax.management.NotificationListener;
029: import javax.management.ObjectName;
030: import javax.management.remote.JMXAuthenticator;
031: import javax.management.remote.JMXConnector;
032: import javax.management.remote.JMXConnectorFactory;
033: import javax.management.remote.JMXConnectorServer;
034: import javax.management.remote.JMXConnectorServerFactory;
035: import javax.management.remote.JMXPrincipal;
036: import javax.management.remote.JMXServiceURL;
037: import javax.security.auth.Subject;
038:
039: import test.MX4JTestCase;
040:
041: /**
042: * @version $Revision: 1.21 $
043: */
044: public abstract class JMXConnectorTestCase extends MX4JTestCase {
045: public JMXConnectorTestCase(String name) {
046: super (name);
047: }
048:
049: protected void tearDown() throws Exception {
050: sleep(5000);
051: }
052:
053: public abstract JMXServiceURL createJMXConnectorServerAddress()
054: throws MalformedURLException;
055:
056: public abstract Map getEnvironment();
057:
058: public void testNewJMXConnectorWithNullURL() throws Exception {
059: try {
060: JMXConnectorFactory.connect(null);
061: fail();
062: } catch (NullPointerException x) {
063: }
064: }
065:
066: public void testConnectionId() throws Exception {
067: // Format is:
068: // protocol:[[host]:port] [clientId] [arbitrary]
069: // Spaces are mandatory, brackets indicates optional parts
070:
071: JMXConnectorServer cntorServer = null;
072: JMXConnector cntor = null;
073: try {
074: JMXServiceURL url = createJMXConnectorServerAddress();
075: cntorServer = JMXConnectorServerFactory
076: .newJMXConnectorServer(url, getEnvironment(),
077: newMBeanServer());
078: cntorServer.start();
079: sleep(5000);
080:
081: cntor = JMXConnectorFactory.connect(cntorServer
082: .getAddress(), getEnvironment());
083: String connectionId = cntor.getConnectionId();
084: String protocol = connectionId.substring(0, connectionId
085: .indexOf(':'));
086: assertEquals(protocol, url.getProtocol());
087:
088: // Match first mandatory space
089: int space = connectionId.indexOf(' ');
090: String remaining = connectionId.substring(space + 1);
091: // Match second mandatory space
092: space = remaining.indexOf(' ');
093: String arbitrary = remaining.substring(space + 1);
094: if (arbitrary.length() < 1)
095: fail("Missing MX4J arbitrary test");
096: } finally {
097: if (cntor != null)
098: cntor.close();
099: if (cntorServer != null)
100: cntorServer.stop();
101: }
102: }
103:
104: public void testConnectionWithNoPath() throws Exception {
105: JMXConnectorServer cntorServer = null;
106: JMXConnector cntor = null;
107: try {
108: JMXServiceURL url = createJMXConnectorServerAddress();
109: cntorServer = JMXConnectorServerFactory
110: .newJMXConnectorServer(url, getEnvironment(),
111: newMBeanServer());
112: cntorServer.start();
113: sleep(5000);
114:
115: cntor = JMXConnectorFactory.connect(cntorServer
116: .getAddress(), getEnvironment());
117: } finally {
118: if (cntor != null)
119: cntor.close();
120: if (cntorServer != null)
121: cntorServer.stop();
122: }
123: }
124:
125: public void testJMXAuthenticator() throws Exception {
126: final String password = "mx4j";
127: JMXAuthenticator authenticator = new JMXAuthenticator() {
128: public Subject authenticate(Object credentials)
129: throws SecurityException {
130: if (password.equals(credentials)) {
131: JMXPrincipal principal = new JMXPrincipal("mx4j");
132: Subject subject = new Subject();
133: subject.getPrincipals().add(principal);
134: subject.setReadOnly();
135: return subject;
136: }
137: throw new SecurityException("Authentication Failed");
138: }
139: };
140:
141: JMXConnectorServer cntorServer = null;
142: JMXConnector cntor = null;
143: try {
144: JMXServiceURL url = createJMXConnectorServerAddress();
145: MBeanServer server = newMBeanServer();
146: Map serverEnv = getEnvironment();
147: serverEnv.put(JMXConnectorServer.AUTHENTICATOR,
148: authenticator);
149: cntorServer = JMXConnectorServerFactory
150: .newJMXConnectorServer(url, serverEnv, server);
151: cntorServer.start();
152: sleep(5000);
153:
154: // Try to provide wrong password
155: Map clientEnv = getEnvironment();
156: try {
157: testJMXAuthenticatorConnect(cntorServer.getAddress(),
158: clientEnv);
159: fail();
160: } catch (SecurityException x) {
161: }
162:
163: // Try now with a correct password
164: clientEnv.put(JMXConnector.CREDENTIALS, password);
165: testJMXAuthenticatorConnect(cntorServer.getAddress(),
166: clientEnv);
167: } finally {
168: if (cntor != null)
169: cntor.close();
170: if (cntorServer != null)
171: cntorServer.stop();
172: }
173: }
174:
175: protected void testJMXAuthenticatorConnect(JMXServiceURL url,
176: Map environment) throws SecurityException, IOException {
177: JMXConnectorFactory.connect(url, environment);
178: }
179:
180: public void testStopServerBeforeClosingClient() throws Exception {
181: JMXServiceURL url = createJMXConnectorServerAddress();
182: JMXConnectorServer cntorServer = JMXConnectorServerFactory
183: .newJMXConnectorServer(url, getEnvironment(),
184: newMBeanServer());
185: cntorServer.start();
186: sleep(5000);
187:
188: JMXConnector cntor = JMXConnectorFactory.connect(cntorServer
189: .getAddress(), getEnvironment());
190: MBeanServerConnection mbsc = cntor.getMBeanServerConnection();
191:
192: cntorServer.stop();
193:
194: try {
195: mbsc.getDefaultDomain();
196: fail();
197: } catch (IOException x) {
198: }
199: }
200:
201: public void testStopServerAndCloseClientThenInvoke()
202: throws Exception {
203: JMXServiceURL url = createJMXConnectorServerAddress();
204: JMXConnectorServer cntorServer = JMXConnectorServerFactory
205: .newJMXConnectorServer(url, getEnvironment(),
206: newMBeanServer());
207: cntorServer.start();
208: sleep(5000);
209:
210: JMXConnector cntor = JMXConnectorFactory.connect(cntorServer
211: .getAddress(), getEnvironment());
212: MBeanServerConnection mbsc = cntor.getMBeanServerConnection();
213:
214: cntor.close();
215: cntorServer.stop();
216:
217: try {
218: mbsc.getDefaultDomain();
219: fail();
220: } catch (IOException x) {
221: }
222: }
223:
224: public void testSerializedConnectorCanConnect() throws Exception {
225: JMXConnectorServer cntorServer = null;
226: JMXConnector cntor = null;
227: try {
228: JMXServiceURL url = createJMXConnectorServerAddress();
229: cntorServer = JMXConnectorServerFactory
230: .newJMXConnectorServer(url, getEnvironment(),
231: newMBeanServer());
232: cntorServer.start();
233: sleep(5000);
234:
235: cntor = JMXConnectorFactory.newJMXConnector(cntorServer
236: .getAddress(), getEnvironment());
237:
238: // Serialize it: we want to test serialization does no reset data members
239: ByteArrayOutputStream baos = new ByteArrayOutputStream();
240: ObjectOutputStream oos = new ObjectOutputStream(baos);
241: oos.writeObject(cntor);
242: oos.close();
243: ByteArrayInputStream bais = new ByteArrayInputStream(baos
244: .toByteArray());
245: ObjectInputStream ois = new ObjectInputStream(bais);
246: cntor = (JMXConnector) ois.readObject();
247: ois.close();
248:
249: cntor.connect();
250: MBeanServerConnection mbsc = cntor
251: .getMBeanServerConnection();
252: mbsc.getDefaultDomain();
253:
254: // Again
255: cntor = JMXConnectorFactory.connect(cntorServer
256: .getAddress(), getEnvironment());
257:
258: // Serialize it: we want to test serialization does no reset data members
259: baos = new ByteArrayOutputStream();
260: oos = new ObjectOutputStream(baos);
261: oos.writeObject(cntor);
262: oos.close();
263: bais = new ByteArrayInputStream(baos.toByteArray());
264: ois = new ObjectInputStream(bais);
265: cntor = (JMXConnector) ois.readObject();
266: ois.close();
267:
268: cntor.connect();
269: mbsc = cntor.getMBeanServerConnection();
270: mbsc.getDefaultDomain();
271: } finally {
272: if (cntor != null)
273: cntor.close();
274: if (cntorServer != null)
275: cntorServer.stop();
276: }
277: }
278:
279: public void testDefaultClassLoader() throws Exception {
280: JMXServiceURL url = createJMXConnectorServerAddress();
281: Map environment = new HashMap(getEnvironment());
282: environment.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER,
283: new Object());
284: try {
285: JMXConnectorFactory.newJMXConnector(url, environment);
286: fail();
287: } catch (IllegalArgumentException x) {
288: }
289:
290: JMXConnector cntor = JMXConnectorFactory.newJMXConnector(url,
291: getEnvironment());
292: try {
293: cntor.connect(environment);
294: fail();
295: } catch (IllegalArgumentException x) {
296: }
297: }
298:
299: public void testListenersAreRemovedOnConnectorClose()
300: throws Exception {
301: JMXConnectorServer cntorServer = null;
302: JMXConnector cntor = null;
303: try {
304: JMXServiceURL url = createJMXConnectorServerAddress();
305: MBeanServer server = newMBeanServer();
306: cntorServer = JMXConnectorServerFactory
307: .newJMXConnectorServer(url, getEnvironment(),
308: server);
309: cntorServer.start();
310: sleep(5000);
311:
312: Emitter emitter = new Emitter();
313: ObjectName emitterName = ObjectName
314: .getInstance(":name=emitter");
315: server.registerMBean(emitter, emitterName);
316:
317: cntor = JMXConnectorFactory.connect(cntorServer
318: .getAddress(), getEnvironment());
319: MBeanServerConnection mbsc = cntor
320: .getMBeanServerConnection();
321:
322: NotificationListener listener = new NotificationListener() {
323: public void handleNotification(
324: Notification notification, Object handback) {
325: }
326: };
327:
328: // Add the listener and be sure the mechanism of removal works fine
329: mbsc.addNotificationListener(emitterName, listener, null,
330: null);
331: assertEquals(emitter.getSize(), 1);
332: mbsc.removeNotificationListener(emitterName, listener,
333: null, null);
334: assertEquals(emitter.getSize(), 0);
335:
336: // Add the listener and close the connector
337: mbsc.addNotificationListener(emitterName, listener, null,
338: null);
339: assertEquals(emitter.getSize(), 1);
340: cntor.close();
341: assertEquals(emitter.getSize(), 0);
342: } finally {
343: if (cntorServer != null)
344: cntorServer.stop();
345: }
346: }
347:
348: public void testConnectWithProviderClassLoader() throws Exception {
349: JMXConnectorServer cntorServer = null;
350: JMXConnector cntor = null;
351: try {
352: JMXServiceURL url = createJMXConnectorServerAddress();
353: MBeanServer server = newMBeanServer();
354: Map serverEnv = getEnvironment();
355: serverEnv
356: .put(
357: JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER,
358: getClass().getClassLoader());
359: ClassLoader old = Thread.currentThread()
360: .getContextClassLoader();
361: Thread.currentThread().setContextClassLoader(
362: getClass().getClassLoader().getParent());
363: cntorServer = JMXConnectorServerFactory
364: .newJMXConnectorServer(url, serverEnv, server);
365: cntorServer.start();
366: Thread.currentThread().setContextClassLoader(old);
367: sleep(5000);
368:
369: cntor = JMXConnectorFactory.connect(cntorServer
370: .getAddress(), getEnvironment());
371: MBeanServerConnection mbsc = cntor
372: .getMBeanServerConnection();
373: assertNotNull(mbsc);
374: } finally {
375: if (cntor != null)
376: cntor.close();
377: if (cntorServer != null)
378: cntorServer.stop();
379: }
380: }
381:
382: public interface EmitterMBean {
383: }
384:
385: private static class Emitter implements NotificationEmitter,
386: EmitterMBean {
387: private List listeners = new ArrayList();
388:
389: public void addNotificationListener(
390: NotificationListener listener,
391: NotificationFilter filter, Object handback)
392: throws IllegalArgumentException {
393: listeners.add(listener);
394: }
395:
396: public void removeNotificationListener(
397: NotificationListener listener,
398: NotificationFilter filter, Object handback)
399: throws ListenerNotFoundException {
400: listeners.remove(listener);
401: }
402:
403: public MBeanNotificationInfo[] getNotificationInfo() {
404: return new MBeanNotificationInfo[0];
405: }
406:
407: public void removeNotificationListener(
408: NotificationListener listener)
409: throws ListenerNotFoundException {
410: }
411:
412: public int getSize() {
413: return listeners.size();
414: }
415: }
416: }
|