Search in sources :

Example 1 with ManagedPropertyMetaData

use of org.exoplatform.management.spi.ManagedPropertyMetaData in project kernel by exoplatform.

the class MetaDataBuilder method build.

/**
 * Build the info.
 *
 * @return returns the info
 * @throws IllegalStateException raised by any build time issue
 */
public ManagedTypeMetaData build() throws IllegalStateException {
    if (!buildable) {
        throw new IllegalStateException("Class " + clazz.getName() + " does not contain management annotation");
    }
    // 
    ManagedDescription typeDescriptionAnn = AnnotationIntrospector.resolveClassAnnotations(clazz, ManagedDescription.class);
    String typeDescription = typeDescriptionAnn != null ? typeDescriptionAnn.value() : null;
    // 
    Map<Method, Managed> managedMethods = AnnotationIntrospector.resolveMethodAnnotations(clazz, Managed.class);
    Map<Method, ManagedName> methodNames = AnnotationIntrospector.resolveMethodAnnotations(clazz, ManagedName.class);
    Map<Method, ManagedDescription> methodDescriptions = AnnotationIntrospector.resolveMethodAnnotations(clazz, ManagedDescription.class);
    // 
    Map<Method, ManagedMethodMetaData> bilto = new HashMap<Method, ManagedMethodMetaData>();
    for (Map.Entry<Method, Managed> entry : managedMethods.entrySet()) {
        Method method = entry.getKey();
        // 
        ManagedDescription methodDescriptionAnn = methodDescriptions.get(method);
        String methodDescription = methodDescriptionAnn != null ? methodDescriptionAnn.value() : null;
        // 
        Impact impactAnn = method.getAnnotation(Impact.class);
        ImpactType impactType = impactAnn != null ? impactAnn.value() : ImpactType.WRITE;
        // Build the default mbean info
        ManagedMethodMetaData managedMethod = new ManagedMethodMetaData(method, impactType);
        managedMethod.setDescription(methodDescription);
        // Overload with annotations meta data
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            ManagedMethodParameterMetaData mmpMD = new ManagedMethodParameterMetaData(i);
            for (Annotation parameterAnnotation : parameterAnnotations[i]) {
                if (parameterAnnotation instanceof ManagedName) {
                    mmpMD.setName(((ManagedName) parameterAnnotation).value());
                } else if (parameterAnnotation instanceof ManagedDescription) {
                    mmpMD.setDescription(((ManagedDescription) parameterAnnotation).value());
                }
            }
            managedMethod.addParameter(mmpMD);
        }
        // 
        bilto.put(method, managedMethod);
    }
    // 
    ManagedTypeMetaData managedType = new ManagedTypeMetaData(clazz);
    managedType.setDescription(typeDescription);
    // 
    Map<String, ManagedMethodMetaData> setters = new HashMap<String, ManagedMethodMetaData>();
    Map<String, ManagedMethodMetaData> getters = new HashMap<String, ManagedMethodMetaData>();
    for (Map.Entry<Method, ManagedMethodMetaData> entry : bilto.entrySet()) {
        Method method = entry.getKey();
        // 
        String methodName = method.getName();
        Class[] parameterTypes = method.getParameterTypes();
        // 
        OperationType type = OperationType.OP;
        Integer index = null;
        if (method.getReturnType() == void.class) {
            if (parameterTypes.length == 1 && methodName.startsWith("set") && methodName.length() > 4) {
                type = OperationType.SET;
                index = 3;
            }
        } else {
            if (parameterTypes.length == 0) {
                type = OperationType.GET;
                if (methodName.startsWith("get") && methodName.length() > 3) {
                    index = 3;
                } else if (methodName.startsWith("is") && methodName.length() > 2) {
                    index = 2;
                }
            }
        }
        // Put in the correct map if it is an attribute
        if (index != null) {
            String attributeName = methodName.substring(index);
            // 
            Map<String, ManagedMethodMetaData> map = type == OperationType.SET ? setters : getters;
            ManagedMethodMetaData previous = map.put(attributeName, entry.getValue());
            if (previous != null) {
                throw new IllegalArgumentException("Duplicate attribute " + type + " " + previous + " and " + method);
            }
        } else {
            ManagedName managedName = methodNames.get(method);
            if (managedName != null) {
                throw new IllegalArgumentException("Managed operation " + method.getName() + " cannot be annoated with @" + ManagedName.class.getName() + " with value " + managedName.value());
            }
            // 
            managedType.addMethod(entry.getValue());
        }
    }
    // Process attributes
    Set<String> attributeNames = new HashSet<String>();
    attributeNames.addAll(getters.keySet());
    attributeNames.addAll(setters.keySet());
    for (String attributeName : attributeNames) {
        ManagedMethodMetaData managedGetter = getters.get(attributeName);
        ManagedMethodMetaData managedSetter = setters.get(attributeName);
        String propertyDescription = null;
        ManagedName getterName = null;
        ManagedName setterName = null;
        String getterDescription = null;
        String setterDescription = null;
        Method getter = null;
        Method setter = null;
        ManagedParameterMetaData mpm = null;
        if (managedGetter != null) {
            getter = managedGetter.getMethod();
            getterName = methodNames.get(getter);
            getterDescription = managedGetter.getDescription();
            propertyDescription = getterDescription;
        }
        if (managedSetter != null) {
            setter = managedSetter.getMethod();
            setterName = methodNames.get(setter);
            setterDescription = managedSetter.getDescription();
            if (propertyDescription == null) {
                propertyDescription = setterDescription;
            }
            mpm = managedSetter.getParameters().iterator().next();
        }
        // Consistency check
        if (getterName != null) {
            if (setterName != null) {
                if (!getterName.value().equals(setterName.value())) {
                    throw new IllegalArgumentException("Getter name=" + getterName.value() + " does not match the setter name=" + setterName.value());
                }
            }
            attributeName = getterName.value();
        } else if (setterName != null) {
            attributeName = setterName.value();
        }
        // 
        ManagedPropertyMetaData managedProperty = new ManagedPropertyMetaData(attributeName, getter, getterDescription, setter, setterDescription, mpm);
        managedProperty.setDescription(propertyDescription);
        // 
        ManagedPropertyMetaData previousManagedProperty = managedType.getProperty(managedProperty.getName());
        if (previousManagedProperty != null) {
            throw new IllegalArgumentException("The same property was declared twice old=" + previousManagedProperty + " new=" + managedProperty);
        }
        // 
        managedType.addProperty(managedProperty);
    }
    // 
    return managedType;
}
Also used : ManagedPropertyMetaData(org.exoplatform.management.spi.ManagedPropertyMetaData) HashMap(java.util.HashMap) ImpactType(org.exoplatform.management.annotations.ImpactType) ManagedMethodParameterMetaData(org.exoplatform.management.spi.ManagedMethodParameterMetaData) ManagedName(org.exoplatform.management.annotations.ManagedName) HashSet(java.util.HashSet) ManagedParameterMetaData(org.exoplatform.management.spi.ManagedParameterMetaData) Impact(org.exoplatform.management.annotations.Impact) ManagedMethodMetaData(org.exoplatform.management.spi.ManagedMethodMetaData) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ManagedTypeMetaData(org.exoplatform.management.spi.ManagedTypeMetaData) ManagedDescription(org.exoplatform.management.annotations.ManagedDescription) HashMap(java.util.HashMap) Map(java.util.Map) Managed(org.exoplatform.management.annotations.Managed)

