Search in sources :

Example 21 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project spring-framework by spring-projects.

the class MBeanServerConnectionFactoryBeanTests method testTestWithLazyConnection.

@Test
public void testTestWithLazyConnection() throws Exception {
    Assume.group(TestGroup.JMXMP);
    MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
    bean.setServiceUrl(serviceUrl);
    bean.setConnectOnStartup(false);
    bean.afterPropertiesSet();
    MBeanServerConnection connection = bean.getObject();
    assertTrue(AopUtils.isAopProxy(connection));
    JMXConnectorServer connector = null;
    try {
        connector = getConnectorServer();
        connector.start();
        assertEquals("Incorrect MBean count", getServer().getMBeanCount(), connection.getMBeanCount());
    } finally {
        bean.destroy();
        if (connector != null) {
            connector.stop();
        }
    }
}
Also used : MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer) Test(org.junit.Test)

Example 22 with MBeanServerConnection

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

the class JMXServerErrorTest method test.

public void test(String url) throws Exception {
    final JMXServiceURL jurl = new JMXServiceURL(url);
    final ObjectName kname = new ObjectName(":type=Kaeffer");
    final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    final String that = "that";
    mbs.registerMBean(new Kaeffer(that), kname);
    final MBeanServer kbs = new MBeanServerKaeffer(mbs);
    final JMXConnectorServer cs;
    try {
        cs = JMXConnectorServerFactory.newJMXConnectorServer(jurl, null, kbs);
    } catch (MalformedURLException m) {
        if ("jmxmp".equals(jurl.getProtocol()) || "iiop".equals(jurl.getProtocol())) {
            // OK, we may not have this in the classpath...
            System.out.println("WARNING: Skipping protocol: " + jurl);
            return;
        }
        throw m;
    }
    final ObjectName cname = new ObjectName(":type=JMXConnectorServer");
    mbs.registerMBean(cs, cname);
    cs.start();
    JMXConnector c = null;
    try {
        c = JMXConnectorFactory.connect(cs.getAddress(), null);
        final MBeanServerConnection mbsc = c.getMBeanServerConnection();
        final KaefferMBean kaeffer = (KaefferMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, kname, KaefferMBean.class, false);
        final String that1 = kaeffer.getThis();
        if (!that.equals(that1))
            throw new Exception("Unexpected string returned by " + kname + ": " + that1);
        try {
            kaeffer.setThis("but not that");
            throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for setAttribute \"This\" ");
        } catch (JMXServerErrorException jsee) {
            if (!(jsee.getCause() instanceof KaefferError)) {
                final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
                e.initCause(jsee);
                throw e;
            }
            System.out.println("Got expected error: " + jsee);
        }
        try {
            kaeffer.doThis("but not that");
            throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for invoke \"doThis\" ");
        } catch (JMXServerErrorException jsee) {
            if (!(jsee.getCause() instanceof KaefferError)) {
                final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
                e.initCause(jsee);
                throw e;
            }
            System.out.println("Got expected error: " + jsee);
        }
    } finally {
        if (c != null)
            try {
                c.close();
            } catch (Exception x) {
                System.err.println("Failed to close client: " + x);
                throw x;
            }
        try {
            cs.stop();
        } catch (Exception x) {
            System.err.println("Failed to stop server: " + x);
            throw x;
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXServerErrorException(javax.management.remote.JMXServerErrorException) MalformedURLException(java.net.MalformedURLException) JMXServerErrorException(javax.management.remote.JMXServerErrorException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 23 with MBeanServerConnection

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

use of javax.management.MBeanServerConnection 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 25 with MBeanServerConnection

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

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