Search in sources :

Example 1 with ModelMBeanOperationInfo

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) ModelMBeanNotificationInfo(javax.management.modelmbean.ModelMBeanNotificationInfo) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) LinkedHashMap(java.util.LinkedHashMap) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport)

Example 2 with ModelMBeanOperationInfo

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 &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 3 with ModelMBeanOperationInfo

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"));
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) Test(org.junit.Test)

Example 4 with ModelMBeanOperationInfo

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);
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) Test(org.junit.Test)

Example 5 with ModelMBeanOperationInfo

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");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) Attribute(javax.management.Attribute) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) Method(java.lang.reflect.Method) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ObjectName(javax.management.ObjectName) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) JMXConnector(javax.management.remote.JMXConnector) Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Aggregations

ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)18 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)11 Descriptor (javax.management.Descriptor)9 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)9 Test (org.junit.Test)6 ModelMBeanInfoSupport (javax.management.modelmbean.ModelMBeanInfoSupport)5 Method (java.lang.reflect.Method)4 DescriptorSupport (javax.management.modelmbean.DescriptorSupport)3 ModelMBeanNotificationInfo (javax.management.modelmbean.ModelMBeanNotificationInfo)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 MBeanParameterInfo (javax.management.MBeanParameterInfo)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 ModelMBean (javax.management.modelmbean.ModelMBean)2 RequiredModelMBean (javax.management.modelmbean.RequiredModelMBean)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Hashtable (java.util.Hashtable)1