Search in sources :

Example 46 with JMXConnectorServer

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

the class NotificationBufferCreationTest method dotest.

private static void dotest(String protocol, NotificationSender s, ObjectName notifierName) throws Exception {
    JMXConnector client = null;
    JMXConnectorServer server = null;
    JMXServiceURL u = null;
    try {
        u = new JMXServiceURL(protocol, null, 0);
        server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
        checkNotifier(s, 0, "new ConnectorServer");
        server.start();
        checkNotifier(s, 0, "ConnectorServer start");
        JMXServiceURL addr = server.getAddress();
        client = JMXConnectorFactory.newJMXConnector(addr, null);
        checkNotifier(s, 0, "new Connector");
        client.connect(null);
        checkNotifier(s, 0, "Connector connect");
        MBeanServerConnection mbsc = client.getMBeanServerConnection();
        final NotificationListener dummyListener = new NotificationListener() {

            public void handleNotification(Notification n, Object o) {
                // do nothing
                return;
            }
        };
        mbsc.addNotificationListener(notifierName, dummyListener, null, null);
        // 1 Listener is expected to be added by the ServerNotifForwader
        checkNotifier(s, 1, "addNotificationListener");
        mbsc.removeNotificationListener(notifierName, dummyListener);
        System.out.println("Test OK for " + protocol);
    } catch (MalformedURLException e) {
        System.out.println("Skipping URL " + u);
    } finally {
        if (client != null)
            client.close();
        if (server != null)
            server.stop();
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Notification(javax.management.Notification) JMXConnectorServer(javax.management.remote.JMXConnectorServer) NotificationListener(javax.management.NotificationListener)

Example 47 with JMXConnectorServer

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

the class RMIAltAuthTest method main.

public static void main(String[] args) {
    try {
        // Override the default JAAS configuration
        //
        final String loginConfig = System.getProperty("test.src") + File.separator + "jmxremote.login";
        System.out.println("JAAS configuration file = " + loginConfig);
        System.setProperty("java.security.auth.login.config", "file:" + loginConfig);
        // 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 username/password entries
        //
        Properties props = new Properties();
        props.setProperty("jmx.remote.x.login.config", "PasswordFileAuthentication");
        // 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", "pwd1" };
        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 48 with JMXConnectorServer

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

the class MXBeanNotifTest method run.

public void run(Map<String, Object> args) {
    System.out.println("MXBeanNotifTest::run: Start");
    int errorCount = 0;
    try {
        parseArgs(args);
        notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        JMXConnector cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
        // ----
        System.out.println("MXBeanNotifTest::run: Create and register the MBean");
        ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi");
        mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
        System.out.println("---- OK\n");
        // ----
        System.out.println("MXBeanNotifTest::run: Add me as notification listener");
        mbsc.addNotificationListener(objName, this, null, null);
        // ----
        System.out.println("MXBeanNotifTest::run: Retrieve the Descriptor" + " that should be in MBeanNotificationInfo");
        TabularData tabData = (TabularData) mbsc.getAttribute(objName, "NotifDescriptorAsMapAtt");
        Map<String, String> descrMap = new HashMap<>();
        for (Iterator<?> it = tabData.values().iterator(); it.hasNext(); ) {
            CompositeData compData = (CompositeData) it.next();
            descrMap.put((String) compData.get("key"), (String) compData.get("value"));
        }
        Descriptor refNotifDescriptor = new ImmutableDescriptor(descrMap);
        System.out.println("---- OK\n");
        // ----
        // Because the MBean holding the targeted attribute is MXBean, we
        // should use for the setAttribute a converted form for the
        // attribute value as described by the MXBean mapping rules.
        // This explains all that lovely stuff for creating a
        // TabularDataSupport.
        //
        // WARNING : the MBeanInfo of the MXBean used on opposite side
        // is computed when the MBean is registered.
        // It means the Descriptor considered for the MBeanNotificationInfo
        // is not the one we set in the lines below, it is too late.
        // However, we check that set is harmless when we check
        // the MBeanNotificationInfo.
        //
        System.out.println("MXBeanNotifTest::run: Set a Map<String, String>" + " attribute");
        String typeName = "java.util.Map<java.lang.String,java.lang.String>";
        String[] keyValue = new String[] { "key", "value" };
        OpenType<?>[] openTypes = new OpenType<?>[] { SimpleType.STRING, SimpleType.STRING };
        CompositeType rowType = new CompositeType(typeName, typeName, keyValue, keyValue, openTypes);
        TabularType tabType = new TabularType(typeName, typeName, rowType, new String[] { "key" });
        TabularDataSupport convertedDescrMap = new TabularDataSupport(tabType);
        for (int i = 0; i < numOfNotifDescriptorElements; i++) {
            Object[] descrValue = { "field" + i, "value" + i };
            CompositeData data = new CompositeDataSupport(rowType, keyValue, descrValue);
            convertedDescrMap.put(data);
        }
        Attribute descrAtt = new Attribute("NotifDescriptorAsMapAtt", convertedDescrMap);
        mbsc.setAttribute(objName, descrAtt);
        System.out.println("---- OK\n");
        // ----
        System.out.println("MXBeanNotifTest::run: Compare the Descriptor from" + " the MBeanNotificationInfo against a reference");
        MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
        errorCount += checkMBeanInfo(mbInfo, refNotifDescriptor);
        System.out.println("---- DONE\n");
        // ----
        System.out.println("Check isInstanceOf(Basic)");
        if (!mbsc.isInstanceOf(objName, BASIC_MXBEAN_CLASS_NAME)) {
            errorCount++;
            System.out.println("---- ERROR isInstanceOf returned false\n");
        } else {
            System.out.println("---- OK\n");
        }
        // ----
        System.out.println("Check isInstanceOf(BasicMXBean)");
        if (!mbsc.isInstanceOf(objName, BASIC_MXBEAN_INTERFACE_NAME)) {
            errorCount++;
            System.out.println("---- ERROR isInstanceOf returned false\n");
        } else {
            System.out.println("---- OK\n");
        }
        // ----
        System.out.println("MXBeanNotifTest::run: Ask for " + numOfNotifications + " notification(s)");
        Object[] sendNotifParam = new Object[1];
        String[] sendNotifSig = new String[] { "java.lang.String" };
        for (int i = 0; i < numOfNotifications; i++) {
            // Select which type of notification we ask for
            if (i % 2 == 0) {
                sendNotifParam[0] = Basic.NOTIF_TYPE_0;
            } else {
                sendNotifParam[0] = Basic.NOTIF_TYPE_1;
            }
            // Trigger notification emission
            mbsc.invoke(objName, "sendNotification", sendNotifParam, sendNotifSig);
            // Wait for it then check it when it comes early enough
            Notification notif = notifList.poll(timeForNotificationInSeconds, TimeUnit.SECONDS);
            // notifications are delivered with, we prefer to secure it.
            if (i == 0 && notif == null) {
                System.out.println("MXBeanNotifTest::run: Wait extra " + timeForNotificationInSeconds + " second(s) the " + " very first notification");
                notif = notifList.poll(timeForNotificationInSeconds, TimeUnit.SECONDS);
            }
            if (notif == null) {
                errorCount++;
                System.out.println("---- ERROR No notification received" + " within allocated " + timeForNotificationInSeconds + " second(s) !");
            } else {
                errorCount += checkNotification(notif, (String) sendNotifParam[0], Basic.NOTIFICATION_MESSAGE, objName);
            }
        }
        int toc = 0;
        while (notifList.size() < 2 && toc < 10) {
            Thread.sleep(499);
            toc++;
        }
        System.out.println("---- DONE\n");
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException(e);
    }
    if (errorCount == 0) {
        System.out.println("MXBeanNotifTest::run: Done without any error");
    } else {
        System.out.println("MXBeanNotifTest::run: Done with " + errorCount + " error(s)");
        throw new RuntimeException("errorCount = " + errorCount);
    }
}
Also used : OpenType(javax.management.openmbean.OpenType) ImmutableDescriptor(javax.management.ImmutableDescriptor) MBeanInfo(javax.management.MBeanInfo) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) CompositeData(javax.management.openmbean.CompositeData) TabularType(javax.management.openmbean.TabularType) Notification(javax.management.Notification) TabularData(javax.management.openmbean.TabularData) JMXConnector(javax.management.remote.JMXConnector) MBeanServer(javax.management.MBeanServer) JMXServiceURL(javax.management.remote.JMXServiceURL) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) TabularDataSupport(javax.management.openmbean.TabularDataSupport) Descriptor(javax.management.Descriptor) ImmutableDescriptor(javax.management.ImmutableDescriptor) MBeanServerConnection(javax.management.MBeanServerConnection) CompositeType(javax.management.openmbean.CompositeType)

Example 49 with JMXConnectorServer

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

the class MXBeanTest method testInterface.

/* This is the core of the test.  Given the MXBean interface c, we
       make an MXBean object that implements that interface by
       constructing a dynamic proxy.  If the interface defines an
       attribute Foo (with getFoo and setFoo methods), then it must
       also contain a field (constant) Foo of the same type, and a
       field (constant) FooType that is an OpenType.  The field Foo is
       a reference value for this case.  We check that the attribute
       does indeed have the given OpenType.  The dynamically-created
       MXBean will return the reference value from the getFoo()
       method, and we check that that value survives the mapping to
       open values and back when the attribute is accessed through an
       MXBean proxy.  The MXBean will also check in its setFoo method
       that the value being set is equal to the reference value, which
       tests that the mapping and unmapping also works in the other
       direction.  The interface should define an operation opFoo with
       two parameters and a return value all of the same type as the
       attribute.  The MXBean will check that the two parameters are
       equal to the reference value, and will return that value.  The
       test checks that calling the operation through an MXBean proxy
       returns the reference value, again after mapping to and back
       from open values.

       If any field (constant) in the MXBean interface has a name that
       ends with ObjectName, say FooObjectName, then its value must be
       a String containing an ObjectName value.  There must be a field
       (constant) called Foo that is a valid MXBean, and that MXBean
       will be registered in the MBean Server with the given name before
       the test starts.  This enables us to test that inter-MXBean
       references are correctly converted to ObjectNames and back.
     */
private static <T> void testInterface(Class<T> c, boolean nullTest) throws Exception {
    System.out.println("Testing " + c.getName() + (nullTest ? " for null values" : "") + "...");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();
    NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
    InvocationHandler ih = nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) : new MXBeanImplInvocationHandler(c, namedMXBeans);
    T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(), new Class[] { c }, ih));
    ObjectName on = new ObjectName("test:type=" + c.getName());
    mbs.registerMBean(impl, on);
    System.out.println("Register any MXBeans...");
    Field[] fields = c.getFields();
    for (Field field : fields) {
        String n = field.getName();
        if (n.endsWith("ObjectName")) {
            String objectNameString = (String) field.get(null);
            String base = n.substring(0, n.length() - 10);
            Field f = c.getField(base);
            Object mxbean = f.get(null);
            ObjectName objectName = ObjectName.getInstance(objectNameString);
            mbs.registerMBean(mxbean, objectName);
            namedMXBeans.put(objectName, mxbean);
        }
    }
    try {
        testInterface(c, mbsc, on, namedMXBeans, nullTest);
    } finally {
        try {
            cc.close();
        } finally {
            cs.stop();
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) CompositeDataInvocationHandler(javax.management.openmbean.CompositeDataInvocationHandler) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) Field(java.lang.reflect.Field) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 50 with JMXConnectorServer

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

the class MXBeanInteropTest2 method run.

public void run(Map<String, Object> args) {
    System.out.println("MXBeanInteropTest2::run: Start");
    int errorCount = 0;
    try {
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        JMXConnector cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
        // Prints all MBeans whatever the domain is.
        printMBeans(mbsc);
        // Call test body
        errorCount += doBasicMXBeanTest(mbsc);
        // Terminate the JMX Client
        cc.close();
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException(e);
    }
    if (errorCount == 0) {
        System.out.println("MXBeanInteropTest2::run: Done without any error");
    } else {
        System.out.println("MXBeanInteropTest2::run: Done with " + errorCount + " error(s)");
        throw new RuntimeException("errorCount = " + errorCount);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

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