1 8 9 package mx4j.examples.remote.rmi.ssl; 10 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.security.KeyStore ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 18 import javax.management.ObjectName ; 19 import javax.management.remote.JMXConnectorServer ; 20 import javax.management.remote.JMXConnectorServerFactory ; 21 import javax.management.remote.JMXServiceURL ; 22 import javax.management.remote.rmi.RMIConnectorServer ; 23 import javax.net.ssl.KeyManagerFactory; 24 import javax.net.ssl.SSLContext; 25 26 import mx4j.tools.remote.rmi.SSLRMIClientSocketFactory; 27 import mx4j.tools.remote.rmi.SSLRMIServerSocketFactory; 28 29 57 public class Server 58 { 59 public static void main(String [] args) throws Exception 60 { 61 MBeanServer server = MBeanServerFactory.createMBeanServer(); 62 63 ObjectName namingName = ObjectName.getInstance("naming:type=rmiregistry"); 65 server.createMBean("mx4j.tools.naming.NamingService", namingName, null); 66 server.invoke(namingName, "start", null, null); 67 int namingPort = ((Integer )server.getAttribute(namingName, "Port")).intValue(); 68 69 String jndiPath = "/ssljmxconnector"; 70 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost/jndi/rmi://localhost:" + namingPort + jndiPath); 71 72 Map environment = new HashMap (); 74 SSLContext context = createSSLContext(); 75 environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new SSLRMIClientSocketFactory()); 76 environment.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new SSLRMIServerSocketFactory(context)); 77 78 JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, null); 80 ObjectName connectorServerName = ObjectName.getInstance("connectors:protocol=" + url.getProtocol()); 81 server.registerMBean(connectorServer, connectorServerName); 82 connectorServer.start(); 83 84 System.out.println("Server up and running"); 85 } 86 87 91 private static SSLContext createSSLContext() throws Exception 92 { 93 String keystoreName = "key.store"; 94 String keystorePassword = "storepwd"; 95 96 KeyStore keystore = KeyStore.getInstance("JKS"); 97 InputStream keystoreStream = Server.class.getClassLoader().getResourceAsStream(keystoreName); 98 if (keystoreStream == null) throw new IOException ("Cannot find KeyStore " + keystoreName + " in classpath"); 100 keystore.load(keystoreStream, keystorePassword.toCharArray()); 101 102 KeyManagerFactory keyFactory = KeyManagerFactory.getInstance("SunX509"); 103 keyFactory.init(keystore, keystorePassword.toCharArray()); 104 105 SSLContext context = SSLContext.getInstance("TLS"); 106 context.init(keyFactory.getKeyManagers(), null, null); 107 108 return context; 109 } 110 } 111 | Popular Tags |