Search in sources :

Example 1 with MBeanInvocationHandler

use of org.apache.karaf.management.internal.MBeanInvocationHandler in project karaf by apache.

the class ConnectorServerFactory method init.

public void init() throws Exception {
    if (this.server == null) {
        throw new IllegalArgumentException("server must be set");
    }
    JMXServiceURL url = new JMXServiceURL(this.serviceUrl);
    setupKarafRMIServerSocketFactory();
    if (isClientAuth()) {
        this.secured = true;
    }
    if (this.secured) {
        this.setupSsl();
    }
    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);
    this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, this.environment, guardedServer);
    if (this.objectName != null) {
        this.server.registerMBean(this.connectorServer, this.objectName);
    }
    try {
        if (this.threaded) {
            Thread connectorThread = new Thread(() -> {
                try {
                    Thread.currentThread().setContextClassLoader(ConnectorServerFactory.class.getClassLoader());
                    connectorServer.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();
        }
    } catch (Exception ex) {
        doUnregister(this.objectName);
        throw ex;
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MBeanInvocationHandler(org.apache.karaf.management.internal.MBeanInvocationHandler) JMXConnectorServerFactory(javax.management.remote.JMXConnectorServerFactory) BindException(java.net.BindException) IOException(java.io.IOException) IOException(java.io.IOException) BindException(java.net.BindException) GeneralSecurityException(java.security.GeneralSecurityException) JMException(javax.management.JMException) MBeanServer(javax.management.MBeanServer)

Aggregations

IOException (java.io.IOException)1 BindException (java.net.BindException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 JMException (javax.management.JMException)1 MBeanServer (javax.management.MBeanServer)1 JMXConnectorServerFactory (javax.management.remote.JMXConnectorServerFactory)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1 MBeanInvocationHandler (org.apache.karaf.management.internal.MBeanInvocationHandler)1