Example usage for javax.management.remote JMXConnectorServerFactory newJMXConnectorServer

List of usage examples for javax.management.remote JMXConnectorServerFactory newJMXConnectorServer

Introduction

In this page you can find the example usage for javax.management.remote JMXConnectorServerFactory newJMXConnectorServer.

Prototype

public static JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL, Map<String, ?> environment,
        MBeanServer mbeanServer) throws IOException 

Source Link

Document

Creates a connector server at the given address.

Usage

From source file:org.apache.geode.admin.jmx.internal.AgentImpl.java

/**
 * Defines and starts the JMX RMIConnector and service.
 * <p>/*from w ww .  j  a  va 2  s  .c om*/
 * If {@link AgentConfig#isRmiEnabled} returns false, then this adaptor will not be started.
 */
private void startRMIConnectorServer() {
    if (!this.agentConfig.isRmiEnabled())
        return;

    String rmiBindAddress = this.agentConfig.getRmiBindAddress();

    // Set RMI Stubs to use the given RMI Bind Address
    // Default bindAddress is "", if none is set - ignore if not set
    // If java.rmi.server.hostname property is specified then
    // that override is not changed
    String rmiStubServerNameKey = "java.rmi.server.hostname";
    String overrideHostName = System.getProperty(rmiStubServerNameKey);
    if ((overrideHostName == null || overrideHostName.trim().length() == 0)
            && (rmiBindAddress != null && rmiBindAddress.trim().length() != 0)) {
        System.setProperty(rmiStubServerNameKey, rmiBindAddress);
        logger.info(LocalizedMessage.create(LocalizedStrings.AgentImpl_SETTING_0,
                new StringBuilder(rmiStubServerNameKey).append(" = ").append(rmiBindAddress)));
    }

    try {
        createRMIRegistry();
        ObjectName objName = getRMIConnectorServerName();

        // make sure this adaptor is not already registered...
        if (getMBeanServer().isRegistered(objName)) {
            // dunno how we got here...
            logger.info(LocalizedMessage
                    .create(LocalizedStrings.AgentImpl_RMICONNECTORSERVER_ALREADY_REGISTERED_AS__0, objName));
            return;
        }

        /*
         * url defined as: service:jmx:protocol:sap where 1. protocol: rmi 2. sap is:
         * [host[:port]][url-path] where host: rmi-binding-address port: rmi-server-port url-path:
         * /jndi/rmi://<rmi-binding-address>:<rmi-port><JNDI_NAME>
         */
        String urlString = null;
        String connectorServerHost = "";
        int connectorServerPort = this.agentConfig.getRmiServerPort();
        String rmiRegistryHost = "";
        int rmiRegistryPort = this.agentConfig.getRmiPort();

        // Set registryHost to localhost if not specified
        // RMI stubs would use a default IP if namingHost is left empty
        if (rmiBindAddress == null || rmiBindAddress.trim().length() == 0) {
            connectorServerHost = "localhost";
            rmiRegistryHost = "";
        } else {
            connectorServerHost = applyRFC2732(rmiBindAddress);
            rmiRegistryHost = connectorServerHost;
        }

        urlString = MessageFormat.format(AgentImpl.JMX_SERVICE_URL, connectorServerHost,
                String.valueOf(connectorServerPort), rmiRegistryHost, String.valueOf(rmiRegistryPort),
                JNDI_NAME);

        logger.debug("JMX Service URL string is : \"{}\"", urlString);

        // The address of the connector
        JMXServiceURL url = new JMXServiceURL(urlString);

        Map<String, Object> env = new HashMap<String, Object>();
        // env.put(Context.INITIAL_CONTEXT_FACTORY,
        // "com.sun.jndi.rmi.registry.RegistryContextFactory");
        // env.put(Context.PROVIDER_URL, "rmi://localhost:1099");

        RMIServerSocketFactory ssf = new MX4JServerSocketFactory(this.agentConfig.isAgentSSLEnabled(), // true,
                this.agentConfig.isAgentSSLRequireAuth(), // true,
                this.agentConfig.getAgentSSLProtocols(), // "any",
                this.agentConfig.getAgentSSLCiphers(), // "any",
                this.agentConfig.getRmiBindAddress(), 10, // backlog
                this.agentConfig.getGfSecurityProperties());
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);

        if (this.agentConfig.isAgentSSLEnabled()) {
            RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
            env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
        }

        MBeanServer mbs = null; // will be set by registering w/ mbeanServer
        this.rmiConnector = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);

        // for cleanup
        this.rmiConnector.addNotificationListener(new ConnectionNotificationAdapter(),
                new ConnectionNotificationFilterImpl(), this);

        // Register the JMXConnectorServer in the MBeanServer
        getMBeanServer().registerMBean(this.rmiConnector, objName);

        // Start the JMXConnectorServer
        this.rmiConnector.start();
    } catch (VirtualMachineError err) {
        SystemFailure.initiateFailure(err);
        // If this ever returns, rethrow the error. We're poisoned
        // now, so don't let this thread continue.
        throw err;
    } catch (Throwable t) {
        // Whenever you catch Error or Throwable, you must also
        // catch VirtualMachineError (see above). However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        logger.error(LocalizedStrings.AgentImpl_FAILED_TO_START_RMICONNECTORSERVER, t);
        throw new StartupException(LocalizedStrings.AgentImpl_FAILED_TO_START_RMI_SERVICE.toLocalizedString(),
                t);
    }
}