Example 2 with ManagedPropertyMetaData

use of org.exoplatform.management.spi.ManagedPropertyMetaData in project kernel by exoplatform.

the class ExoMBeanInfoBuilder method build.

/**
 * Build the info.
 *
 * @return returns the info
 * @throws IllegalStateException raised by any build time issue
 */
public ModelMBeanInfo build() throws IllegalStateException {
    String mbeanDescription = "Exo model mbean";
    if (typeMD.getDescription() != null) {
        mbeanDescription = typeMD.getDescription();
    }
    // 
    ArrayList<ModelMBeanOperationInfo> operations = new ArrayList<ModelMBeanOperationInfo>();
    for (ManagedMethodMetaData methodMD : typeMD.getMethods()) {
        ModelMBeanOperationInfo operationInfo = buildOperationInfo(methodMD.getMethod(), methodMD.getDescription(), Role.OP, methodMD.getParameters(), methodMD.getImpact());
        operations.add(operationInfo);
    }
    // 
    Map<String, ModelMBeanAttributeInfo> attributeInfos = new HashMap<String, ModelMBeanAttributeInfo>();
    for (ManagedPropertyMetaData propertyMD : typeMD.getProperties()) {
        Method getter = propertyMD.getGetter();
        if (getter != null) {
            Role role;
            String getterName = getter.getName();
            if (getterName.startsWith("get") && getterName.length() > 3) {
                role = Role.GET;
            } else if (getterName.startsWith("is") && getterName.length() > 2) {
                role = Role.IS;
            } else {
                throw new AssertionError();
            }
            Collection<ManagedMethodParameterMetaData> blah = Collections.emptyList();
            ModelMBeanOperationInfo operationInfo = buildOperationInfo(getter, propertyMD.getGetterDescription(), role, blah, ImpactType.READ);
            operations.add(operationInfo);
        }
        // 
        Method setter = propertyMD.getSetter();
        if (setter != null) {
            ManagedMethodParameterMetaData s = new ManagedMethodParameterMetaData(0);
            s.setDescription(propertyMD.getSetterParameter().getDescription());
            s.setName(propertyMD.getSetterParameter().getName());
            Collection<ManagedMethodParameterMetaData> blah = Collections.singletonList(s);
            ModelMBeanOperationInfo operationInfo = buildOperationInfo(setter, propertyMD.getSetterDescription(), Role.SET, blah, ImpactType.IDEMPOTENT_WRITE);
            operations.add(operationInfo);
        }
        // 
        try {
            String attributeDescription = propertyMD.getDescription() != null ? propertyMD.getDescription() : ("Managed attribute " + propertyMD.getName());
            // 
            ModelMBeanAttributeInfo attributeInfo = new ModelMBeanAttributeInfo(propertyMD.getName(), attributeDescription, getter, setter);
            // 
            Descriptor attributeDescriptor = attributeInfo.getDescriptor();
            if (getter != null) {
                attributeDescriptor.setField("getMethod", getter.getName());
            }
            if (setter != null) {
                attributeDescriptor.setField("setMethod", setter.getName());
            }
            attributeDescriptor.setField("currencyTimeLimit", "-1");
            attributeDescriptor.setField("persistPolicy", "Never");
            attributeInfo.setDescriptor(attributeDescriptor);
            // 
            ModelMBeanAttributeInfo previous = attributeInfos.put(propertyMD.getName(), attributeInfo);
            if (previous != null) {
                throw new IllegalArgumentException();
            }
        } catch (IntrospectionException e) {
            throw new AssertionError(e);
        }
    }
    // 
    return new ModelMBeanInfoSupport(typeMD.getType().getName(), mbeanDescription, attributeInfos.values().toArray(new ModelMBeanAttributeInfo[attributeInfos.size()]), new ModelMBeanConstructorInfo[0], operations.toArray(new ModelMBeanOperationInfo[operations.size()]), new ModelMBeanNotificationInfo[0]);
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) ManagedPropertyMetaData(org.exoplatform.management.spi.ManagedPropertyMetaData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IntrospectionException(javax.management.IntrospectionException) ManagedMethodMetaData(org.exoplatform.management.spi.ManagedMethodMetaData) Method(java.lang.reflect.Method) ManagedMethodParameterMetaData(org.exoplatform.management.spi.ManagedMethodParameterMetaData) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport)

Aggregations

Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 ManagedMethodMetaData (org.exoplatform.management.spi.ManagedMethodMetaData)2 ManagedMethodParameterMetaData (org.exoplatform.management.spi.ManagedMethodParameterMetaData)2 ManagedPropertyMetaData (org.exoplatform.management.spi.ManagedPropertyMetaData)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Descriptor (javax.management.Descriptor)1 IntrospectionException (javax.management.IntrospectionException)1 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)1 ModelMBeanInfoSupport (javax.management.modelmbean.ModelMBeanInfoSupport)1 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)1 Impact (org.exoplatform.management.annotations.Impact)1 ImpactType (org.exoplatform.management.annotations.ImpactType)1 Managed (org.exoplatform.management.annotations.Managed)1 ManagedDescription (org.exoplatform.management.annotations.ManagedDescription)1 ManagedName (org.exoplatform.management.annotations.ManagedName)1 ManagedParameterMetaData (org.exoplatform.management.spi.ManagedParameterMetaData)1