Search in sources :

Example 26 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class ConnectorBootstrap method startLocalConnectorServer.

/*
     * Creates and starts a RMI Connector Server for "local" monitoring
     * and management.
     */
public static JMXConnectorServer startLocalConnectorServer() {
    // Ensure cryptographically strong random number generater used
    // to choose the object number - see java.rmi.server.ObjID
    System.setProperty("java.rmi.server.randomIDs", "true");
    // This RMI server should not keep the VM alive
    Map<String, Object> env = new HashMap<>();
    env.put(RMIExporter.EXPORTER_ATTRIBUTE, new PermanentExporter());
    env.put(EnvHelp.CREDENTIAL_TYPES, new String[] { String[].class.getName(), String.class.getName() });
    // The local connector server need only be available via the
    // loopback connection.
    String localhost = "localhost";
    InetAddress lh = null;
    try {
        lh = InetAddress.getByName(localhost);
        localhost = lh.getHostAddress();
    } catch (UnknownHostException x) {
    }
    // a loopback address.
    if (lh == null || !lh.isLoopbackAddress()) {
        localhost = "127.0.0.1";
    }
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {
        JMXServiceURL url = new JMXServiceURL("rmi", localhost, 0);
        // Do we accept connections from local interfaces only?
        Properties props = Agent.getManagementProperties();
        if (props == null) {
            props = new Properties();
        }
        String useLocalOnlyStr = props.getProperty(PropertyNames.USE_LOCAL_ONLY, DefaultValues.USE_LOCAL_ONLY);
        boolean useLocalOnly = Boolean.valueOf(useLocalOnlyStr).booleanValue();
        if (useLocalOnly) {
            env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new LocalRMIServerSocketFactory());
        }
        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        server.start();
        return server;
    } catch (Exception e) {
        throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) Properties(java.util.Properties) RemoteException(java.rmi.RemoteException) NoSuchObjectException(java.rmi.NoSuchObjectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) AgentConfigurationError(sun.management.AgentConfigurationError) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) RemoteObject(java.rmi.server.RemoteObject) InetAddress(java.net.InetAddress) MBeanServer(javax.management.MBeanServer)

Example 27 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jvm-breakglass by matlux.

the class MBeanTest method createJMXServer.

private JMXConnectorServer createJMXServer(int port, JMXServiceURL url) throws Exception {
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    java.rmi.registry.LocateRegistry.createRegistry(port);
    JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
    return connectorServer;
}
Also used : MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 28 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jvm-breakglass by matlux.

the class MBeanTest method testStartStopViaJMX.

@Test
public void testStartStopViaJMX() throws Exception {
    int port = 9876;
    JMXServiceURL url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port));
    JMXConnectorServer jmxServer = createJMXServer(port, url);
    try {
        jmxServer.start();
        assertFalse(nreplServer.isStarted());
        JMXConnector connector = JMXConnectorFactory.connect(url);
        try {
            MBeanServerConnection conn = connector.getMBeanServerConnection();
            ObjectName objectName = MBeanRegistration.getObjectName();
            NreplMBean proxy = JMX.newMBeanProxy(conn, objectName, NreplMBean.class);
            assertFalse(proxy.isStarted());
            proxy.start();
            assertTrue(proxy.isStarted());
            proxy.stop();
            assertFalse(proxy.isStarted());
        } finally {
            connector.close();
        }
    } finally {
        jmxServer.stop();
    }
    assertFalse(nreplServer.isStarted());
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 29 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jvm-breakglass by matlux.

the class MBeanTest method testStartViaJMX.

