Search in sources :

Example 26 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project jdk8u_jdk by JetBrains.

the class NotificationAccessControllerTest method runTest.

/**
     * Run test
     */
public int runTest(boolean enableChecks, boolean throwException) throws Exception {
    echo("\n=-=-= " + (enableChecks ? "Enable" : "Disable") + " notification access control checks " + (!enableChecks ? "" : (throwException ? ": add/remove " : ": fetch ")) + "=-=-=");
    JMXConnectorServer server = null;
    JMXConnector client = null;
    /*
        * (!enableChecks)
        * - List must contain three notifs from sources nb1, nb2 and nb3
        * (enableChecks && !throwException)
        * - List must contain one notif from source nb1
        * (enableChecks && throwException)
        * - List must contain two notifs from sources nb2 and nb3
        */
    final int expected_notifs = (!enableChecks ? 3 : (throwException ? 2 : 1));
    // Create a new MBeanServer
    //
    final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    try {
        // Create server environment map
        //
        final Map<String, Object> env = new HashMap<>();
        env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
        if (enableChecks) {
            env.put("com.sun.jmx.remote.notification.access.controller", new NAC(throwException));
        }
        // Create the JMXServiceURL
        //
        final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
        // Create a JMXConnectorServer
        //
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        // Start the JMXConnectorServer
        //
        server.start();
        // Create server environment map
        //
        final Map<String, Object> cenv = new HashMap<>();
        String[] credentials = new String[] { "role", "password" };
        cenv.put("jmx.remote.credentials", credentials);
        // Create JMXConnector and connect to JMXConnectorServer
        //
        client = JMXConnectorFactory.connect(server.getAddress(), cenv);
        // Get non-secure MBeanServerConnection
        //
        final MBeanServerConnection mbsc = client.getMBeanServerConnection();
        // Create NB MBean
        //
        ObjectName nb1 = ObjectName.getInstance("domain:type=NB,name=1");
        ObjectName nb2 = ObjectName.getInstance("domain:type=NB,name=2");
        ObjectName nb3 = ObjectName.getInstance("domain:type=NB,name=3");
        mbsc.createMBean(NB.class.getName(), nb1);
        mbsc.createMBean(NB.class.getName(), nb2);
        mbsc.createMBean(NB.class.getName(), nb3);
        // Add notification listener
        //
        Semaphore s = new Semaphore(0);
        Listener li = new Listener(s);
        try {
            mbsc.addNotificationListener(nb1, li, null, null);
            if (enableChecks && throwException) {
                echo("Didn't get expected exception");
                return 1;
            }
        } catch (SecurityException e) {
            if (enableChecks && throwException) {
                echo("Got expected exception: " + e);
            } else {
                echo("Got unexpected exception: " + e);
                return 1;
            }
        }
        mbsc.addNotificationListener(nb2, li, null, null);
        System.out.println("\n+++ Expecting to receive " + expected_notifs + " notification" + (expected_notifs > 1 ? "s" : "") + " +++");
        // Invoke the "sendNotification" method
        //
        mbsc.invoke(nb1, "emitNotification", new Object[] { 0, null }, new String[] { "int", "javax.management.ObjectName" });
        mbsc.invoke(nb2, "emitNotification", new Object[] { 1, null }, new String[] { "int", "javax.management.ObjectName" });
        mbsc.invoke(nb2, "emitNotification", new Object[] { 2, nb3 }, new String[] { "int", "javax.management.ObjectName" });
        // Wait for notifications to be emitted
        //
        s.acquire(expected_notifs);
        //
        if (!throwException)
            mbsc.removeNotificationListener(nb1, li);
        try {
            mbsc.removeNotificationListener(nb2, li);
            if (enableChecks && throwException) {
                echo("Didn't get expected exception");
                return 1;
            }
        } catch (SecurityException e) {
            if (enableChecks && throwException) {
                echo("Got expected exception: " + e);
            } else {
                echo("Got unexpected exception: " + e);
                return 1;
            }
        }
        int result = 0;
        List<ObjectName> sources = new ArrayList();
        sources.add(nb1);
        sources.add(nb2);
        sources.add(nb3);
        result = checkNotifs(expected_notifs, li.notifs, sources);
        if (result > 0) {
            return result;
        }
    } catch (Exception e) {
        echo("Failed to perform operation: " + e);
        e.printStackTrace();
        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) NotificationListener(javax.management.NotificationListener) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Semaphore(java.util.concurrent.Semaphore) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 27 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project jdk8u_jdk by JetBrains.

the class IdleTimeoutTest method getIdleTimeout.

private static long getIdleTimeout(MBeanServer mbs, JMXServiceURL url) throws Exception {
    JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    server.start();
    try {
        url = server.getAddress();
        // Force initialization (class loading, JIT, etc...)
        //
        JMXConnector client = JMXConnectorFactory.connect(url);
        try {
            String connId = client.getConnectionId();
            MBeanServerConnection conn = client.getMBeanServerConnection();
        } finally {
            client.close();
        }
        // Do the time measurement
        //
        final long firstTime = System.currentTimeMillis();
        final long endtime;
        client = JMXConnectorFactory.connect(url);
        try {
            String connId = client.getConnectionId();
            MBeanServerConnection conn = client.getMBeanServerConnection();
            endtime = System.currentTimeMillis();
        } finally {
            client.close();
        }
        // multipled by 10 for a slow machine, plus 1500 for a fast one.
        return 10 * (endtime - firstTime) + 1500;
    } finally {
        server.stop();
    }
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 28 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project jdk8u_jdk by JetBrains.

the class IdleTimeoutTest method test.

private static boolean test(String proto, int opCount, int liCount) throws Exception {
    System.out.println("Idle timeout test for protocol " + proto);
    ObjectName delegateName = ObjectName.getInstance("JMImplementation:" + "type=MBeanServerDelegate");
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");
    final long timeout = getIdleTimeout(mbs, url);
    System.out.println("Timeout for " + proto + " is: " + timeout + " ms");
    Map idleMap = new HashMap();
    idleMap.put(EnvHelp.SERVER_CONNECTION_TIMEOUT, new Long(timeout));
    JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, idleMap, mbs);
    final int[] listenerCount = new int[1];
    final NotificationListener[] countListeners = new NotificationListener[liCount];
    int i;
    for (i = 0; i < countListeners.length; i++) {
        countListeners[i] = new NotificationCounter(listenerCount, "Listener" + i);
    }
    server.start();
    try {
        url = server.getAddress();
        final long firstTime = System.currentTimeMillis();
        JMXConnector client = JMXConnectorFactory.connect(url);
        long elapsed, startIdle = 0;
        try {
            String connId = client.getConnectionId();
            MBeanServerConnection conn = client.getMBeanServerConnection();
            elapsed = System.currentTimeMillis() - firstTime;
            System.out.println("Idle Time: " + elapsed + "ms");
            for (i = 0; i < countListeners.length; i++) {
                System.out.println("add " + countListeners[i] + ": starting at " + elapsed + "ms");
                conn.addNotificationListener(delegateName, countListeners[i], null, null);
            }
            System.out.println("connId=" + connId);
            for (i = 0; i < opCount; i++) {
                elapsed = System.currentTimeMillis() - firstTime;
                System.out.println("Operation[" + (i + 1) + "]: starting at " + elapsed + "ms");
                final String name = "d:type=mlet,instance=" + i;
                mbs.createMBean("javax.management.loading.MLet", new ObjectName(name));
                if (i == (opCount - 1))
                    startIdle = System.currentTimeMillis();
                Thread.sleep(2);
            }
            // Wait for notifs to arrive before doing removeNListener
            long startTime = System.currentTimeMillis();
            long deadline = startTime + 10000;
            System.out.println("Waiting for notifs: starting at " + (startTime - firstTime) + "ms");
            final int expectedCount = opCount * countListeners.length;
            while (System.currentTimeMillis() < deadline) {
                synchronized (listenerCount) {
                    if (listenerCount[0] >= expectedCount)
                        break;
                    listenerCount.wait();
                }
            }
            long elapsedWait = System.currentTimeMillis() - startTime;
            System.out.println("Waited " + elapsedWait + "ms for notifs to arrive");
            if (listenerCount[0] != expectedCount) {
                System.out.println("Did not get expected " + expectedCount + " notifications: " + listenerCount[0]);
                return false;
            }
            elapsed = System.currentTimeMillis() - firstTime;
            System.out.println("idle time since last operation: " + (elapsed + firstTime - startIdle) + "ms");
            System.out.println("Requesting conn id at: " + elapsed + "ms");
            final String cid = client.getConnectionId();
            elapsed = System.currentTimeMillis() - firstTime;
            System.out.println("Got conn id <" + cid + "> at: " + elapsed + "ms");
            if (!connId.equals(cid)) {
                System.out.println("Client id changed: <" + connId + "> -> <" + cid + ">");
                return false;
            }
            List ids = Arrays.asList(server.getConnectionIds());
            if (!ids.contains(connId)) {
                System.out.println("Server ids don't contain our id: " + ids + " - " + connId);
                return false;
            }
            for (i = 0; i < countListeners.length; i++) {
                System.out.println("Removing notification listener: " + countListeners[i]);
                conn.removeNotificationListener(delegateName, countListeners[i]);
            }
            System.out.println("Waiting for id list to drop ours");
            // pass or timed out by test harness - see 8025204
            do {
                Thread.sleep(100);
                ids = Arrays.asList(server.getConnectionIds());
            } while (ids.contains(connId));
            conn.getDefaultDomain();
            if (connId.equals(client.getConnectionId())) {
                System.out.println("Client id did not change: <" + connId + ">: idle timeout did not happen?");
                return false;
            } else {
                System.out.println("Client id changed as expected: <" + connId + "> -> <" + client.getConnectionId() + ">");
            }
        } finally {
            client.close();
            System.out.println("Connection id list on server after " + "client close: " + Arrays.asList(server.getConnectionIds()));
        }
    } finally {
        server.stop();
    }
    System.out.println("*** ------------------------------------------");
    System.out.println("*** Test passed for " + proto);
    System.out.println("*** ------------------------------------------");
    return true;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 29 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project jdk8u_jdk by JetBrains.

the class GetConnectionTest method test.

private static boolean test(String proto) throws Exception {
    JMXConnector client;
    try {
        JMXServiceURL url = new JMXServiceURL(proto, null, 0);
        client = JMXConnectorFactory.newJMXConnector(url, null);
    } catch (MalformedURLException e) {
        System.out.println("Protocol " + proto + " not supported, ignoring");
        return true;
    }
    // IOException is expected
    try {
        MBeanServerConnection connection = client.getMBeanServerConnection();
        System.out.println("FAILED: Expected IOException is not thrown.");
        return false;
    } catch (IOException e) {
        System.out.println("PASSED");
        return true;
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXConnector(javax.management.remote.JMXConnector) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 30 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project jdk8u_jdk by JetBrains.

the class RMIExitTest method test.

private static void test() {
    try {
        JMXServiceURL u = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer server;
        JMXServiceURL addr;
        JMXConnector client;
        MBeanServerConnection mserver;
        final ObjectName delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
        final NotificationListener dummyListener = new NotificationListener() {

            public void handleNotification(Notification n, Object o) {
                // do nothing
                return;
            }
        };
        server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
        server.start();
        addr = server.getAddress();
        client = JMXConnectorFactory.newJMXConnector(addr, null);
        client.connect(null);
        mserver = client.getMBeanServerConnection();
        String s1 = "1";
        String s2 = "2";
        String s3 = "3";
        mserver.addNotificationListener(delegateName, dummyListener, null, s1);
        mserver.addNotificationListener(delegateName, dummyListener, null, s2);
        mserver.addNotificationListener(delegateName, dummyListener, null, s3);
        mserver.removeNotificationListener(delegateName, dummyListener, null, s3);
        mserver.removeNotificationListener(delegateName, dummyListener, null, s2);
        mserver.removeNotificationListener(delegateName, dummyListener, null, s1);
        client.close();
        server.stop();
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Notification(javax.management.Notification) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) NotificationListener(javax.management.NotificationListener)

Aggregations

MBeanServerConnection (javax.management.MBeanServerConnection)125 JMXConnector (javax.management.remote.JMXConnector)84 ObjectName (javax.management.ObjectName)73 JMXServiceURL (javax.management.remote.JMXServiceURL)59 JMXConnectorServer (javax.management.remote.JMXConnectorServer)38 Test (org.junit.Test)35 IOException (java.io.IOException)31 MBeanServer (javax.management.MBeanServer)28 HashMap (java.util.HashMap)23 Attribute (javax.management.Attribute)15 NotificationListener (javax.management.NotificationListener)13 MalformedURLException (java.net.MalformedURLException)12 ArrayList (java.util.ArrayList)12 Notification (javax.management.Notification)12 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Map (java.util.Map)10 List (java.util.List)8 RemoteException (java.rmi.RemoteException)7 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7