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();
}
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;
}
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;
}
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 "role" 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()]);
}
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"));
}
Aggregations