Search in sources :

Example 66 with CompositeType

use of javax.management.openmbean.CompositeType 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 67 with CompositeType

use of javax.management.openmbean.CompositeType in project jdk8u_jdk by JetBrains.

the class MXBeanLoadingTest1 method run.

public void run(Map<String, Object> args) {
    System.out.println("MXBeanLoadingTest1::run: Start");
    try {
        System.out.println("We ensure no reference is retained on MXBean class" + " after it is unregistered. We take time to perform" + " some little extra check of Descriptors, MBean*Info.");
        ClassLoader myClassLoader = MXBeanLoadingTest1.class.getClassLoader();
        if (!(myClassLoader instanceof URLClassLoader)) {
            String message = "(ERROR) Test's class loader is not " + "a URLClassLoader";
            System.out.println(message);
            throw new RuntimeException(message);
        }
        URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;
        URL[] urls = myURLClassLoader.getURLs();
        PrivateMLet mlet = new PrivateMLet(urls, null, false);
        Class<?> shadowClass = mlet.loadClass(TestMXBean.class.getName());
        if (shadowClass == TestMXBean.class) {
            String message = "(ERROR) MLet got original TestMXBean, not shadow";
            System.out.println(message);
            throw new RuntimeException(message);
        }
        shadowClass = null;
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        ObjectName mletName = new ObjectName("x:type=mlet");
        mbs.registerMBean(mlet, mletName);
        ObjectName testName = new ObjectName("x:type=test");
        mbs.createMBean(Test.class.getName(), testName, mletName);
        // That test fails because the MXBean instance is accessed via
        // a delegate OpenMBean which has
        ClassLoader testLoader = mbs.getClassLoaderFor(testName);
        if (testLoader != mlet) {
            System.out.println("MLet " + mlet);
            String message = "(ERROR) MXBean's class loader is not MLet: " + testLoader;
            System.out.println(message);
            throw new RuntimeException(message);
        }
        testLoader = null;
        // Cycle get/set/get of the attribute of type Luis.
        // We check the set is effective.
        CompositeData cd_B = (CompositeData) mbs.getAttribute(testName, "B");
        CompositeType compType_B = cd_B.getCompositeType();
        CompositeDataSupport cds_B = new CompositeDataSupport(compType_B, new String[] { "something" }, new Object[] { Integer.valueOf(13) });
        Attribute myAtt = new Attribute("B", cds_B);
        mbs.setAttribute(testName, myAtt);
        CompositeData cd_B2 = (CompositeData) mbs.getAttribute(testName, "B");
        if (((Integer) cd_B2.get("something")).intValue() != 13) {
            String message = "(ERROR) The setAttribute of att B did not work;" + " expect Luis.something = 13 but got " + cd_B2.get("something");
            System.out.println(message);
            throw new RuntimeException(message);
        }
        MBeanInfo info = mbs.getMBeanInfo(testName);
        String mxbeanField = (String) info.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);
        if (mxbeanField == null || !mxbeanField.equals("true")) {
            String message = "(ERROR) Improper mxbean field value " + mxbeanField;
            System.out.println(message);
            throw new RuntimeException(message);
        }
        // Check the 2 attributes.
        MBeanAttributeInfo[] attrs = info.getAttributes();
        if (attrs.length == 2) {
            for (MBeanAttributeInfo mbai : attrs) {
                String originalTypeFieldValue = (String) mbai.getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
                OpenType<?> openTypeFieldValue = (OpenType<?>) mbai.getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
                if (mbai.getName().equals("A")) {
                    if (!mbai.isReadable() || !mbai.isWritable() || mbai.isIs() || !mbai.getType().equals("int")) {
                        String message = "(ERROR) Unexpected MBeanAttributeInfo for A " + mbai;
                        System.out.println(message);
                        throw new RuntimeException(message);
                    }
                    if (!originalTypeFieldValue.equals("int")) {
                        String message = "(ERROR) Unexpected originalType in Descriptor for A " + originalTypeFieldValue;
                        System.out.println(message);
                        throw new RuntimeException(message);
                    }
                    if (!openTypeFieldValue.equals(SimpleType.INTEGER)) {
                        String message = "(ERROR) Unexpected openType in Descriptor for A " + originalTypeFieldValue;
                        System.out.println(message);
                        throw new RuntimeException(message);
                    }
                } else if (mbai.getName().equals("B")) {
                    if (!mbai.isReadable() || !mbai.isWritable() || mbai.isIs() || !mbai.getType().equals("javax.management.openmbean.CompositeData")) {
                        String message = "(ERROR) Unexpected MBeanAttributeInfo for B " + mbai;
                        System.out.println(message);
                        throw new RuntimeException(message);
                    }
                    if (!originalTypeFieldValue.equals(Luis.class.getName())) {
                        String message = "(ERROR) Unexpected originalType in Descriptor for B " + originalTypeFieldValue;
                        System.out.println(message);
                        throw new RuntimeException(message);
                    }
                    if (!openTypeFieldValue.equals(compType_B)) {
                        String message = "(ERROR) Unexpected openType in Descriptor for B " + compType_B;
                        System.out.println(message);
                        throw new RuntimeException(message);
                    }
                } else {
                    String message = "(ERROR) Unknown attribute name";
                    System.out.println(message);
                    throw new RuntimeException(message);
                }
            }
        } else {
            String message = "(ERROR) Unexpected MBeanAttributeInfo array" + Arrays.deepToString(attrs);
            System.out.println(message);
            throw new RuntimeException(message);
        }
        // Check the MXBean operation.
        MBeanOperationInfo[] ops = info.getOperations();
        // logged 6320104.
        if (ops.length != 1 || !ops[0].getName().equals("bogus") || ops[0].getSignature().length > 0 || !ops[0].getReturnType().equals("void")) {
            String message = "(ERROR) Unexpected MBeanOperationInfo array " + Arrays.deepToString(ops);
            System.out.println(message);
            throw new RuntimeException(message);
        }
        String originalTypeFieldValue = (String) ops[0].getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
        OpenType<?> openTypeFieldValue = (OpenType<?>) ops[0].getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
        if (!originalTypeFieldValue.equals("void")) {
            String message = "(ERROR) Unexpected originalType in Descriptor for bogus " + originalTypeFieldValue;
            System.out.println(message);
            throw new RuntimeException(message);
        }
        if (!openTypeFieldValue.equals(SimpleType.VOID)) {
            String message = "(ERROR) Unexpected openType in Descriptor for bogus " + originalTypeFieldValue;
            System.out.println(message);
            throw new RuntimeException(message);
        }
        // Check there is 2 constructors.
        if (info.getConstructors().length != 2) {
            String message = "(ERROR) Wrong number of constructors " + "in introspected bean: " + Arrays.asList(info.getConstructors());
            System.out.println(message);
            throw new RuntimeException(message);
        }
        // Check MXBean class name.
        if (!info.getClassName().endsWith("Test")) {
            String message = "(ERROR) Wrong info class name: " + info.getClassName();
            System.out.println(message);
            throw new RuntimeException(message);
        }
        mbs.unregisterMBean(testName);
        mbs.unregisterMBean(mletName);
        WeakReference<PrivateMLet> mletRef = new WeakReference<PrivateMLet>(mlet);
        mlet = null;
        System.out.println("MXBean registered and unregistered, waiting for " + "garbage collector to collect class loader");
        for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
            System.gc();
            Thread.sleep(1);
        }
        if (mletRef.get() == null)
            System.out.println("(OK) class loader was GC'd");
        else {
            String message = "(ERROR) Class loader was not GC'd";
            System.out.println(message);
            throw new RuntimeException(message);
        }
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException(e);
    }
    System.out.println("MXBeanLoadingTest1::run: Done without any error");
}
Also used : OpenType(javax.management.openmbean.OpenType) MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) CompositeData(javax.management.openmbean.CompositeData) URL(java.net.URL) WeakReference(java.lang.ref.WeakReference) URLClassLoader(java.net.URLClassLoader) PrivateMLet(javax.management.loading.PrivateMLet) MBeanServer(javax.management.MBeanServer) MBeanOperationInfo(javax.management.MBeanOperationInfo) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) URLClassLoader(java.net.URLClassLoader) CompositeType(javax.management.openmbean.CompositeType)

