Search in sources :

Example 21 with JMXConnector

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

the class EmptyDomainNotificationTest method main.

public static void main(String[] args) throws Exception {
    final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    server.start();
    JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
    final MBeanServerConnection mbsc = client.getMBeanServerConnection();
    final ObjectName mbean = ObjectName.getInstance(":type=Simple");
    mbsc.createMBean(Simple.class.getName(), mbean);
    System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
    final Listener li = new Listener();
    mbsc.addNotificationListener(mbean, li, null, null);
    System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
    mbsc.invoke(mbean, "emitNotification", null, null);
    System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
    final long stopTime = System.currentTimeMillis() + 2000;
    synchronized (li) {
        long toWait = stopTime - System.currentTimeMillis();
        while (li.received < 1 && toWait > 0) {
            li.wait(toWait);
            toWait = stopTime - System.currentTimeMillis();
        }
    }
    if (li.received < 1) {
        throw new RuntimeException("No notif received!");
    } else if (li.received > 1) {
        throw new RuntimeException("Wait one notif but got: " + li.received);
    }
    System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
    System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
    mbsc.removeNotificationListener(mbean, li);
    // clean
    client.close();
    server.stop();
    System.out.println("EmptyDomainNotificationTest-main: Bye.");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) NotificationListener(javax.management.NotificationListener) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName)

Example 22 with JMXConnector

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

the class NotificationEmissionTest method testNotificationEmission.

public int testNotificationEmission(boolean prop, Object propValue, boolean sm, boolean policyPositive) throws Exception {
    JMXConnectorServer server = null;
    JMXConnector client = null;
    // Set policy file
    //
    String policyFile = System.getProperty("test.src") + File.separator + (policyPositive ? "policy.positive" : "policy.negative");
    echo("\nSetting policy file " + policyFile);
    System.setProperty("java.security.policy", policyFile);
    // Create a new MBeanServer
    //
    final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    try {
        // Create server environment map
        //
        final Map<String, Object> env = new HashMap<String, Object>();
        env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
        if (prop)
            env.put("jmx.remote.x.check.notification.emission", propValue);
        // 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, Object>();
        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
        //
        Listener li = new Listener();
        mbsc.addNotificationListener(nb1, li, null, null);
        mbsc.addNotificationListener(nb2, li, null, null);
        //
        if (sm) {
            echo("Setting SM");
            System.setSecurityManager(new SecurityManager());
        }
        // 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" });
        // If the check is effective and we're using policy.negative,
        // then we should see the two notifs sent by nb2 (of which one
        // has a getSource() that is nb3), but not the notif sent by nb1.
        // Otherwise we should see all three notifs.  The check is only
        // effective if the property jmx.remote.x.check.notification.emission
        // is explicitly true and there is a security manager.
        int expectedNotifs = (prop && sm && !policyPositive) ? 2 : 3;
        // Wait for notifications to be emitted
        //
        long deadline = System.currentTimeMillis() + 2000;
        while (li.notifs.size() < expectedNotifs && System.currentTimeMillis() < deadline) Thread.sleep(1);
        // Remove notification listener
        //
        mbsc.removeNotificationListener(nb1, li);
        mbsc.removeNotificationListener(nb2, li);
        int result = 0;
        List<ObjectName> sources = new ArrayList<ObjectName>();
        sources.add(nb1);
        sources.add(nb2);
        sources.add(nb3);
        result = checkNotifs(expectedNotifs, li.notifs, sources);
        if (result > 0) {
            echo("...SecurityManager=" + sm + "; policy=" + policyPositive);
            return result;
        }
    } 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) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 23 with JMXConnector

use of javax.management.remote.JMXConnector 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 24 with JMXConnector

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

the class IIOPURLTest method main.

public static void main(String[] args) throws Exception {
    JMXServiceURL inputAddr = new JMXServiceURL("service:jmx:iiop://");
    JMXConnectorServer s;
    try {
        s = JMXConnectorServerFactory.newJMXConnectorServer(inputAddr, null, null);
    } catch (java.net.MalformedURLException x) {
        try {
            Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie");
            throw new RuntimeException("MalformedURLException thrown but iiop appears to be supported");
        } catch (ClassNotFoundException expected) {
        }
        System.out.println("IIOP protocol not supported, test skipped");
        return;
    }
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    mbs.registerMBean(s, new ObjectName("a:b=c"));
    s.start();
    JMXServiceURL outputAddr = s.getAddress();
    if (!outputAddr.getURLPath().startsWith("/ior/IOR:")) {
        throw new RuntimeException("URL path should start with \"/ior/IOR:\": " + outputAddr);
    }
    System.out.println("IIOP URL path looks OK: " + outputAddr);
    JMXConnector c = JMXConnectorFactory.connect(outputAddr);
    System.out.println("Successfully got default domain: " + c.getMBeanServerConnection().getDefaultDomain());
    c.close();
    s.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) JMXConnectorServer(javax.management.remote.JMXConnectorServer) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 25 with JMXConnector

use of javax.management.remote.JMXConnector 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)

Aggregations

JMXConnector (javax.management.remote.JMXConnector)118 MBeanServerConnection (javax.management.MBeanServerConnection)85 JMXServiceURL (javax.management.remote.JMXServiceURL)78 ObjectName (javax.management.ObjectName)54 JMXConnectorServer (javax.management.remote.JMXConnectorServer)47 MBeanServer (javax.management.MBeanServer)37 IOException (java.io.IOException)35 HashMap (java.util.HashMap)27 Test (org.junit.Test)22 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 Attribute (javax.management.Attribute)13 MalformedURLException (java.net.MalformedURLException)12 RemoteException (java.rmi.RemoteException)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 LocateRegistry (java.rmi.registry.LocateRegistry)8 Registry (java.rmi.registry.Registry)8 Properties (java.util.Properties)7