Search in sources :

Example 6 with JMXConnectorServer

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

the class RMISocketFactoriesTest method main.

public static void main(String[] args) {
    System.out.println("Test RMI factories : " + args[0]);
    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);
        }
        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new RMIServerFactory(args[0]));
        env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new RMIClientFactory(args[0]));
        // 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);
        JMXConnectorServer rcs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        rcs.start();
        // Create an RMI connector client
        //
        System.out.println("Create an RMI connector client");
        JMXConnector jmxc = JMXConnectorFactory.connect(url, new HashMap());
        // If this line is executed then the test failed
        //
        System.exit(1);
    } catch (Exception e) {
        if (e.getMessage().equals(args[0])) {
            System.out.println("Expected exception caught = " + e);
            System.out.println("Bye! Bye!");
        } else {
            System.out.println("Unexpected exception caught = " + e);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RemoteException(java.rmi.RemoteException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 7 with JMXConnectorServer

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

the class ConcurrentModificationTest method test.

private static void test(String proto) throws Exception {
    JMXServiceURL u = new JMXServiceURL(proto, null, 0);
    JMXConnectorServer server;
    JMXConnector client;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
        server.start();
        JMXServiceURL addr = server.getAddress();
        client = JMXConnectorFactory.connect(addr, null);
    } catch (MalformedURLException e) {
        System.out.println(">>> not support: " + proto);
        return;
    }
    final MBeanServerConnection mserver = client.getMBeanServerConnection();
    int count = 0;
    boolean adding = true;
    while (uncaughtException == null && count++ < 10) {
        for (int i = 0; i < number; i++) {
            listenerOp(mserver, listeners[i], adding);
            mbeanOp(mserver, timerNames[i], adding);
        }
        adding = !adding;
    }
    if (uncaughtException != null) {
        // clean
        for (int i = 0; i < number; i++) {
            try {
                mbeanOp(mserver, timerNames[i], false);
            } catch (Exception e) {
            }
        }
    }
    client.close();
    server.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MalformedURLException(java.net.MalformedURLException) ConcurrentModificationException(java.util.ConcurrentModificationException) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 8 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer 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 9 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer 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 10 with JMXConnectorServer

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

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