Example 68 with CompositeType

use of javax.management.openmbean.CompositeType in project jdk8u_jdk by JetBrains.

the class PropertyNamesTest method main.

public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName pointName = new ObjectName("a:type=Point");
    PointMXBean pointmx = new PointImpl();
    mbs.registerMBean(pointmx, pointName);
    Point point = new Point(1, 2);
    PointMXBean pointproxy = JMX.newMXBeanProxy(mbs, pointName, PointMXBean.class);
    Point point1 = pointproxy.identity(point);
    if (point1.getX() != point.getX() || point1.getY() != point.getY())
        throw new Exception("Point doesn't match");
    System.out.println("Point test passed");
    ObjectName evolveName = new ObjectName("a:type=Evolve");
    EvolveMXBean evolvemx = new EvolveImpl();
    mbs.registerMBean(evolvemx, evolveName);
    Evolve evolve = new Evolve(59, "tralala", Collections.singletonList("tiddly"));
    EvolveMXBean evolveProxy = JMX.newMXBeanProxy(mbs, evolveName, EvolveMXBean.class);
    Evolve evolve1 = evolveProxy.identity(evolve);
    if (evolve1.getOldInt() != evolve.getOldInt() || !evolve1.getNewString().equals(evolve.getNewString()) || !evolve1.getNewerList().equals(evolve.getNewerList()))
        throw new Exception("Evolve doesn't match");
    System.out.println("Evolve test passed");
    ObjectName evolvedName = new ObjectName("a:type=Evolved");
    EvolveMXBean evolvedmx = new EvolveImpl();
    mbs.registerMBean(evolvedmx, evolvedName);
    CompositeType evolvedType = new CompositeType("Evolved", "descr", new String[] { "oldInt" }, new String[] { "oldInt descr" }, new OpenType[] { SimpleType.INTEGER });
    CompositeData evolvedData = new CompositeDataSupport(evolvedType, new String[] { "oldInt" }, new Object[] { 5 });
    CompositeData evolved1 = (CompositeData) mbs.invoke(evolvedName, "identity", new Object[] { evolvedData }, new String[] { CompositeData.class.getName() });
    if ((Integer) evolved1.get("oldInt") != 5 || !evolved1.get("newString").equals("defaultString") || ((String[]) evolved1.get("newerList")).length != 0)
        throw new Exception("Evolved doesn't match: " + evolved1);
    System.out.println("Evolved test passed");
}
Also used : CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) CompositeType(javax.management.openmbean.CompositeType)

