Search in sources :

Example 86 with MBeanInfo

use of javax.management.MBeanInfo in project jmxtrans by jmxtrans.

the class Query method fetchResults.

public Iterable<Result> fetchResults(MBeanServerConnection mbeanServer, ObjectName queryName) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    MBeanInfo info = mbeanServer.getMBeanInfo(queryName);
    ObjectInstance oi = mbeanServer.getObjectInstance(queryName);
    List<String> attributes;
    if (attr.isEmpty()) {
        attributes = new ArrayList<>();
        for (MBeanAttributeInfo attrInfo : info.getAttributes()) {
            attributes.add(attrInfo.getName());
        }
    } else {
        attributes = attr;
    }
    try {
        if (!attributes.isEmpty()) {
            logger.debug("Executing queryName [{}] from query [{}]", queryName.getCanonicalName(), this);
            AttributeList al = mbeanServer.getAttributes(queryName, attributes.toArray(new String[attributes.size()]));
            return new JmxResultProcessor(this, oi, al.asList(), info.getClassName(), queryName.getDomain()).getResults();
        }
    } catch (UnmarshalException ue) {
        if ((ue.getCause() != null) && (ue.getCause() instanceof ClassNotFoundException)) {
            logger.debug("Bad unmarshall, continuing. This is probably ok and due to something like this: " + "http://ehcache.org/xref/net/sf/ehcache/distribution/RMICacheManagerPeerListener.html#52", ue.getMessage());
        } else {
            throw ue;
        }
    }
    return ImmutableList.of();
}
Also used : MBeanInfo(javax.management.MBeanInfo) AttributeList(javax.management.AttributeList) UnmarshalException(java.rmi.UnmarshalException) ObjectInstance(javax.management.ObjectInstance) ToString(lombok.ToString) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 87 with MBeanInfo

use of javax.management.MBeanInfo in project jvm-tools by aragozin.

the class MBeanHelper method get.

