Search in sources :

Example 21 with RMIServerSocketFactory

use of java.rmi.server.RMIServerSocketFactory in project hbase by apache.

the class JMXListener method startConnectorServer.

public void startConnectorServer(int rmiRegistryPort, int rmiConnectorPort) throws IOException {
    boolean rmiSSL = false;
    boolean authenticate = true;
    String passwordFile = null;
    String accessFile = null;
    System.setProperty("java.rmi.server.randomIDs", "true");
    String rmiSSLValue = System.getProperty("com.sun.management.jmxremote.ssl", "false");
    rmiSSL = Boolean.parseBoolean(rmiSSLValue);
    String authenticateValue = System.getProperty("com.sun.management.jmxremote.authenticate", "false");
    authenticate = Boolean.parseBoolean(authenticateValue);
    passwordFile = System.getProperty("com.sun.management.jmxremote.password.file");
    accessFile = System.getProperty("com.sun.management.jmxremote.access.file");
    LOG.info("rmiSSL:" + rmiSSLValue + ",authenticate:" + authenticateValue + ",passwordFile:" + passwordFile + ",accessFile:" + accessFile);
    // Environment map
    HashMap<String, Object> jmxEnv = new HashMap<>();
    RMIClientSocketFactory csf = null;
    RMIServerSocketFactory ssf = null;
    if (rmiSSL) {
        if (rmiRegistryPort == rmiConnectorPort) {
            throw new IOException("SSL is enabled. " + "rmiConnectorPort cannot share with the rmiRegistryPort!");
        }
        csf = new SslRMIClientSocketFactorySecure();
        ssf = new SslRMIServerSocketFactorySecure();
    }
    if (csf != null) {
        jmxEnv.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
    }
    if (ssf != null) {
        jmxEnv.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
    }
    // Configure authentication
    if (authenticate) {
        jmxEnv.put("jmx.remote.x.password.file", passwordFile);
        jmxEnv.put("jmx.remote.x.access.file", accessFile);
    }
    // Create the RMI registry
    rmiRegistry = LocateRegistry.createRegistry(rmiRegistryPort);
    // Retrieve the PlatformMBeanServer.
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    // Build jmxURL
    JMXServiceURL serviceUrl = buildJMXServiceURL(rmiRegistryPort, rmiConnectorPort);
    try {
        // Start the JMXListener with the connection string
        synchronized (JMXListener.class) {
            if (JMX_CS != null) {
                throw new RuntimeException("Started by another thread?");
            }
            JMX_CS = JMXConnectorServerFactory.newJMXConnectorServer(serviceUrl, jmxEnv, mbs);
            JMX_CS.start();
        }
        LOG.info("JMXConnectorServer started!");
    } catch (IOException e) {
        LOG.error("Failed start of JMXConnectorServer!", e);
        // deregister the RMI registry
        if (rmiRegistry != null) {
            UnicastRemoteObject.unexportObject(rmiRegistry, true);
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) IOException(java.io.IOException) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) MBeanServer(javax.management.MBeanServer)

Aggregations

RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)21 RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)12 HashMap (java.util.HashMap)8 SslRMIClientSocketFactory (javax.rmi.ssl.SslRMIClientSocketFactory)7 JMXServiceURL (javax.management.remote.JMXServiceURL)6 InetAddress (java.net.InetAddress)5 RemoteException (java.rmi.RemoteException)5 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)5 IOException (java.io.IOException)4 AlreadyBoundException (java.rmi.AlreadyBoundException)4 MBeanServer (javax.management.MBeanServer)4 ServerSocket (java.net.ServerSocket)3 LocateRegistry (java.rmi.registry.LocateRegistry)3 Registry (java.rmi.registry.Registry)3 SslRMIServerSocketFactory (javax.rmi.ssl.SslRMIServerSocketFactory)3 UnknownHostException (java.net.UnknownHostException)2 Remote (java.rmi.Remote)2 JMXConnectorServer (javax.management.remote.JMXConnectorServer)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1