Search in sources :

Example 96 with MBeanAttributeInfo

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

the class MXBeanTest method testMXBean.

private static void testMXBean(MBeanServer mbs, ObjectName on) throws Exception {
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    MBeanAttributeInfo[] attrs = mbi.getAttributes();
    int nattrs = attrs.length;
    if (mbi.getAttributes().length != 1)
        failure("wrong number of attributes: " + attrs);
    else {
        MBeanAttributeInfo mbai = attrs[0];
        if (mbai.getName().equals("Ints") && mbai.isReadable() && !mbai.isWritable() && mbai.getDescriptor().getFieldValue("openType").equals(new ArrayType<int[]>(SimpleType.INTEGER, true)) && attrs[0].getType().equals("[I"))
            success("MBeanAttributeInfo");
        else
            failure("MBeanAttributeInfo: " + mbai);
    }
    int[] ints = (int[]) mbs.getAttribute(on, "Ints");
    if (equal(ints, new int[] { 1, 2, 3 }, null))
        success("getAttribute");
    else
        failure("getAttribute: " + Arrays.toString(ints));
    ExplicitMXBean proxy = JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
    int[] pints = proxy.getInts();
    if (equal(pints, new int[] { 1, 2, 3 }, null))
        success("getAttribute through proxy");
    else
        failure("getAttribute through proxy: " + Arrays.toString(pints));
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 97 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo 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 98 with MBeanAttributeInfo

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

the class AttributeContentProvider method refresh.

/**
     * Refreshes the content provider.
     * 
     * @param jvm The active JVM
     */
public void refresh(IActiveJvm jvm) {
    domains = new HashMap<String, MBeanDomain>();
    // add or update elements
    for (ObjectName objectName : getObjectNames(jvm)) {
        MBeanInfo mBeanInfo = getMBeanInfo(jvm, objectName);
        if (mBeanInfo == null) {
            continue;
        }
        List<AttributeNode> mBeanAttributes = new ArrayList<AttributeNode>();
        for (MBeanAttributeInfo attributeInfo : mBeanInfo.getAttributes()) {
            String attributeName = attributeInfo.getName();
            Object value = getContents(jvm, objectName, attributeName);
            addAttributeRoots(mBeanAttributes, new AttributeNode(attributeName, null, objectName), value);
        }
        for (AttributeNode node : mBeanAttributes.toArray(new AttributeNode[0])) {
            validateAttributes(node);
            if (node.getChildren().size() == 0 && !node.isValidLeaf()) {
                mBeanAttributes.remove(node);
            }
        }
        if (mBeanAttributes.size() == 0) {
            continue;
        }
        attributes.put(objectName, mBeanAttributes);
        String domainName = objectName.getDomain();
        MBeanDomain domain;
        if (domains.containsKey(domainName)) {
            domain = domains.get(domainName);
        } else {
            domain = new MBeanDomain(domainName);
        }
        domain.refresh(objectName, jvm);
        domains.put(domainName, domain);
    }
}
Also used : MBeanDomain(org.talend.designer.runtime.visualization.internal.ui.properties.MBean.MBeanDomain) MBeanInfo(javax.management.MBeanInfo) ArrayList(java.util.ArrayList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName)

Example 99 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project geode by apache.

the class MX4JModelMBean method addAttributeChangeNotificationListener.

public void addAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException {
    if (listener == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_LISTENER_CANNOT_BE_NULL.toLocalizedString()));
    AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
    if (attributeName != null) {
        filter.enableAttribute(attributeName);
    } else {
        MBeanAttributeInfo[] ai = m_modelMBeanInfo.getAttributes();
        for (int i = 0; i < ai.length; i++) {
            Descriptor d = ((ModelMBeanAttributeInfo) ai[i]).getDescriptor();
            filter.enableAttribute((String) d.getFieldValue("name"));
        }
    }
    getAttributeChangeBroadcaster().addNotificationListener(listener, filter, handback);
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Listener " + listener + " for attribute " + attributeName + " added successfully, handback is " + handback);
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) AttributeChangeNotificationFilter(javax.management.AttributeChangeNotificationFilter) Descriptor(javax.management.Descriptor) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 100 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project lucene-solr by apache.

the class TestJmxIntegration method testJmxRegistration.

@Test
public void testJmxRegistration() throws Exception {
    assertTrue("No MBeans found in server", mbeanServer.getMBeanCount() > 0);
    Set<ObjectInstance> objects = mbeanServer.queryMBeans(null, null);
    assertFalse("No objects found in mbean server", objects.isEmpty());
    int numDynamicMbeans = 0;
    for (ObjectInstance o : objects) {
        ObjectName name = o.getObjectName();
        assertNotNull("Null name on: " + o.toString(), name);
        MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(name);
        if (name.getDomain().equals("solr")) {
            numDynamicMbeans++;
            MBeanAttributeInfo[] attrs = mbeanInfo.getAttributes();
            if (name.getKeyProperty("name").equals("fetcher")) {
                // no attributes without active replication
                continue;
            }
            assertTrue("No Attributes found for mbean: " + o.getObjectName() + ", " + mbeanInfo, 0 < attrs.length);
            for (MBeanAttributeInfo attr : attrs) {
                // ensure every advertised attribute is gettable
                try {
                    Object trash = mbeanServer.getAttribute(o.getObjectName(), attr.getName());
                } catch (javax.management.AttributeNotFoundException e) {
                    throw new RuntimeException("Unable to featch attribute for " + o.getObjectName() + ": " + attr.getName(), e);
                }
            }
        }
    }
    assertTrue("No MBeans found", 0 < numDynamicMbeans);
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) ObjectInstance(javax.management.ObjectInstance) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

MBeanAttributeInfo (javax.management.MBeanAttributeInfo)106 MBeanInfo (javax.management.MBeanInfo)75 ObjectName (javax.management.ObjectName)45 MBeanOperationInfo (javax.management.MBeanOperationInfo)24 Test (org.junit.Test)21 MBeanServer (javax.management.MBeanServer)15 ArrayList (java.util.ArrayList)13 AttributeNotFoundException (javax.management.AttributeNotFoundException)12 ReflectionException (javax.management.ReflectionException)12 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)11 IOException (java.io.IOException)10 AttributeList (javax.management.AttributeList)10 Attribute (javax.management.Attribute)9 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 IntrospectionException (javax.management.IntrospectionException)9 MBeanParameterInfo (javax.management.MBeanParameterInfo)9 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)9 HashMap (java.util.HashMap)8 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)7 MBeanException (javax.management.MBeanException)7