Search in sources :

Example 11 with RMIConnectorServer

use of javax.management.remote.rmi.RMIConnectorServer in project jvm-tools by aragozin.

the class MxProxyCmd method startLocalJMXEndPoint.

private static void startLocalJMXEndPoint(String bindhost, int port, MBeanServerConnection conn) throws MalformedURLException, IOException {
    String host = bindhost == null ? "localhost" : bindhost;
    InetAddress localInetAddress = null;
    try {
        localInetAddress = InetAddress.getByName(host);
        host = localInetAddress.getHostAddress();
    } catch (UnknownHostException localUnknownHostException) {
    }
    if ((localInetAddress == null) || (localInetAddress.isLoopbackAddress())) {
        host = "127.0.0.1";
    }
    String uri = "service:jmx:rmi://" + host + ":" + port + "/jmxrmi";
    System.out.println("Open proxy JMX end point on URI - " + uri);
    ProxyMBeanServer proxyM = new ProxyMBeanServer(conn);
    RMIJRMPServerImpl serverImpl = new RMIJRMPServerImpl(port, null, null, new HashMap<String, Object>());
    RMIConnectorServer server = new RMIConnectorServer(new JMXServiceURL(uri), new HashMap<String, Object>(), serverImpl, proxyM);
    server.start();
    new SingleEntryRegistry(port, "jmxrmi", serverImpl.toStub());
}
Also used : RMIJRMPServerImpl(javax.management.remote.rmi.RMIJRMPServerImpl) JMXServiceURL(javax.management.remote.JMXServiceURL) UnknownHostException(java.net.UnknownHostException) RMIConnectorServer(javax.management.remote.rmi.RMIConnectorServer) InetAddress(java.net.InetAddress)

Example 12 with RMIConnectorServer

use of javax.management.remote.rmi.RMIConnectorServer 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

RMIConnectorServer (javax.management.remote.rmi.RMIConnectorServer)12 JMXServiceURL (javax.management.remote.JMXServiceURL)9 RMIJRMPServerImpl (javax.management.remote.rmi.RMIJRMPServerImpl)7 IOException (java.io.IOException)5 AlreadyBoundException (java.rmi.AlreadyBoundException)4 Registry (java.rmi.registry.Registry)4 HashMap (java.util.HashMap)4 MBeanServer (javax.management.MBeanServer)4 InetAddress (java.net.InetAddress)3 MalformedURLException (java.net.MalformedURLException)3 RemoteException (java.rmi.RemoteException)3 LocateRegistry (java.rmi.registry.LocateRegistry)3 RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)3 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)3 RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)2 SslRMIClientSocketFactory (javax.rmi.ssl.SslRMIClientSocketFactory)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ServerNotifForwarder (com.sun.jmx.remote.internal.ServerNotifForwarder)1 WeakReference (java.lang.ref.WeakReference)1 Field (java.lang.reflect.Field)1