Search in sources :

Example 81 with JMXConnector

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

the class ExceptionTest method run.

public void run(Map<String, Object> args) {
    System.out.println("ExceptionTest::run: Start");
    int errorCount = 0;
    JMXConnectorServer cs = null;
    JMXConnector cc = null;
    try {
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
        // ----
        ObjectName objName = new ObjectName(ExceptionThrower.EXCEPTION_THROWER_NAME);
        System.out.println("ExceptionTest::run: Create and register MBean " + objName);
        mbsc.createMBean("ExceptionThrower", objName);
        System.out.println("---- OK\n");
        // ----
        System.out.println("ExceptionTest::run: Ask for exception(s)");
        Object[] throwExceptionParam = new Object[1];
        String[] throwExceptionSig = new String[] { "int" };
        for (int i = 0; i < ExceptionFactory.exceptions.size(); i++) {
            throwExceptionParam[0] = new Integer(i);
            Exception ex = (Exception) mbsc.invoke(objName, "throwException", throwExceptionParam, throwExceptionSig);
            if (!matches(ex, ExceptionFactory.exceptions.get(i))) {
                errorCount++;
                System.out.println("ExceptionTest::run: (ERROR) Received \n[" + ex + "]\nin place of\n[" + ExceptionFactory.exceptions.get(i) + "]");
            } else {
                System.out.println("OK [" + ex + "]");
            }
        }
        System.out.println("---- DONE\n");
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException();
    } finally {
        try {
            // Close JMX Connector Client
            cc.close();
            // Stop connertor server
            cs.stop();
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            throw new RuntimeException("Unable to either close connector client or stop connector server");
        }
    }
    if (errorCount == 0) {
        System.out.println("ExceptionTest::run: Done without any error");
    } else {
        System.out.println("ExceptionTest::run: Done with " + errorCount + " error(s)");
        throw new RuntimeException("errorCount = " + errorCount);
    }
    System.out.println("ExceptionTest::run: Done");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 82 with JMXConnector

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

the class RMIPasswdAuthTest method main.

public static void main(String[] args) {
    try {
        // Set the default password file
        //
        final String passwordFile = System.getProperty("test.src") + File.separator + "jmxremote.password";
        System.out.println("Password file = " + passwordFile);
        // 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();
        // Register the ClassPathClassLoaderMBean
        //
        System.out.println("Create ClassPathClassLoader MBean");
        ObjectName cpcl = new ObjectName("ClassLoader:name=ClassPathClassLoader");
        mbs.createMBean("javax.management.loading.MLet", cpcl);
        // Register the SimpleStandardMBean
        //
        System.out.println("Create SimpleStandard MBean");
        mbs.createMBean("SimpleStandard", new ObjectName("MBeans:name=SimpleStandard"));
        // Create Properties containing the location of the password file
        //
        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));
        // 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");
        HashMap cli_env = new HashMap();
        // These credentials must match those in the supplied password file
        //
        String[] credentials = new String[] { "monitorRole", "QED" };
        cli_env.put("jmx.remote.credentials", credentials);
        JMXConnector 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:name=SimpleStandard"), "State");
        System.out.println("Old State = \"" + oldState + "\"");
        // Set State attribute
        //
        System.out.println("Set State to \"changed state\"");
        mbsc.setAttribute(new ObjectName("MBeans:name=SimpleStandard"), new Attribute("State", "changed state"));
        // Get State attribute
        //
        String newState = (String) mbsc.getAttribute(new ObjectName("MBeans:name=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:name=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:name=SimpleStandard"));
        // Close MBeanServer connection
        //
        jmxc.close();
        System.out.println("Bye! Bye!");
    } catch (Exception e) {
        System.out.println("Unexpected exception caught = " + e);
        e.printStackTrace();
        System.exit(1);
    }
}
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) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) RemoteException(java.rmi.RemoteException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 83 with JMXConnector

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

the class ProviderTest method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting ProviderTest");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    // First do the test with a protocol handled by Service providers
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    dotest(url, mbs);
    boolean clientCalled = provider.JMXConnectorProviderImpl.called();
    boolean serverCalled = provider.JMXConnectorServerProviderImpl.called();
    boolean ok = clientCalled && serverCalled;
    if (!ok) {
        if (!clientCalled)
            System.out.println("Client provider not called");
        if (!serverCalled)
            System.out.println("Server provider not called");
        throw new RuntimeException("Test failed - see log for details");
    }
    // The Service Provider doesn't handle IIOP. Default providers MUST
    // be called, which may or may not support IIOP.
    url = new JMXServiceURL("service:jmx:iiop://");
    try {
        dotest(url, mbs);
    } catch (MalformedURLException e) {
        try {
            Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie");
            e.printStackTrace(System.out);
            throw new RuntimeException("MalformedURLException throw but IIOP appears to be supported");
        } catch (ClassNotFoundException expected) {
        }
        System.out.println("MalformedURLException thrown, IIOP transport not supported");
    }
    // Unsupported protocol.
    JMXConnectorServer server = null;
    JMXConnector client = null;
    url = new JMXServiceURL("service:jmx:unknown-protocol://");
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        throw new RuntimeException("Exception not thrown.");
    } catch (MalformedURLException e) {
        System.out.println("Expected MalformedURLException thrown.");
    }
    try {
        client = JMXConnectorFactory.newJMXConnector(url, null);
        throw new RuntimeException("Exception not thrown.");
    } catch (MalformedURLException e) {
        System.out.println("Expected MalformedURLException thrown.");
    }
    //JMXConnectorProviderException
    url = new JMXServiceURL("service:jmx:throw-provider-exception://");
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        throw new RuntimeException("Exception not thrown.");
    } catch (JMXProviderException e) {
        System.out.println("Expected JMXProviderException thrown.");
    }
    try {
        client = JMXConnectorFactory.newJMXConnector(url, null);
        throw new RuntimeException("Exception not thrown.");
    } catch (JMXProviderException e) {
        System.out.println("Expected JMXProviderException thrown.");
    }
    System.out.println("Test OK");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXProviderException(javax.management.remote.JMXProviderException) MalformedURLException(java.net.MalformedURLException) JMXConnector(javax.management.remote.JMXConnector) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 84 with JMXConnector

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

