Search in sources :

Example 21 with SslRMIClientSocketFactory

use of javax.rmi.ssl.SslRMIClientSocketFactory in project jvm-tools by aragozin.

the class JmxConnectionInfo method connectJmx.

@SuppressWarnings("resource")
private MBeanServerConnection connectJmx(String host, int port, Map<String, Object> props) {
    try {
        RMIServer rmiServer = null;
        try {
            if (diagMode) {
                System.out.println("Try to connect via TLS");
            }
            Registry registry = LocateRegistry.getRegistry(host, port, new SslRMIClientSocketFactory());
            try {
                rmiServer = (RMIServer) registry.lookup("jmxrmi");
            } catch (NotBoundException nbe) {
                throw (IOException) new IOException(nbe.getMessage()).initCause(nbe);
            }
        } catch (IOException e) {
            if (diagMode) {
                System.out.println("Failed to connect using TLS: " + e.toString());
                System.out.println("Try to use plain socket");
            }
            Registry registry = LocateRegistry.getRegistry(host, port);
            try {
                rmiServer = (RMIServer) registry.lookup("jmxrmi");
            } catch (NotBoundException nbe) {
                if (diagMode) {
                    System.out.println("Failed using LocateRegistry. Fallback to JMXConnectorFactory");
                }
            }
        }
        if (rmiServer != null) {
            RMIConnector rmiConnector = new RMIConnector(rmiServer, props);
            rmiConnector.connect();
            return rmiConnector.getMBeanServerConnection();
        }
        String proto = System.getProperty("jmx.service.protocol", "rmi");
        final String uri = "rmi".equals(proto) ? "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi" : "service:jmx:" + proto + "://" + host + ":" + port;
        if (diagMode) {
            System.out.println("Using JMX URI: " + uri);
        }
        JMXServiceURL jmxurl = new JMXServiceURL(uri);
        JMXConnector conn = props == null ? JMXConnectorFactory.connect(jmxurl) : JMXConnectorFactory.connect(jmxurl, props);
        MBeanServerConnection mserver = conn.getMBeanServerConnection();
        return mserver;
    } catch (MalformedURLException e) {
        commandHost.fail("JMX Connection failed: " + e.toString(), e);
    } catch (IOException e) {
        commandHost.fail("JMX Connection failed: " + e.toString(), e);
    }
    return null;
}
Also used : SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) JMXServiceURL(javax.management.remote.JMXServiceURL) RMIConnector(javax.management.remote.rmi.RMIConnector) MalformedURLException(java.net.MalformedURLException) RMIServer(javax.management.remote.rmi.RMIServer) NotBoundException(java.rmi.NotBoundException) JMXConnector(javax.management.remote.JMXConnector) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 22 with SslRMIClientSocketFactory

use of javax.rmi.ssl.SslRMIClientSocketFactory in project graphdb by neo4j-attic.

the class HotspotManagementSupport method createServer.

private JMXConnectorServer createServer(int port, boolean useSSL) {
    MBeanServer server = getMBeanServer();
    final JMXServiceURL url;
    try {
        url = new JMXServiceURL("rmi", null, port);
    } catch (MalformedURLException e) {
        log.log(Level.WARNING, "Failed to start JMX Server", e);
        return null;
    }
    Map<String, Object> env = new HashMap<String, Object>();
    if (useSSL) {
        env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new SslRMIClientSocketFactory());
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new SslRMIServerSocketFactory());
    }
    try {
        return JMXConnectorServerFactory.newJMXConnectorServer(url, env, server);
    } catch (IOException e) {
        log.log(Level.WARNING, "Failed to start JMX Server", e);
        return null;
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) SslRMIServerSocketFactory(javax.rmi.ssl.SslRMIServerSocketFactory) IOException(java.io.IOException) MBeanServer(javax.management.MBeanServer)

Aggregations

SslRMIClientSocketFactory (javax.rmi.ssl.SslRMIClientSocketFactory)22 HashMap (java.util.HashMap)15 JMXServiceURL (javax.management.remote.JMXServiceURL)10 SslRMIServerSocketFactory (javax.rmi.ssl.SslRMIServerSocketFactory)10 IOException (java.io.IOException)8 RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)7 RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)7 MBeanServer (javax.management.MBeanServer)4 JMXConnector (javax.management.remote.JMXConnector)4 MalformedURLException (java.net.MalformedURLException)3 MBeanServerConnection (javax.management.MBeanServerConnection)3 SSLContext (javax.net.ssl.SSLContext)3 UnknownHostException (java.net.UnknownHostException)2 LocateRegistry (java.rmi.registry.LocateRegistry)2 Registry (java.rmi.registry.Registry)2 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)2 ObjectName (javax.management.ObjectName)2 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)2 InetAddress (java.net.InetAddress)1 AlreadyBoundException (java.rmi.AlreadyBoundException)1