Search in sources :

Example 1 with ManagedAttribute

use of org.apache.camel.api.management.ManagedAttribute in project camel by apache.

the class MBeanInfoAssembler method doDoExtractAttributesAndOperations.

private void doDoExtractAttributesAndOperations(Class<?> managedClass, Map<String, ManagedAttributeInfo> attributes, Set<ManagedOperationInfo> operations) {
    LOG.trace("Extracting attributes and operations from class: {}", managedClass);
    // introspect the class, and leverage the cache to have better performance
    IntrospectionSupport.ClassInfo cache = IntrospectionSupport.cacheClass(managedClass);
    for (IntrospectionSupport.MethodInfo cacheInfo : cache.methods) {
        // must be from declaring class
        if (cacheInfo.method.getDeclaringClass() != managedClass) {
            continue;
        }
        LOG.trace("Extracting attributes and operations from method: {}", cacheInfo.method);
        ManagedAttribute ma = cacheInfo.method.getAnnotation(ManagedAttribute.class);
        if (ma != null) {
            String key;
            String desc = ma.description();
            Method getter = null;
            Method setter = null;
            boolean mask = ma.mask();
            if (cacheInfo.isGetter) {
                key = cacheInfo.getterOrSetterShorthandName;
                getter = cacheInfo.method;
            } else if (cacheInfo.isSetter) {
                key = cacheInfo.getterOrSetterShorthandName;
                setter = cacheInfo.method;
            } else {
                throw new IllegalArgumentException("@ManagedAttribute can only be used on Java bean methods, was: " + cacheInfo.method + " on bean: " + managedClass);
            }
            // they key must be capitalized
            key = ObjectHelper.capitalize(key);
            // lookup first
            ManagedAttributeInfo info = attributes.get(key);
            if (info == null) {
                info = new ManagedAttributeInfo(key, desc);
            }
            if (getter != null) {
                info.setGetter(getter);
            }
            if (setter != null) {
                info.setSetter(setter);
            }
            info.setMask(mask);
            attributes.put(key, info);
        }
        // operations
        ManagedOperation mo = cacheInfo.method.getAnnotation(ManagedOperation.class);
        if (mo != null) {
            String desc = mo.description();
            Method operation = cacheInfo.method;
            boolean mask = mo.mask();
            operations.add(new ManagedOperationInfo(desc, operation, mask));
        }
    }
}
Also used : IntrospectionSupport(org.apache.camel.util.IntrospectionSupport) Method(java.lang.reflect.Method) ManagedAttribute(org.apache.camel.api.management.ManagedAttribute) ManagedOperation(org.apache.camel.api.management.ManagedOperation)

Aggregations

Method (java.lang.reflect.Method)1 ManagedAttribute (org.apache.camel.api.management.ManagedAttribute)1 ManagedOperation (org.apache.camel.api.management.ManagedOperation)1 IntrospectionSupport (org.apache.camel.util.IntrospectionSupport)1