@Test
public void testStartViaJMX() throws Exception {
    int port = 9875;
    JMXServiceURL url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port));
    JMXConnectorServer jmxServer = createJMXServer(port, url);
    try {
        jmxServer.start();
        assertFalse(nreplServer.isStarted());
        JMXConnector connector = JMXConnectorFactory.connect(url);
        try {
            MBeanServerConnection conn = connector.getMBeanServerConnection();
            ObjectName objectName = MBeanRegistration.getObjectName();
            NreplMBean proxy = JMX.newMBeanProxy(conn, objectName, NreplMBean.class);
            assertFalse(proxy.isStarted());
            proxy.start();
            assertTrue(proxy.isStarted());
        } finally {
            connector.close();
        }
        assertTrue(nreplServer.isStarted());
    } finally {
        jmxServer.stop();
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 30 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project graphdb by neo4j-attic.

the class HotspotManagementSupport method getJMXServiceURL.

@Override
protected JMXServiceURL getJMXServiceURL(KernelData kernel) {
    JMXServiceURL url = null;
    try {
        Class<?> cal = Class.forName("sun.management.ConnectorAddressLink");
        try {
            Method importRemoteFrom = cal.getMethod("importRemoteFrom", int.class);
            @SuppressWarnings("unchecked") Map<String, String> remote = (Map<String, String>) importRemoteFrom.invoke(null, Integer.valueOf(0));
            url = getUrlFrom(remote);
        } catch (NoSuchMethodException ex) {
        // handled by null check
        }
        if (url == null) {
            Method importFrom = cal.getMethod("importFrom", int.class);
            url = getUrlFrom((String) importFrom.invoke(null, Integer.valueOf(0)));
        }
    } catch (InvocationTargetException e) {
        log.log(Level.CONFIG, "Failed to load local JMX connection URL.", e.getTargetException());
    } catch (LinkageError e) {
        log.log(Level.CONFIG, "Failed to load local JMX connection URL.", e);
    } catch (Exception e) {
        log.log(Level.CONFIG, "Failed to load local JMX connection URL.", e);
    }
    // No previous connection server -- create one!
    if (url == null) {
        Object portObj = kernel.getParam("jmx.port");
        int port = 0;
        if (portObj instanceof Integer) {
            port = ((Integer) portObj).intValue();
        } else if (portObj instanceof String) {
            try {
                port = Integer.parseInt((String) portObj);
            } catch (NumberFormatException ok) {
            // handled by 0-check
            }
        }
        if (port > 0) {
            Object useSslObj = kernel.getParam("jmx.use_ssl");
            boolean useSSL = false;
            if (useSslObj instanceof Boolean) {
                useSSL = ((Boolean) useSslObj).booleanValue();
            } else if (useSslObj instanceof String) {
                useSSL = Boolean.parseBoolean((String) useSslObj);
            }
            log.log(Level.CONFIG, "Creating new MBean server on port %s%s", new Object[] { Integer.valueOf(port), useSSL ? " using ssl" : "" });
            JMXConnectorServer server = createServer(port, useSSL);
            if (server != null) {
                try {
                    server.start();
                } catch (IOException e) {
                    log.log(Level.CONFIG, "Failed to start MBean server", e);
                    server = null;
                }
                if (server != null) {
                    try {
                        server.getMBeanServer().registerMBean(server, KernelProxy.createObjectName(kernel.instanceId(), "JMX Server"));
                    } catch (Exception e) {
                        log.log(Level.CONFIG, "Failed to register MBean server as JMX bean", e);
                    }
                    url = server.getAddress();
                }
            }
        }
    }
    return url;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JMXConnectorServer (javax.management.remote.JMXConnectorServer)63 JMXServiceURL (javax.management.remote.JMXServiceURL)57 JMXConnector (javax.management.remote.JMXConnector)47 MBeanServer (javax.management.MBeanServer)44 MBeanServerConnection (javax.management.MBeanServerConnection)38 ObjectName (javax.management.ObjectName)31 HashMap (java.util.HashMap)24 IOException (java.io.IOException)13 MalformedURLException (java.net.MalformedURLException)13 RemoteException (java.rmi.RemoteException)13 NotificationListener (javax.management.NotificationListener)12 Notification (javax.management.Notification)11 Attribute (javax.management.Attribute)9 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7 Map (java.util.Map)7 Properties (java.util.Properties)7 JMXPluggableAuthenticator (com.sun.jmx.remote.security.JMXPluggableAuthenticator)5 UnknownHostException (java.net.UnknownHostException)5 Test (org.junit.Test)5