Search in sources :

Example 1 with NotificationListener

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

the class SubjectDelegation2Test method main.

public static void main(String[] args) throws Exception {
    // Check for supported operating systems: Solaris
    //
    // This test runs only on Solaris due to CR 6285916
    //
    String osName = System.getProperty("os.name");
    System.out.println("os.name = " + osName);
    if (!osName.equals("SunOS")) {
        System.out.println("This test runs on Solaris only.");
        System.out.println("Bye! Bye!");
        return;
    }
    String policyFile = args[0];
    String testResult = args[1];
    System.out.println("Policy file = " + policyFile);
    System.out.println("Expected test result = " + testResult);
    JMXConnectorServer jmxcs = null;
    JMXConnector jmxc = null;
    try {
        // Create an RMI registry
        //
        System.out.println("Start RMI registry...");
        Registry reg = null;
        int port = 5800;
        while (port++ < 6000) {
            try {
                reg = LocateRegistry.createRegistry(port);
                System.out.println("RMI registry running on port " + port);
                break;
            } catch (RemoteException e) {
                // Failed to create RMI registry...
                System.out.println("Failed to create RMI registry " + "on port " + port);
            }
        }
        if (reg == null) {
            System.exit(1);
        }
        // Set the default password file
        //
        final String passwordFile = System.getProperty("test.src") + File.separator + "jmxremote.password";
        System.out.println("Password file = " + passwordFile);
        // Set policy file
        //
        final String policy = System.getProperty("test.src") + File.separator + policyFile;
        System.out.println("PolicyFile = " + policy);
        System.setProperty("java.security.policy", policy);
        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        // Register the SimpleStandardMBean
        //
        System.out.println("Create SimpleStandard MBean");
        SimpleStandard s = new SimpleStandard("monitorRole");
        mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));
        // Create Properties containing the username/password entries
        //
        Properties props = new Properties();
        props.setProperty("jmx.remote.x.password.file", passwordFile);
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put("jmx.remote.authenticator", new JMXPluggableAuthenticator(props));
        // Set Security Manager
        //
        System.setSecurityManager(new SecurityManager());
        // Create an RMI connector server
        //
        System.out.println("Create an RMI connector server");
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0, "/jndi/rmi://:" + port + "/server" + port);
        jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        jmxcs.start();
        // Create an RMI connector client
        //
        System.out.println("Create an RMI connector client");
        HashMap cli_env = new HashMap();
        // These credentials must match those in the default password file
        //
        String[] credentials = new String[] { "monitorRole", "QED" };
        cli_env.put("jmx.remote.credentials", credentials);
        jmxc = JMXConnectorFactory.connect(url, cli_env);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        // Get domains from MBeanServer
        //
        System.out.println("Domains:");
        String[] domains = mbsc.getDomains();
        for (int i = 0; i < domains.length; i++) {
            System.out.println("\tDomain[" + i + "] = " + domains[i]);
        }
        // Get MBean count
        //
        System.out.println("MBean count = " + mbsc.getMBeanCount());
        // Get State attribute
        //
        String oldState = (String) mbsc.getAttribute(new ObjectName("MBeans:type=SimpleStandard"), "State");
        System.out.println("Old State = \"" + oldState + "\"");
        // Set State attribute
        //
        System.out.println("Set State to \"changed state\"");
        mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"), new Attribute("State", "changed state"));
        // Get State attribute
        //
        String newState = (String) mbsc.getAttribute(new ObjectName("MBeans:type=SimpleStandard"), "State");
        System.out.println("New State = \"" + newState + "\"");
        if (!newState.equals("changed state")) {
            System.out.println("Invalid State = \"" + newState + "\"");
            System.exit(1);
        }
        // Add notification listener on SimpleStandard MBean
        //
        System.out.println("Add notification listener...");
        mbsc.addNotificationListener(new ObjectName("MBeans:type=SimpleStandard"), new NotificationListener() {

            public void handleNotification(Notification notification, Object handback) {
                System.out.println("Received notification: " + notification);
            }
        }, null, null);
        // Unregister SimpleStandard MBean
        //
        System.out.println("Unregister SimpleStandard MBean...");
        mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));
    } catch (SecurityException e) {
        if (testResult.equals("ko")) {
            System.out.println("Got expected security exception = " + e);
        } else {
            System.out.println("Got unexpected security exception = " + e);
            e.printStackTrace();
            throw e;
        }
    } catch (Exception e) {
        System.out.println("Unexpected exception caught = " + e);
        e.printStackTrace();
        throw e;
    } finally {
        //
        if (jmxc != null)
            jmxc.close();
        //
        if (jmxcs != null)
            jmxcs.stop();
        // Say goodbye
        //
        System.out.println("Bye! Bye!");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXPluggableAuthenticator(com.sun.jmx.remote.security.JMXPluggableAuthenticator) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) Properties(java.util.Properties) Notification(javax.management.Notification) RemoteException(java.rmi.RemoteException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) RemoteException(java.rmi.RemoteException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 2 with NotificationListener

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

the class SubjectDelegation3Test method main.

public static void main(String[] args) throws Exception {
    // Check for supported operating systems: Solaris
    //
    // This test runs only on Solaris due to CR 6285916
    //
    String osName = System.getProperty("os.name");
    System.out.println("os.name = " + osName);
    if (!osName.equals("SunOS")) {
        System.out.println("This test runs on Solaris only.");
        System.out.println("Bye! Bye!");
        return;
    }
    String policyFile = args[0];
    String testResult = args[1];
    System.out.println("Policy file = " + policyFile);
    System.out.println("Expected test result = " + testResult);
    JMXConnectorServer jmxcs = null;
    JMXConnector jmxc = null;
    try {
        // Create an RMI registry
        //
        System.out.println("Start RMI registry...");
        Registry reg = null;
        int port = 5800;
        while (port++ < 6000) {
            try {
                reg = LocateRegistry.createRegistry(port);
                System.out.println("RMI registry running on port " + port);
                break;
            } catch (RemoteException e) {
                // Failed to create RMI registry...
                System.out.println("Failed to create RMI registry " + "on port " + port);
            }
        }
        if (reg == null) {
            System.exit(1);
        }
        // Set the default password file
        //
        final String passwordFile = System.getProperty("test.src") + File.separator + "jmxremote.password";
        System.out.println("Password file = " + passwordFile);
        // Set policy file
        //
        final String policy = System.getProperty("test.src") + File.separator + policyFile;
        System.out.println("PolicyFile = " + policy);
        System.setProperty("java.security.policy", policy);
        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        // Register the SimpleStandardMBean
        //
        System.out.println("Create SimpleStandard MBean");
        SimpleStandard s = new SimpleStandard("delegate");
        mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));
        // Create Properties containing the username/password entries
        //
        Properties props = new Properties();
        props.setProperty("jmx.remote.x.password.file", passwordFile);
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put("jmx.remote.authenticator", new JMXPluggableAuthenticator(props));
        // Set Security Manager
        //
        System.setSecurityManager(new SecurityManager());
        // Create an RMI connector server
        //
        System.out.println("Create an RMI connector server");
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0, "/jndi/rmi://:" + port + "/server" + port);
        jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        jmxcs.start();
        // Create an RMI connector client
        //
        System.out.println("Create an RMI connector client");
        HashMap cli_env = new HashMap();
        // These credentials must match those in the default password file
        //
        String[] credentials = new String[] { "monitorRole", "QED" };
        cli_env.put("jmx.remote.credentials", credentials);
        jmxc = JMXConnectorFactory.connect(url, cli_env);
        Subject delegationSubject = new Subject(true, Collections.singleton(new JMXPrincipal("delegate")), Collections.EMPTY_SET, Collections.EMPTY_SET);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(delegationSubject);
        // Get domains from MBeanServer
        //
        System.out.println("Domains:");
        String[] domains = mbsc.getDomains();
        for (int i = 0; i < domains.length; i++) {
            System.out.println("\tDomain[" + i + "] = " + domains[i]);
        }
        // Get MBean count
        //
        System.out.println("MBean count = " + mbsc.getMBeanCount());
        // Get State attribute
        //
        String oldState = (String) mbsc.getAttribute(new ObjectName("MBeans:type=SimpleStandard"), "State");
        System.out.println("Old State = \"" + oldState + "\"");
        // Set State attribute
        //
        System.out.println("Set State to \"changed state\"");
        mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"), new Attribute("State", "changed state"));
        // Get State attribute
        //
        String newState = (String) mbsc.getAttribute(new ObjectName("MBeans:type=SimpleStandard"), "State");
        System.out.println("New State = \"" + newState + "\"");
        if (!newState.equals("changed state")) {
            System.out.println("Invalid State = \"" + newState + "\"");
            System.exit(1);
        }
        // Add notification listener on SimpleStandard MBean
        //
        System.out.println("Add notification listener...");
        mbsc.addNotificationListener(new ObjectName("MBeans:type=SimpleStandard"), new NotificationListener() {

            public void handleNotification(Notification notification, Object handback) {
                System.out.println("Received notification: " + notification);
            }
        }, null, null);
        // Unregister SimpleStandard MBean
        //
        System.out.println("Unregister SimpleStandard MBean...");
        mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));
    } catch (SecurityException e) {
        if (testResult.equals("ko")) {
            System.out.println("Got expected security exception = " + e);
        } else {
            System.out.println("Got unexpected security exception = " + e);
            e.printStackTrace();
            throw e;
        }
    } catch (Exception e) {
        System.out.println("Unexpected exception caught = " + e);
        e.printStackTrace();
        throw e;
    } finally {
        //
        if (jmxc != null)
            jmxc.close();
        //
        if (jmxcs != null)
            jmxcs.stop();
        // Say goodbye
        //
        System.out.println("Bye! Bye!");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXPluggableAuthenticator(com.sun.jmx.remote.security.JMXPluggableAuthenticator) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JMXPrincipal(javax.management.remote.JMXPrincipal) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) Properties(java.util.Properties) Subject(javax.security.auth.Subject) Notification(javax.management.Notification) RemoteException(java.rmi.RemoteException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) RemoteException(java.rmi.RemoteException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 3 with NotificationListener

use of javax.management.NotificationListener 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 4 with NotificationListener

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

the class ConnectionListenerNullTest method test.

public static int test(String... urls) {
    int errCount = 0;
    for (int i = 0; i < urls.length; i++) {
        try {
            final JMXServiceURL url = new JMXServiceURL(urls[i]);
            final JMXConnector c = JMXConnectorFactory.newJMXConnector(url, (Map<String, String>) null);
            final NotificationListener nl = null;
            final NotificationFilter nf = null;
            final Object h = null;
            System.out.println("Testing " + c.getClass().getName());
            try {
                System.out.println("addConnectionNotificationListener(null,null,null)");
                c.addConnectionNotificationListener(nl, nf, h);
                throw new AssertionError("No exception raised");
            } catch (NullPointerException npe) {
            // OK.
            }
            final NotificationListener listener = new NotificationListener() {

                public void handleNotification(Notification notification, Object handback) {
                }
            };
            c.addConnectionNotificationListener(listener, nf, h);
            try {
                System.out.println("removeConnectionNotificationListener(null)");
                c.removeConnectionNotificationListener(nl);
                throw new AssertionError("No exception raised");
            } catch (NullPointerException npe) {
            // OK.
            }
            try {
                System.out.println("removeConnectionNotificationListener(null,null,null)");
                c.removeConnectionNotificationListener(nl, nf, h);
                throw new AssertionError("No exception raised");
            } catch (NullPointerException npe) {
            // OK.
            }
            c.removeConnectionNotificationListener(listener);
            System.out.println(c.getClass().getName() + " successfully tested.");
        } catch (Exception x) {
            System.err.println("Unexpected exception for " + urls[i] + ": " + x);
            x.printStackTrace();
            errCount++;
        } catch (AssertionError e) {
            System.err.println("Unexpected assertion error for " + urls[i] + ": " + e);
            e.printStackTrace();
            errCount++;
        }
    }
    return errCount;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) NotificationFilter(javax.management.NotificationFilter) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener)

Example 5 with NotificationListener

use of javax.management.NotificationListener 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

NotificationListener (javax.management.NotificationListener)52 Notification (javax.management.Notification)36 ObjectName (javax.management.ObjectName)18 MBeanServer (javax.management.MBeanServer)11 NotificationFilter (javax.management.NotificationFilter)10 JMXConnector (javax.management.remote.JMXConnector)10 JMXServiceURL (javax.management.remote.JMXServiceURL)10 AttributeChangeNotification (javax.management.AttributeChangeNotification)9 MBeanServerConnection (javax.management.MBeanServerConnection)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 NotificationEmitter (javax.management.NotificationEmitter)8 JMXConnectorServer (javax.management.remote.JMXConnectorServer)8 Attribute (javax.management.Attribute)7 Test (org.junit.Test)6 JMXPluggableAuthenticator (com.sun.jmx.remote.security.JMXPluggableAuthenticator)5 RemoteException (java.rmi.RemoteException)5 LocateRegistry (java.rmi.registry.LocateRegistry)5 Registry (java.rmi.registry.Registry)5 Properties (java.util.Properties)5