Search in sources :

Example 1 with Descriptor

use of javax.management.Descriptor in project jcollectd by collectd.

the class MBeanCollector method collect.

public void collect(MBeanQuery query, ObjectName name) throws Exception {
    MBeanServerConnection conn = _sender.getMBeanServerConnection();
    String plugin = query.getPlugin();
    if (plugin == null) {
        plugin = name.getDomain();
    }
    Map<String, MBeanAttributeInfo> attrInfo = null;
    if (_useDescriptors) {
        MBeanInfo info = conn.getMBeanInfo(name);
        attrInfo = new HashMap<String, MBeanAttributeInfo>();
        for (MBeanAttributeInfo ainfo : info.getAttributes()) {
            attrInfo.put(ainfo.getName(), ainfo);
        }
    }
    for (MBeanAttribute attr : query.getAttributes()) {
        String attrName = attr.getAttributeName();
        Object obj;
        try {
            obj = conn.getAttribute(name, attrName);
        } catch (Exception e) {
            //XXX remove attr for future collection e.g. UnsupportedOperation
            continue;
        }
        if (_useDescriptors) {
            //e.g. spring @ManagedMetric(metricType = MetricType.COUNTER)
            try {
                Descriptor descriptor = (Descriptor) _getDescriptor.invoke(attrInfo.get(attrName), (Object[]) null);
                Object type = descriptor.getFieldValue(_metricTypeField);
                if (TypesDB.NAME_COUNTER.equals(type)) {
                    if (attr.getTypeName().equals(TypesDB.NAME_GAUGE)) {
                        attr.setTypeName(TypesDB.NAME_COUNTER);
                    }
                    attr.setDataType(Network.DS_TYPE_COUNTER);
                }
            } catch (Exception e) {
            }
        }
        if (obj instanceof CompositeData) {
            CompositeData data = (CompositeData) obj;
            String key = attr.getCompositeKey();
            if (key == null) {
                //no key specified; collect all
                Set<String> keys = data.getCompositeType().keySet();
                for (String ckey : keys) {
                    obj = data.get(ckey);
                    if (!isNumber(obj)) {
                        continue;
                    }
                    dispatch(query, plugin, attrName + "." + ckey, name, attr, (Number) obj);
                }
                continue;
            } else {
                obj = data.get(key);
            }
        }
        if (!isNumber(obj)) {
            continue;
        }
        dispatch(query, plugin, attr.getName(), name, attr, (Number) obj);
    }
    _sender.flush();
}
Also used : MBeanInfo(javax.management.MBeanInfo) CompositeData(javax.management.openmbean.CompositeData) Descriptor(javax.management.Descriptor) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 2 with Descriptor

use of javax.management.Descriptor in project qi4j-sdk by Qi4j.

the class ModelMBeanBuilder method attribute.

public ModelMBeanBuilder attribute(String name, String displayName, String type, String description, String getMethod, String setMethod) {
    Descriptor stateDesc = new DescriptorSupport();
    stateDesc.setField("name", name);
    stateDesc.setField("descriptorType", "attribute");
    stateDesc.setField("displayName", displayName);
    if (getMethod != null) {
        stateDesc.setField("getMethod", getMethod);
        operation(getMethod, description, type, ModelMBeanOperationInfo.INFO);
    }
    if (setMethod != null) {
        stateDesc.setField("setMethod", setMethod);
        operation(setMethod, description, type, ModelMBeanOperationInfo.INFO, new MBeanParameterInfo("Value", type, description));
    }
    ModelMBeanAttributeInfo attributeInfo = new ModelMBeanAttributeInfo(name, type, description, getMethod != null, setMethod != null, getMethod != null && getMethod.startsWith("is"), stateDesc);
    attributes.add(attributeInfo);
    return this;
}
Also used : Descriptor(javax.management.Descriptor) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Example 3 with Descriptor

use of javax.management.Descriptor in project spring-framework by spring-projects.

the class AbstractMBeanInfoAssembler method getMBeanInfo.

/**
	 * Create an instance of the {@code ModelMBeanInfoSupport} class supplied with all
	 * JMX implementations and populates the metadata through calls to the subclass.
	 * @param managedBean the bean that will be exposed (might be an AOP proxy)
	 * @param beanKey the key associated with the managed bean
	 * @return the populated ModelMBeanInfo instance
	 * @throws JMException in case of errors
	 * @see #getDescription(Object, String)
	 * @see #getAttributeInfo(Object, String)
	 * @see #getConstructorInfo(Object, String)
	 * @see #getOperationInfo(Object, String)
	 * @see #getNotificationInfo(Object, String)
	 * @see #populateMBeanDescriptor(javax.management.Descriptor, Object, String)
	 */
@Override
public ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws JMException {
    checkManagedBean(managedBean);
    ModelMBeanInfo info = new ModelMBeanInfoSupport(getClassName(managedBean, beanKey), getDescription(managedBean, beanKey), getAttributeInfo(managedBean, beanKey), getConstructorInfo(managedBean, beanKey), getOperationInfo(managedBean, beanKey), getNotificationInfo(managedBean, beanKey));
    Descriptor desc = info.getMBeanDescriptor();
    populateMBeanDescriptor(desc, managedBean, beanKey);
    info.setMBeanDescriptor(desc);
    return info;
}
Also used : Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo)