Example 69 with CompositeType

use of javax.management.openmbean.CompositeType in project tdi-studio-se by Talend.

the class AttributeParser method refreshCompositeData.

/**
     * Refreshes the composite data.
     * 
     * @param node
     *            The attribute node
     */
private void refreshCompositeData(AttributeNode node) {
    List<AttributeNode> children = new ArrayList<AttributeNode>();
    CompositeData compositeData = (CompositeData) node.getValue();
    CompositeType type = compositeData.getCompositeType();
    for (String key : type.keySet()) {
        AttributeNode attribute = null;
        for (AttributeNode child : node.getChildren()) {
            if (child.getName().equals(key)) {
                attribute = child;
                attribute.setValue(compositeData.get(key));
                break;
            }
        }
        if (attribute == null) {
            attribute = new AttributeNode(key, node, compositeData.get(key));
        }
        children.add(attribute);
    }
    node.removeChildren();
    for (AttributeNode child : children) {
        node.addChild(child);
        refreshAttribute(child);
    }
}
Also used : CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) CompositeType(javax.management.openmbean.CompositeType)

Example 70 with CompositeType

use of javax.management.openmbean.CompositeType in project tdi-studio-se by Talend.

the class AttributeContentProvider method addAttributeRoots.

/**
     * Adds the attribute to the given list.
     * 
     * @param mBeanAttributes The attributes
     * @param root The root attribute
     * @param value The value
     */
private void addAttributeRoots(List<AttributeNode> mBeanAttributes, AttributeNode root, Object value) {
    if (value instanceof CompositeData) {
        mBeanAttributes.add(root);
        CompositeData compositeData = (CompositeData) value;
        CompositeType type = compositeData.getCompositeType();
        for (String key : type.keySet()) {
            AttributeNode attribute = new AttributeNode(key, root);
            root.addChild(attribute);
            addAttributeItems(attribute, compositeData.get(key));
        }
    } else if (value instanceof TabularData) {
        mBeanAttributes.add(root);
        TabularData tabularData = (TabularData) value;
        for (Object keyList : tabularData.keySet()) {
            @SuppressWarnings("unchecked") Object[] keys = ((List<Object>) keyList).toArray(new Object[0]);
            AttributeNode attribute = new AttributeNode(String.valueOf(keys[0]), root);
            root.addChild(attribute);
            addAttributeItems(attribute, tabularData.get(keys));
        }
    } else if (value instanceof Long || value instanceof Integer || value instanceof Double) {
        root.setValidLeaf(true);
        root.setRgb(getRGB(root.getQualifiedName()));
        mBeanAttributes.add(root);
    }
}
Also used : CompositeData(javax.management.openmbean.CompositeData) CompositeType(javax.management.openmbean.CompositeType) TabularData(javax.management.openmbean.TabularData)

Aggregations

CompositeType (javax.management.openmbean.CompositeType)82 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)55 CompositeData (javax.management.openmbean.CompositeData)50 TabularDataSupport (javax.management.openmbean.TabularDataSupport)50 TabularData (javax.management.openmbean.TabularData)47 TabularType (javax.management.openmbean.TabularType)27 OpenType (javax.management.openmbean.OpenType)23 OpenDataException (javax.management.openmbean.OpenDataException)22 Map (java.util.Map)20 ObjectName (javax.management.ObjectName)7 EndpointUtilizationStatistics (org.apache.camel.spi.EndpointUtilizationStatistics)7 MBeanServer (javax.management.MBeanServer)6 ArrayType (javax.management.openmbean.ArrayType)6 ConcurrentMap (java.util.concurrent.ConcurrentMap)5 MBeanException (javax.management.MBeanException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 DataSourceProxyImpl (com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3