Search in sources :

Example 1 with InstanceLifecycleListener

use of org.motechproject.mds.annotations.InstanceLifecycleListener in project motech by motech.

the class InstanceLifecycleListenerProcessor method processAnnotations.

/**
 * Processes <code>InstanceLifecycleListener</code> annotations in the given bundle.
 * When the annotation is found, it is verified if following rules are fulfilled:
 * - array of <code>InstanceLifecycleListenerType</code> cannot be empty
 * - annotated methods have exactly one parameter
 *
 * @param bundle the bundle which is processed
 */
public void processAnnotations(Bundle bundle) {
    Set<Method> methods = ReflectionsUtil.getMethods(InstanceLifecycleListener.class, bundle);
    for (Method method : methods) {
        InstanceLifecycleListener annotation = ReflectionsUtil.getAnnotationClassLoaderSafe(method, method.getDeclaringClass(), InstanceLifecycleListener.class);
        InstanceLifecycleListenerType[] types = annotation.value();
        String packageName = annotation.packageName();
        if (ArrayUtils.isEmpty(types)) {
            LOGGER.error("InstanceLifecycleListener annotation for {} is specified but its value is missing.", method.toString());
        } else if (method.getParameterTypes().length != NUMBER_OF_PARAMETERS) {
            LOGGER.error("InstanceLifecycleListener annotation cannot be specified for method {}, because it does not have exactly " + "{} parameter", method.toString(), NUMBER_OF_PARAMETERS);
        } else {
            String paramType = method.getParameterTypes()[0].getName();
            if (!packageName.isEmpty() && !paramType.equals(Object.class.getName())) {
                LOGGER.error("InstanceLifecycleListener annotation cannot be specified for method {}, " + "because its parameter is not of type Object.", method.toString());
            } else {
                MotechLifecycleListener listener = new MotechLifecycleListener(method.getDeclaringClass(), method.getName(), paramType, packageName, types, Arrays.asList(paramType));
                jdoListenerRegistryService.registerListener(listener);
            }
        }
    }
}
Also used : InstanceLifecycleListener(org.motechproject.mds.annotations.InstanceLifecycleListener) MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener) Method(java.lang.reflect.Method) InstanceLifecycleListenerType(org.motechproject.mds.annotations.InstanceLifecycleListenerType)

Aggregations

Method (java.lang.reflect.Method)1 InstanceLifecycleListener (org.motechproject.mds.annotations.InstanceLifecycleListener)1 InstanceLifecycleListenerType (org.motechproject.mds.annotations.InstanceLifecycleListenerType)1 MotechLifecycleListener (org.motechproject.mds.listener.MotechLifecycleListener)1