the class ProviderTest method dotest.

private static void dotest(JMXServiceURL url, MBeanServer mbs) throws Exception {
    JMXConnectorServer server = null;
    JMXConnector client = null;
    server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    server.start();
    JMXServiceURL outputAddr = server.getAddress();
    System.out.println("Server started [" + outputAddr + "]");
    client = JMXConnectorFactory.newJMXConnector(outputAddr, null);
    client.connect();
    System.out.println("Client connected");
    MBeanServerConnection connection = client.getMBeanServerConnection();
    System.out.println(connection.getDefaultDomain());
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 85 with JMXConnector

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

the class NotSerializableNotifTest method test.

private static void test(String proto) throws Exception {
    System.out.println("\n>>> Test for protocol " + proto);
    JMXServiceURL url = new JMXServiceURL(proto, null, 0);
    System.out.println(">>> Create a server: " + url);
    JMXConnectorServer server = null;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
    } catch (MalformedURLException e) {
        System.out.println("System does not recognize URL: " + url + "; ignoring");
        return;
    }
    server.start();
    url = server.getAddress();
    System.out.println(">>> Creating a client connectint to: " + url);
    JMXConnector conn = JMXConnectorFactory.connect(url, null);
    MBeanServerConnection client = conn.getMBeanServerConnection();
    // add listener from the client side
    Listener listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);
    // ask to send one not serializable notif
    Object[] params = new Object[] { new Integer(1) };
    String[] signatures = new String[] { "java.lang.Integer" };
    client.invoke(emitter, "sendNotserializableNotifs", params, signatures);
    // listener clean
    client.removeNotificationListener(emitter, listener);
    listener = new Listener();
    client.addNotificationListener(emitter, listener, null, null);
    //ask to send serializable notifs
    params = new Object[] { new Integer(sentNotifs) };
    client.invoke(emitter, "sendNotifications", params, signatures);
    // waiting ...
    synchronized (listener) {
        while (listener.received() < sentNotifs) {
            // either pass or test timeout (killed by test harness)
            listener.wait();
        }
    }
    // clean
    client.removeNotificationListener(emitter, listener);
    conn.close();
    server.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) NotificationListener(javax.management.NotificationListener) 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