Search in sources :

Example 56 with JMXConnectorServer

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

the class RMIConnectorInternalMapTest method main.

public static void main(String[] args) throws Exception {
    System.out.println("---RMIConnectorInternalMapTest starting...");
    JMXConnectorServer connectorServer = null;
    JMXConnector connectorClient = null;
    try {
        MBeanServer mserver = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL serverURL = new JMXServiceURL("rmi", "localhost", 0);
        connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serverURL, null, mserver);
        connectorServer.start();
        JMXServiceURL serverAddr = connectorServer.getAddress();
        connectorClient = JMXConnectorFactory.connect(serverAddr, null);
        connectorClient.connect();
        Field rmbscMapField = RMIConnector.class.getDeclaredField("rmbscMap");
        rmbscMapField.setAccessible(true);
        Map<Subject, WeakReference<MBeanServerConnection>> map = (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient);
        if (map != null && !map.isEmpty()) {
            // failed
            throw new RuntimeException("RMIConnector's rmbscMap must be empty at the initial time.");
        }
        Subject delegationSubject = new Subject(true, Collections.singleton(new JMXPrincipal("delegate")), Collections.EMPTY_SET, Collections.EMPTY_SET);
        MBeanServerConnection mbsc1 = connectorClient.getMBeanServerConnection(delegationSubject);
        MBeanServerConnection mbsc2 = connectorClient.getMBeanServerConnection(delegationSubject);
        if (mbsc1 == null) {
            throw new RuntimeException("Got null connection.");
        }
        if (mbsc1 != mbsc2) {
            throw new RuntimeException("Not got same connection with a same subject.");
        }
        map = (Map<Subject, WeakReference<MBeanServerConnection>>) rmbscMapField.get(connectorClient);
        if (map == null || map.isEmpty()) {
            // failed
            throw new RuntimeException("RMIConnector's rmbscMap has wrong size " + "after creating a delegated connection.");
        }
        delegationSubject = null;
        mbsc1 = null;
        mbsc2 = null;
        int i = 0;
        while (!map.isEmpty() && i++ < 60) {
            System.gc();
            Thread.sleep(100);
        }
        System.out.println("---GC times: " + i);
        if (!map.isEmpty()) {
            throw new RuntimeException("Failed to clean RMIConnector's rmbscMap");
        } else {
            System.out.println("---RMIConnectorInternalMapTest: PASSED!");
        }
    } finally {
        try {
            connectorClient.close();
            connectorServer.stop();
        } catch (Exception e) {
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXPrincipal(javax.management.remote.JMXPrincipal) Subject(javax.security.auth.Subject) JMXConnectorServer(javax.management.remote.JMXConnectorServer) Field(java.lang.reflect.Field) JMXConnector(javax.management.remote.JMXConnector) WeakReference(java.lang.ref.WeakReference) Map(java.util.Map) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 57 with JMXConnectorServer

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

the class RMIConnectorLogAttributesTest method runTest.

private void runTest() {
    int rmiPort = findPort();
    if (rmiPort == -1) {
        throw new RuntimeException("Test failed. No available port");
    }
    JMXConnectorServer server = null;
    try {
        server = startServer(rmiPort);
        JMXConnector connector = connectToServer(server);
        doTest(connector);
    } catch (Exception e) {
        throw new RuntimeException("Test failed unexpectedly", e);
    } finally {
        if (server != null) {
            try {
                server.stop();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) IOException(java.io.IOException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 58 with JMXConnectorServer

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

the class ConnectionTest method test.

private static boolean test(String proto) throws Exception {
    ObjectName serverName = ObjectName.getInstance("d:type=server");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    JMXAuthenticator authenticator = new BogusAuthenticator();
    Map env = Collections.singletonMap("jmx.remote.authenticator", authenticator);
    JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");
    JMXConnectorServer server;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, null);
    } catch (MalformedURLException e) {
        System.out.println("Protocol " + proto + " not supported, ignoring");
        return true;
    }
    System.out.println("Created connector server");
    mbs.registerMBean(server, serverName);
    System.out.println("Registered connector server in MBean server");
    mbs.addNotificationListener(serverName, logListener, null, null);
    mbs.invoke(serverName, "start", null, null);
    System.out.println("Started connector server");
    JMXServiceURL address = (JMXServiceURL) mbs.getAttribute(serverName, "Address");
    System.out.println("Retrieved address: " + address);
    if (address.getHost().length() == 0) {
        System.out.println("Generated address has empty hostname");
        return false;
    }
    JMXConnector client = JMXConnectorFactory.connect(address);
    System.out.println("Client connected");
    String clientConnId = client.getConnectionId();
    System.out.println("Got connection ID on client: " + clientConnId);
    boolean ok = checkConnectionId(proto, clientConnId);
    if (!ok)
        return false;
    System.out.println("Connection ID is OK");
    // 4901826: connection ids need some time to be updated using jmxmp
    // we don't get the notif immediately either
    // this was originally timeout 1ms, which was not enough
    Notification notif = waitForNotification(1000);
    System.out.println("Server got notification: " + notif);
    ok = mustBeConnectionNotification(notif, clientConnId, JMXConnectionNotification.OPENED);
    if (!ok)
        return false;
    client.close();
    System.out.println("Closed client");
    notif = waitForNotification(1000);
    System.out.println("Got notification: " + notif);
    ok = mustBeConnectionNotification(notif, clientConnId, JMXConnectionNotification.CLOSED);
    if (!ok)
        return false;
    client = JMXConnectorFactory.connect(address);
    System.out.println("Second client connected");
    String clientConnId2 = client.getConnectionId();
    if (clientConnId.equals(clientConnId2)) {
        System.out.println("Same connection ID for two connections: " + clientConnId2);
        return false;
    }
    System.out.println("Second client connection ID is different");
    notif = waitForNotification(1);
    ok = mustBeConnectionNotification(notif, clientConnId2, JMXConnectionNotification.OPENED);
    if (!ok)
        return false;
    MBeanServerConnection mbsc = client.getMBeanServerConnection();
    Map attrs = (Map) mbsc.getAttribute(serverName, "Attributes");
    System.out.println("Server attributes received by client: " + attrs);
    server.stop();
    System.out.println("Server stopped");
    notif = waitForNotification(1000);
    System.out.println("Server got connection-closed notification: " + notif);
    ok = mustBeConnectionNotification(notif, clientConnId2, JMXConnectionNotification.CLOSED);
    if (!ok)
        return false;
    try {
        mbsc.getDefaultDomain();
        System.out.println("Connection still working but should not be");
        return false;
    } catch (IOException e) {
        System.out.println("Connection correctly got exception: " + e);
    }
    try {
        client = JMXConnectorFactory.connect(address);
        System.out.println("Connector server still working but should " + "not be");
        return false;
    } catch (IOException e) {
        System.out.println("New connection correctly got exception: " + e);
    }
    return true;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXAuthenticator(javax.management.remote.JMXAuthenticator) IOException(java.io.IOException) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) HashMap(java.util.HashMap) Map(java.util.Map) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 59 with JMXConnectorServer

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

the class MBSFPreStartPostStartTest method runTest.

/**
     * Run test
     */
public int runTest(boolean setBeforeStart) throws Exception {
    echo("=-=-= MBSFPreStartPostStartTest: Set MBSF " + (setBeforeStart ? "before" : "after") + " starting the connector server =-=-=");
    JMXConnectorServer server = null;
    JMXConnector client = null;
    // Create a new MBeanServer
    //
    final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    try {
        // Create the JMXServiceURL
        //
        final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
        // Create a JMXConnectorServer
        //
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        // Create MBeanServerForwarder
        //
        MBeanServerForwarder mbsf = MBSFInvocationHandler.newProxyInstance();
        //
        if (setBeforeStart)
            server.setMBeanServerForwarder(mbsf);
        // Start the JMXConnectorServer
        //
        server.start();
        //
        if (!setBeforeStart)
            server.setMBeanServerForwarder(mbsf);
        // Create a JMXConnector
        //
        client = server.toJMXConnector(null);
        // Connect to the connector server
        //
        client.connect(null);
        // Get non-secure MBeanServerConnection
        //
        final MBeanServerConnection mbsc = client.getMBeanServerConnection();
        // Run method
        //
        mbsc.getDefaultDomain();
        // Check flag in MBeanServerForwarder
        //
        MBSFInvocationHandler mbsfih = (MBSFInvocationHandler) Proxy.getInvocationHandler(mbsf);
        if (mbsfih.getFlag() == true) {
            echo("OK: Did go into MBeanServerForwarder!");
        } else {
            echo("KO: Didn't go into MBeanServerForwarder!");
            return 1;
        }
    } catch (Exception e) {
        echo("Failed to perform operation: " + e);
        return 1;
    } finally {
        //
        if (client != null)
            client.close();
        //
        if (server != null)
            server.stop();
        //
        if (mbs != null)
            MBeanServerFactory.releaseMBeanServer(mbs);
    }
    return 0;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer) MBeanServer(javax.management.MBeanServer) MBeanServerForwarder(javax.management.remote.MBeanServerForwarder)

Example 60 with JMXConnectorServer

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

the class RMIConnectorLogAttributesTest method startServer.

private JMXConnectorServer startServer(int rmiPort) throws Exception {
    System.out.println("DEBUG: Create RMI registry on port " + rmiPort);
    LocateRegistry.createRegistry(rmiPort);
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    HashMap<String, Object> env = new HashMap<String, Object>();
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:" + rmiPort + "/jmxrmi");
    JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
    cs.start();
    System.out.println("DEBUG: Started the RMI connector server");
    return cs;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

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