use of javax.management.modelmbean.ModelMBeanOperationInfo in project camel by apache.
the class MBeanInfoAssembler method getMBeanInfo.
/**
* Gets the {@link ModelMBeanInfo} for the given managed bean
*
* @param defaultManagedBean the default managed bean
* @param customManagedBean an optional custom managed bean
* @param objectName the object name
* @return the model info, or <tt>null</tt> if not possible to create, for example due the managed bean is a proxy class
* @throws JMException is thrown if error creating the model info
*/
public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object customManagedBean, String objectName) throws JMException {
// skip proxy classes
if (defaultManagedBean != null && Proxy.isProxyClass(defaultManagedBean.getClass())) {
LOG.trace("Skip creating ModelMBeanInfo due proxy class {}", defaultManagedBean.getClass());
return null;
}
// maps and lists to contain information about attributes and operations
Map<String, ManagedAttributeInfo> attributes = new LinkedHashMap<String, ManagedAttributeInfo>();
Set<ManagedOperationInfo> operations = new LinkedHashSet<ManagedOperationInfo>();
Set<ModelMBeanAttributeInfo> mBeanAttributes = new LinkedHashSet<ModelMBeanAttributeInfo>();
Set<ModelMBeanOperationInfo> mBeanOperations = new LinkedHashSet<ModelMBeanOperationInfo>();
Set<ModelMBeanNotificationInfo> mBeanNotifications = new LinkedHashSet<ModelMBeanNotificationInfo>();
// extract details from default managed bean
if (defaultManagedBean != null) {
extractAttributesAndOperations(defaultManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(defaultManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(defaultManagedBean, operations, mBeanOperations);
extractMbeanNotifications(defaultManagedBean, mBeanNotifications);
}
// extract details from custom managed bean
if (customManagedBean != null) {
extractAttributesAndOperations(customManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(customManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(customManagedBean, operations, mBeanOperations);
extractMbeanNotifications(customManagedBean, mBeanNotifications);
}
// create the ModelMBeanInfo
String name = getName(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
String description = getDescription(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
ModelMBeanAttributeInfo[] arrayAttributes = mBeanAttributes.toArray(new ModelMBeanAttributeInfo[mBeanAttributes.size()]);
ModelMBeanOperationInfo[] arrayOperations = mBeanOperations.toArray(new ModelMBeanOperationInfo[mBeanOperations.size()]);
ModelMBeanNotificationInfo[] arrayNotifications = mBeanNotifications.toArray(new ModelMBeanNotificationInfo[mBeanNotifications.size()]);
ModelMBeanInfo info = new ModelMBeanInfoSupport(name, description, arrayAttributes, null, arrayOperations, arrayNotifications);
LOG.trace("Created ModelMBeanInfo {}", info);
return info;
}
use of javax.management.modelmbean.ModelMBeanOperationInfo 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.modelmbean.ModelMBeanOperationInfo in project spring-framework by spring-projects.
the class AnnotationMetadataAssemblerTests method testRegistrationOnInterface.
@Test
public void testRegistrationOnInterface() throws Exception {
Object bean = getContext().getBean("testInterfaceBean");
ModelMBeanInfo inf = getAssembler().getMBeanInfo(bean, "bean:name=interfaceTestBean");
assertNotNull(inf);
assertEquals("My Managed Bean", inf.getDescription());
ModelMBeanOperationInfo op = inf.getOperation("foo");
assertNotNull("foo operation not exposed", op);
assertEquals("invoke foo", op.getDescription());
assertNull("doNotExpose operation should not be exposed", inf.getOperation("doNotExpose"));
ModelMBeanAttributeInfo attr = inf.getAttribute("Bar");
assertNotNull("bar attribute not exposed", attr);
assertEquals("Bar description", attr.getDescription());
ModelMBeanAttributeInfo attr2 = inf.getAttribute("CacheEntries");
assertNotNull("cacheEntries attribute not exposed", attr2);
assertEquals("Metric Type should be COUNTER", "COUNTER", attr2.getDescriptor().getFieldValue("metricType"));
}
use of javax.management.modelmbean.ModelMBeanOperationInfo in project spring-framework by spring-projects.
the class AnnotationMetadataAssemblerTests method testOperationFromInterface.
@Test
public void testOperationFromInterface() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo op = inf.getOperation("fromInterface");
assertNotNull(op);
}
use of javax.management.modelmbean.ModelMBeanOperationInfo in project jdk8u_jdk by JetBrains.
the class UnserializableTargetObjectTest method main.
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "operation", resource1 });
Descriptor getCountDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "getCount", resource2 });
Descriptor setCountDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "setCount", resource2 });
Descriptor countDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "getMethod", "setMethod" }, new Object[] { "attribute", "Count", "getCount", "setCount" });
ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo("operation description", operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo = new ModelMBeanOperationInfo("getCount description", getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo = new ModelMBeanOperationInfo("setCount description", setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo = new ModelMBeanAttributeInfo("Count", "Count description", getCountMethod, setCountMethod, countDescriptor);
ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(Resource.class.getName(), "ModelMBean to test targetObject", new ModelMBeanAttributeInfo[] { countInfo }, // no constructors
null, new ModelMBeanOperationInfo[] { operationInfo, getCountInfo, setCountInfo }, // no notifications
null);
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
Aggregations