public String get(ObjectName bean, String attr) throws Exception {
    MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
    MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
    if (ai == null) {
        throw new IllegalArgumentException("No such attribute '" + attr + "'");
    }
    if (!ai.isReadable()) {
        throw new IllegalArgumentException("Attribute '" + attr + "' is write-only");
    }
    Object v = mserver.getAttribute(bean, attr);
    String type = ai.getType();
    String text = format(v, type);
    return text;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 88 with MBeanInfo

use of javax.management.MBeanInfo in project jvm-tools by aragozin.

the class MBeanHelper method set.

public void set(ObjectName bean, String attr, String value) throws Exception {
    MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
    MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
    if (ai == null) {
        throw new IllegalArgumentException("No such attribute '" + attr + "'");
    }
    if (!ai.isWritable()) {
        throw new IllegalArgumentException("Attribute '" + attr + "' is not writeable");
    }
    String type = ai.getType();
    Object ov = convert(value, type);
    mserver.setAttribute(bean, new Attribute(attr, ov));
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 89 with MBeanInfo

use of javax.management.MBeanInfo in project jetty.project by eclipse.

the class ObjectMBean method getMBeanInfo.

public MBeanInfo getMBeanInfo() {
    try {
        if (_info == null) {
            // Start with blank lazy lists attributes etc.
            String desc = null;
            List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
            List<MBeanConstructorInfo> constructors = new ArrayList<MBeanConstructorInfo>();
            List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>();
            List<MBeanNotificationInfo> notifications = new ArrayList<MBeanNotificationInfo>();
            // Find list of classes that can influence the mbean
            Class<?> o_class = _managed.getClass();
            List<Class<?>> influences = new ArrayList<Class<?>>();
            // always add MBean itself
            influences.add(this.getClass());
            influences = findInfluences(influences, _managed.getClass());
            if (LOG.isDebugEnabled())
                LOG.debug("Influence Count: {}", influences.size());
            // Process Type Annotations
            ManagedObject primary = o_class.getAnnotation(ManagedObject.class);
            if (primary != null) {
                desc = primary.value();
            } else {
                if (LOG.isDebugEnabled())
                    LOG.debug("No @ManagedObject declared on {}", _managed.getClass());
            }
            // For each influence
            for (int i = 0; i < influences.size(); i++) {
                Class<?> oClass = influences.get(i);
                ManagedObject typeAnnotation = oClass.getAnnotation(ManagedObject.class);
                if (LOG.isDebugEnabled())
                    LOG.debug("Influenced by: " + oClass.getCanonicalName());
                if (typeAnnotation == null) {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Annotations not found for: {}", oClass.getCanonicalName());
                    continue;
                }
                for (Method method : oClass.getDeclaredMethods()) {
                    ManagedAttribute methodAttributeAnnotation = method.getAnnotation(ManagedAttribute.class);
                    if (methodAttributeAnnotation != null) {
                        // TODO sort out how a proper name could get here, its a method name as an attribute at this point.
                        if (LOG.isDebugEnabled())
                            LOG.debug("Attribute Annotation found for: {}", method.getName());
                        MBeanAttributeInfo mai = defineAttribute(method, methodAttributeAnnotation);
                        if (mai != null) {
                            attributes.add(mai);
                        }
                    }
                    ManagedOperation methodOperationAnnotation = method.getAnnotation(ManagedOperation.class);
                    if (methodOperationAnnotation != null) {
                        if (LOG.isDebugEnabled())
                            LOG.debug("Method Annotation found for: {}", method.getName());
                        MBeanOperationInfo oi = defineOperation(method, methodOperationAnnotation);
                        if (oi != null) {
                            operations.add(oi);
                        }
                    }
                }
            }
            _info = new MBeanInfo(o_class.getName(), desc, (MBeanAttributeInfo[]) attributes.toArray(new MBeanAttributeInfo[attributes.size()]), (MBeanConstructorInfo[]) constructors.toArray(new MBeanConstructorInfo[constructors.size()]), (MBeanOperationInfo[]) operations.toArray(new MBeanOperationInfo[operations.size()]), (MBeanNotificationInfo[]) notifications.toArray(new MBeanNotificationInfo[notifications.size()]));
        }
    } catch (RuntimeException e) {
        LOG.warn(e);
        throw e;
    }
    return _info;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute) ManagedOperation(org.eclipse.jetty.util.annotation.ManagedOperation) MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject)

Example 90 with MBeanInfo

use of javax.management.MBeanInfo in project jetty.project by eclipse.

the class ObjectMBeanTest method testDerivedOperations.

@Test
public void testDerivedOperations() throws Exception {
    Derived derived = new Derived();
    ObjectMBean mbean = (ObjectMBean) ObjectMBean.mbeanFor(derived);
    mbean.setMBeanContainer(container);
    container.beanAdded(null, derived);
    MBeanInfo info = mbean.getMBeanInfo();
    Assert.assertEquals("operation count does not match", 5, info.getOperations().length);
    MBeanOperationInfo[] opinfos = info.getOperations();
    boolean publish = false;
    boolean doodle = false;
    boolean good = false;
    for (int i = 0; i < opinfos.length; ++i) {
        MBeanOperationInfo opinfo = opinfos[i];
        if ("publish".equals(opinfo.getName())) {
            publish = true;
            Assert.assertEquals("description doesn't match", "publish something", opinfo.getDescription());
        }
        if ("doodle".equals(opinfo.getName())) {
            doodle = true;
            Assert.assertEquals("description doesn't match", "Doodle something", opinfo.getDescription());
            MBeanParameterInfo[] pinfos = opinfo.getSignature();
            Assert.assertEquals("parameter description doesn't match", "A description of the argument", pinfos[0].getDescription());
            Assert.assertEquals("parameter name doesn't match", "doodle", pinfos[0].getName());
        }
        // This is a proxied operation on the JMX wrapper
        if ("good".equals(opinfo.getName())) {
            good = true;
            Assert.assertEquals("description does not match", "test of proxy operations", opinfo.getDescription());
            Assert.assertEquals("execution contexts wrong", "not bad", mbean.invoke("good", new Object[] {}, new String[] {}));
        }
    }
    Assert.assertTrue("publish operation was not not found", publish);
    Assert.assertTrue("doodle operation was not not found", doodle);
    Assert.assertTrue("good operation was not not found", good);
}
Also used : Derived(com.acme.Derived) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanParameterInfo(javax.management.MBeanParameterInfo) Test(org.junit.Test)

Aggregations

MBeanInfo (javax.management.MBeanInfo)154 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)87 ObjectName (javax.management.ObjectName)79 MBeanOperationInfo (javax.management.MBeanOperationInfo)38 MBeanServer (javax.management.MBeanServer)27 Test (org.junit.Test)27 Attribute (javax.management.Attribute)19 ArrayList (java.util.ArrayList)17 IntrospectionException (javax.management.IntrospectionException)16 ReflectionException (javax.management.ReflectionException)16 HashMap (java.util.HashMap)15 InstanceNotFoundException (javax.management.InstanceNotFoundException)15 IOException (java.io.IOException)12 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)12 MBeanParameterInfo (javax.management.MBeanParameterInfo)12 MBeanServerConnection (javax.management.MBeanServerConnection)10 MalformedObjectNameException (javax.management.MalformedObjectNameException)10 AttributeList (javax.management.AttributeList)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)9 Descriptor (javax.management.Descriptor)8