Search in sources :

Example 1 with ManagedTypeMetaData

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

the class ManagementContextImpl method register.

public void register(Object o) {
    Object resource = null;
    // Apply managed by annotation
    ManagedBy managedBy = o.getClass().getAnnotation(ManagedBy.class);
    if (managedBy != null) {
        try {
            Class<?> managedByClass = managedBy.value();
            Constructor<?> blah = managedByClass.getConstructor(o.getClass());
            resource = blah.newInstance(o);
        } catch (NoSuchMethodException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (InstantiationException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (IllegalAccessException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (InvocationTargetException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    } else {
        resource = o;
    }
    // 
    if (resource != null) {
        MetaDataBuilder builder = new MetaDataBuilder(resource.getClass());
        if (builder.isBuildable()) {
            ManagedTypeMetaData metaData = builder.build();
            // 
            ManagementContextImpl managementContext;
            if (resource instanceof Container) {
                managementContext = (ManagementContextImpl) ((Container) resource).getManagementContext();
            } else {
                managementContext = new ManagementContextImpl(this, resource, metaData);
            }
            // 
            if (registrations.containsKey(resource)) {
                LOG.debug("The component " + resource + " has already been registered");
                return;
            } else {
                registrations.put(resource, managementContext);
            }
            // 
            ManageableContainer container = findContainer();
            // Install for all the providers related
            for (ManagementProvider provider : container.getProviders()) {
                Object name = provider.manage(managementContext);
                if (name != null) {
                    managementContext.managedSet.put(provider, name);
                }
            }
            // Allow for more resource management
            if (resource instanceof ManagementAware) {
                ((ManagementAware) resource).setContext(managementContext);
            }
        }
    }
}
Also used : ManagedBy(org.exoplatform.management.annotations.ManagedBy) InvocationTargetException(java.lang.reflect.InvocationTargetException) RootContainer(org.exoplatform.container.RootContainer) ExoContainer(org.exoplatform.container.ExoContainer) Container(org.exoplatform.container.spi.Container) ManagedTypeMetaData(org.exoplatform.management.spi.ManagedTypeMetaData) ManagementAware(org.exoplatform.management.ManagementAware) ManagementProvider(org.exoplatform.management.spi.ManagementProvider)

Example 2 with ManagedTypeMetaData

use of org.exoplatform.management.spi.ManagedTypeMetaData 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 3 with ManagedTypeMetaData

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

the class TestMetaData method testImpact.

public void testImpact() {
    MetaDataBuilder builder = new MetaDataBuilder(Foo.class);
    ManagedTypeMetaData type = builder.build();
    Map<String, ManagedMethodMetaData> methodMap = getMethodMap(type);
    ManagedMethodMetaData read = methodMap.get("read");
    assertEquals(ImpactType.READ, read.getImpact());
    ManagedMethodMetaData write = methodMap.get("write");
    assertEquals(ImpactType.WRITE, write.getImpact());
    ManagedMethodMetaData idempotentWrite = methodMap.get("idempotentWrite");
    assertEquals(ImpactType.IDEMPOTENT_WRITE, idempotentWrite.getImpact());
}
Also used : ManagedTypeMetaData(org.exoplatform.management.spi.ManagedTypeMetaData) ManagedMethodMetaData(org.exoplatform.management.spi.ManagedMethodMetaData) MetaDataBuilder(org.exoplatform.container.management.MetaDataBuilder)

Aggregations

ManagedTypeMetaData (org.exoplatform.management.spi.ManagedTypeMetaData)3 ManagedMethodMetaData (org.exoplatform.management.spi.ManagedMethodMetaData)2 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExoContainer (org.exoplatform.container.ExoContainer)1 RootContainer (org.exoplatform.container.RootContainer)1 MetaDataBuilder (org.exoplatform.container.management.MetaDataBuilder)1 Container (org.exoplatform.container.spi.Container)1 ManagementAware (org.exoplatform.management.ManagementAware)1 Impact (org.exoplatform.management.annotations.Impact)1 ImpactType (org.exoplatform.management.annotations.ImpactType)1 Managed (org.exoplatform.management.annotations.Managed)1 ManagedBy (org.exoplatform.management.annotations.ManagedBy)1 ManagedDescription (org.exoplatform.management.annotations.ManagedDescription)1 ManagedName (org.exoplatform.management.annotations.ManagedName)1 ManagedMethodParameterMetaData (org.exoplatform.management.spi.ManagedMethodParameterMetaData)1