Example 4 with Descriptor

use of javax.management.Descriptor in project spring-framework by spring-projects.

the class AbstractReflectiveMBeanInfoAssembler method getOperationInfo.

/**
	 * Iterate through all methods on the MBean class and gives subclasses the chance
	 * to vote on their inclusion. If a particular method corresponds to the accessor
	 * or mutator of an attribute that is inclued in the managment interface, then
	 * the corresponding operation is exposed with the &quot;role&quot; descriptor
	 * field set to the appropriate value.
	 * @param managedBean the bean instance (might be an AOP proxy)
	 * @param beanKey the key associated with the MBean in the beans map
	 * of the {@code MBeanExporter}
	 * @return the operation metadata
	 * @see #populateOperationDescriptor
	 */
@Override
protected ModelMBeanOperationInfo[] getOperationInfo(Object managedBean, String beanKey) {
    Method[] methods = getClassToExpose(managedBean).getMethods();
    List<ModelMBeanOperationInfo> infos = new ArrayList<>();
    for (Method method : methods) {
        if (method.isSynthetic()) {
            continue;
        }
        if (Object.class == method.getDeclaringClass()) {
            continue;
        }
        ModelMBeanOperationInfo info = null;
        PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
        if (pd != null) {
            if ((method.equals(pd.getReadMethod()) && includeReadAttribute(method, beanKey)) || (method.equals(pd.getWriteMethod()) && includeWriteAttribute(method, beanKey))) {
                // Attributes need to have their methods exposed as
                // operations to the JMX server as well.
                info = createModelMBeanOperationInfo(method, pd.getName(), beanKey);
                Descriptor desc = info.getDescriptor();
                if (method.equals(pd.getReadMethod())) {
                    desc.setField(FIELD_ROLE, ROLE_GETTER);
                } else {
                    desc.setField(FIELD_ROLE, ROLE_SETTER);
                }
                desc.setField(FIELD_VISIBILITY, ATTRIBUTE_OPERATION_VISIBILITY);
                if (isExposeClassDescriptor()) {
                    desc.setField(FIELD_CLASS, getClassForDescriptor(managedBean).getName());
                }
                info.setDescriptor(desc);
            }
        }
        // allow getters and setters to be marked as operations directly
        if (info == null && includeOperation(method, beanKey)) {
            info = createModelMBeanOperationInfo(method, method.getName(), beanKey);
            Descriptor desc = info.getDescriptor();
            desc.setField(FIELD_ROLE, ROLE_OPERATION);
            if (isExposeClassDescriptor()) {
                desc.setField(FIELD_CLASS, getClassForDescriptor(managedBean).getName());
            }
            populateOperationDescriptor(desc, method, beanKey);
            info.setDescriptor(desc);
        }
        if (info != null) {
            infos.add(info);
        }
    }
    return infos.toArray(new ModelMBeanOperationInfo[infos.size()]);
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) Descriptor(javax.management.Descriptor) PropertyDescriptor(java.beans.PropertyDescriptor) Method(java.lang.reflect.Method)

Example 5 with Descriptor

use of javax.management.Descriptor in project spring-framework by spring-projects.

the class AbstractJmxAssemblerTests method testAttributeInfoHasDescriptors.

@Test
public void testAttributeInfoHasDescriptors() throws Exception {
    ModelMBeanInfo info = getMBeanInfoFromAssembler();
    ModelMBeanAttributeInfo attr = info.getAttribute(NAME_ATTRIBUTE);
    Descriptor desc = attr.getDescriptor();
    assertNotNull("getMethod field should not be null", desc.getFieldValue("getMethod"));
    assertNotNull("setMethod field should not be null", desc.getFieldValue("setMethod"));
    assertEquals("getMethod field has incorrect value", "getName", desc.getFieldValue("getMethod"));
    assertEquals("setMethod field has incorrect value", "setName", desc.getFieldValue("setMethod"));
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) Test(org.junit.Test)

Aggregations

Descriptor (javax.management.Descriptor)60 RuntimeOperationsException (javax.management.RuntimeOperationsException)18 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)16 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)15 ImmutableDescriptor (javax.management.ImmutableDescriptor)9 MBeanException (javax.management.MBeanException)9 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)8 MBeanServer (javax.management.MBeanServer)8 ObjectName (javax.management.ObjectName)8 ServiceNotFoundException (javax.management.ServiceNotFoundException)8 Method (java.lang.reflect.Method)7 InstanceNotFoundException (javax.management.InstanceNotFoundException)7 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)7 FileLogger (mx4j.log.FileLogger)7 Logger (mx4j.log.Logger)7 MBeanLogger (mx4j.log.MBeanLogger)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 Date (java.util.Date)6 ListenerNotFoundException (javax.management.ListenerNotFoundException)6