1 8 9 package test.javax.management.remote.rmi; 10 11 import java.io.IOException ; 12 import java.io.Serializable ; 13 import java.net.MalformedURLException ; 14 import java.net.ServerSocket ; 15 import java.net.Socket ; 16 import java.rmi.server.RMIClientSocketFactory ; 17 import java.rmi.server.RMIServerSocketFactory ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import javax.management.remote.JMXConnector ; 21 import javax.management.remote.JMXConnectorFactory ; 22 import javax.management.remote.JMXConnectorServer ; 23 import javax.management.remote.JMXConnectorServerFactory ; 24 import javax.management.remote.JMXServiceURL ; 25 import javax.management.remote.rmi.RMIConnectorServer ; 26 import javax.naming.Context ; 27 28 import mx4j.tools.naming.NamingService; 29 import test.MutableBoolean; 30 31 34 public class RMIJRMPConnectorTest extends RMIConnectorTestCase 35 { 36 private NamingService naming; 37 38 public RMIJRMPConnectorTest(String s) 39 { 40 super(s); 41 } 42 43 public JMXServiceURL createJMXConnectorServerAddress() throws MalformedURLException 44 { 45 return new JMXServiceURL ("rmi", "localhost", 0); 46 } 47 48 public void startNaming() throws Exception 49 { 50 naming = new NamingService(getNamingPort()); 51 naming.start(); 52 } 53 54 public void stopNaming() throws Exception 55 { 56 naming.stop(); 57 naming = null; 58 Thread.sleep(5000); 59 } 60 61 public int getNamingPort() 62 { 63 return 1099; 64 } 65 66 public Map getEnvironment() 67 { 68 HashMap env = new HashMap (); 69 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); 70 env.put(Context.PROVIDER_URL, "rmi://localhost:" + getNamingPort()); 71 return env; 72 } 73 74 public void testRMIConnectorWithCustomSocketFactories() throws Exception 75 { 76 RMIClientSocketFactory client = new RMICSF(); 77 78 final MutableBoolean serverCheck = new MutableBoolean(false); 79 RMIServerSocketFactory server = new RMISSF(serverCheck); 80 81 JMXServiceURL url = createJMXConnectorServerAddress(); 82 Map env = getEnvironment(); 83 env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, client); 84 env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, server); 85 86 JMXConnectorServer cntorServer = null; 87 JMXConnector cntor = null; 88 89 try 90 { 91 cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, env, newMBeanServer()); 92 cntorServer.start(); 93 94 cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment()); 95 assertTrue(serverCheck.get()); 96 } 97 finally 98 { 99 if (cntor != null) cntor.close(); 100 if (cntorServer != null) cntorServer.stop(); 101 } 102 } 103 104 public static class RMICSF implements RMIClientSocketFactory , Serializable 105 { 106 public Socket createSocket(String host, int port) throws IOException 107 { 108 return new Socket (host, port); 109 } 110 } 111 112 public static class RMISSF implements RMIServerSocketFactory 113 { 114 private MutableBoolean check; 115 116 public RMISSF(MutableBoolean check) 117 { 118 this.check = check; 119 } 120 121 public ServerSocket createServerSocket(int port) throws IOException 122 { 123 check.set(true); 124 return new ServerSocket (port); 125 } 126 } 127 } 128 | Popular Tags |