Search in sources :

Example 16 with RMIClientSocketFactory

use of java.rmi.server.RMIClientSocketFactory 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)

Example 17 with RMIClientSocketFactory

use of java.rmi.server.RMIClientSocketFactory in project karaf by apache.

the class ConnectorServerFactory method init.

public void init() throws Exception {
    JMXServiceURL url = new JMXServiceURL(this.serviceUrl);
    if (registry == null && locate) {
        try {
            Registry reg = LocateRegistry.getRegistry(host, getPort());
            reg.list();
            registry = reg;
        } catch (RemoteException e) {
        // ignore
        }
    }
    if (registry == null && create) {
        registry = new JmxRegistry(getPort(), getBindingName(url));
        locallyCreated = true;
    }
    if (registry != null) {
        // register the registry as an OSGi service
        Hashtable<String, Object> props = new Hashtable<>();
        props.put("port", getPort());
        props.put("host", getHost());
        bundleContext.registerService(Registry.class, registry, props);
    }
    if (this.server == null) {
        throw new IllegalArgumentException("server must be set");
    }
    if (isClientAuth()) {
        this.secured = true;
    }
    if (this.secured) {
        setupSsl();
    } else {
        setupKarafRMIServerSocketFactory();
    }
    if (!AuthenticatorType.PASSWORD.equals(this.authenticatorType)) {
        this.environment.remove("jmx.remote.authenticator");
    }
    MBeanInvocationHandler handler = new MBeanInvocationHandler(server, guard);
    MBeanServer guardedServer = (MBeanServer) Proxy.newProxyInstance(server.getClass().getClassLoader(), new Class[] { MBeanServer.class }, handler);
    rmiServer = new RMIJRMPServerImpl(url.getPort(), (RMIClientSocketFactory) environment.get(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE), (RMIServerSocketFactory) environment.get(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE), environment);
    // Create the connector server now.
    this.connectorServer = new RMIConnectorServer(url, environment, rmiServer, guardedServer);
    if (this.objectName != null) {
        this.server.registerMBean(this.connectorServer, this.objectName);
    }
    if (jmxmpEnabled) {
        Security.addProvider(new PlainSaslServer.SaslPlainProvider());
        JMXServiceURL jmxmpUrl = new JMXServiceURL(this.jmxmpServiceUrl);
        this.jmxmpConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxmpUrl, this.jmxmpEnvironment, guardedServer);
        if (this.jmxmpObjectName != null) {
            this.server.registerMBean(this.jmxmpConnectorServer, this.jmxmpObjectName);
        }
    }
    try {
        if (this.threaded) {
            Thread connectorThread = new Thread(() -> {
                try {
                    Thread.currentThread().setContextClassLoader(ConnectorServerFactory.class.getClassLoader());
                    connectorServer.start();
                    remoteServerStub = rmiServer.toStub();
                    if (jmxmpEnabled && jmxmpConnectorServer != null) {
                        jmxmpConnectorServer.start();
                    }
                } catch (IOException ex) {
                    if (ex.getCause() instanceof BindException) {
                        // we want just the port message
                        int endIndex = ex.getMessage().indexOf("nested exception is");
                        // check to make sure we do not get an index out of range
                        if (endIndex > ex.getMessage().length() || endIndex < 0) {
                            endIndex = ex.getMessage().length();
                        }
                        throw new RuntimeException("\n" + ex.getMessage().substring(0, endIndex) + "\nYou may have started two containers.  If you need to start a second container or the default ports are already in use " + "update the config file etc/org.apache.karaf.management.cfg and change the Registry Port and Server Port to unused ports");
                    }
                    throw new RuntimeException("Could not start JMX connector server", ex);
                }
            });
            connectorThread.setName("JMX Connector Thread [" + this.serviceUrl + "]");
            connectorThread.setDaemon(this.daemon);
            connectorThread.start();
        } else {
            this.connectorServer.start();
            remoteServerStub = rmiServer.toStub();
            if (jmxmpEnabled && jmxmpConnectorServer != null) {
                jmxmpConnectorServer.start();
            }
        }
    } catch (Exception ex) {
        if (this.objectName != null) {
            doUnregister(this.objectName);
        }
        if (jmxmpEnabled && this.jmxmpObjectName != null) {
            doUnregister(this.jmxmpObjectName);
        }
        throw ex;
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MBeanInvocationHandler(org.apache.karaf.management.internal.MBeanInvocationHandler) Hashtable(java.util.Hashtable) JMXConnectorServerFactory(javax.management.remote.JMXConnectorServerFactory) BindException(java.net.BindException) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AccessException(java.rmi.AccessException) BindException(java.net.BindException) SocketException(java.net.SocketException) GeneralSecurityException(java.security.GeneralSecurityException) NotBoundException(java.rmi.NotBoundException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) JMException(javax.management.JMException) AlreadyBoundException(java.rmi.AlreadyBoundException) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) RMIJRMPServerImpl(javax.management.remote.rmi.RMIJRMPServerImpl) RMIConnectorServer(javax.management.remote.rmi.RMIConnectorServer) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) RemoteException(java.rmi.RemoteException) MBeanServer(javax.management.MBeanServer)

Aggregations

RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)17 RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)13 SslRMIClientSocketFactory (javax.rmi.ssl.SslRMIClientSocketFactory)9 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)5 JMXServiceURL (javax.management.remote.JMXServiceURL)5 RemoteException (java.rmi.RemoteException)4 MBeanServer (javax.management.MBeanServer)4 RMIJRMPServerImpl (javax.management.remote.rmi.RMIJRMPServerImpl)3 SslRMIServerSocketFactory (javax.rmi.ssl.SslRMIServerSocketFactory)3 InetAddress (java.net.InetAddress)2 AlreadyBoundException (java.rmi.AlreadyBoundException)2 ConnectIOException (java.rmi.ConnectIOException)2 LocateRegistry (java.rmi.registry.LocateRegistry)2 Registry (java.rmi.registry.Registry)2 RemoteObject (java.rmi.server.RemoteObject)2 Hashtable (java.util.Hashtable)2 RMIConnectorServer (javax.management.remote.rmi.RMIConnectorServer)2 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)2