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.io.IOException;
012: import java.io.Serializable;
013: import java.net.MalformedURLException;
014: import java.net.ServerSocket;
015: import java.net.Socket;
016: import java.rmi.server.RMIClientSocketFactory;
017: import java.rmi.server.RMIServerSocketFactory;
018: import java.util.HashMap;
019: import java.util.Map;
020: import javax.management.remote.JMXConnector;
021: import javax.management.remote.JMXConnectorFactory;
022: import javax.management.remote.JMXConnectorServer;
023: import javax.management.remote.JMXConnectorServerFactory;
024: import javax.management.remote.JMXServiceURL;
025: import javax.management.remote.rmi.RMIConnectorServer;
026: import javax.naming.Context;
027:
028: import mx4j.tools.naming.NamingService;
029: import test.MutableBoolean;
030:
031: /**
032: * @version $Revision: 1.9 $
033: */
034: public class RMIJRMPConnectorTest extends RMIConnectorTestCase {
035: private NamingService naming;
036:
037: public RMIJRMPConnectorTest(String s) {
038: super (s);
039: }
040:
041: public JMXServiceURL createJMXConnectorServerAddress()
042: throws MalformedURLException {
043: return new JMXServiceURL("rmi", "localhost", 0);
044: }
045:
046: public void startNaming() throws Exception {
047: naming = new NamingService(getNamingPort());
048: naming.start();
049: }
050:
051: public void stopNaming() throws Exception {
052: naming.stop();
053: naming = null;
054: Thread.sleep(5000);
055: }
056:
057: public int getNamingPort() {
058: return 1099;
059: }
060:
061: public Map getEnvironment() {
062: HashMap env = new HashMap();
063: env.put(Context.INITIAL_CONTEXT_FACTORY,
064: "com.sun.jndi.rmi.registry.RegistryContextFactory");
065: env.put(Context.PROVIDER_URL, "rmi://localhost:"
066: + getNamingPort());
067: return env;
068: }
069:
070: public void testRMIConnectorWithCustomSocketFactories()
071: throws Exception {
072: RMIClientSocketFactory client = new RMICSF();
073:
074: final MutableBoolean serverCheck = new MutableBoolean(false);
075: RMIServerSocketFactory server = new RMISSF(serverCheck);
076:
077: JMXServiceURL url = createJMXConnectorServerAddress();
078: Map env = getEnvironment();
079: env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
080: client);
081: env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE,
082: server);
083:
084: JMXConnectorServer cntorServer = null;
085: JMXConnector cntor = null;
086:
087: try {
088: cntorServer = JMXConnectorServerFactory
089: .newJMXConnectorServer(url, env, newMBeanServer());
090: cntorServer.start();
091:
092: cntor = JMXConnectorFactory.connect(cntorServer
093: .getAddress(), getEnvironment());
094: assertTrue(serverCheck.get());
095: } finally {
096: if (cntor != null)
097: cntor.close();
098: if (cntorServer != null)
099: cntorServer.stop();
100: }
101: }
102:
103: public static class RMICSF implements RMIClientSocketFactory,
104: Serializable {
105: public Socket createSocket(String host, int port)
106: throws IOException {
107: return new Socket(host, port);
108: }
109: }
110:
111: public static class RMISSF implements RMIServerSocketFactory {
112: private MutableBoolean check;
113:
114: public RMISSF(MutableBoolean check) {
115: this .check = check;
116: }
117:
118: public ServerSocket createServerSocket(int port)
119: throws IOException {
120: check.set(true);
121: return new ServerSocket(port);
122: }
123: }
124: }
|