Search in sources :

Example 1 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener in project motech by motech.

the class JdoListenerRegistryServiceTest method setUp.

@Before
public void setUp() {
    InstanceLifecycleListenerType[] types = { InstanceLifecycleListenerType.POST_CREATE, InstanceLifecycleListenerType.POST_DELETE };
    InstanceLifecycleListenerType[] postCreateType = { InstanceLifecycleListenerType.POST_CREATE };
    listener = new MotechLifecycleListener(Sample.class, "SampleMethodName", Sample.class.getName(), "", types, Arrays.asList(Sample.class.getName()));
    listener2 = new MotechLifecycleListener(Sample.class, "SampleMethodName2", Sample.class.getName(), "", postCreateType, Arrays.asList(Sample.class.getName()));
    listener3 = new MotechLifecycleListener(AnotherSample.class, "AnotherSampleMethodName", Sample.class.getName(), "", postCreateType, Arrays.asList(Sample.class.getName()));
    listener4 = new MotechLifecycleListener(AnotherSample.class, "AnotherSampleMethodName2", String.class.getName(), "", types, Arrays.asList(String.class.getName()));
    listener5 = new MotechLifecycleListener(AnotherSample.class, "AnotherSampleMethodName3", AnotherSample.class.getName(), "", types, Arrays.asList(AnotherSample.class.getName()));
    listener6 = new MotechLifecycleListener(AnotherSample.class, "AnotherSampleMethodName4", Object.class.getName(), "org.motechproject.mds", types, Arrays.asList(Sample.class.getName(), AnotherSample.class.getName()));
    jdoListenerRegistryService.registerListener(listener);
    jdoListenerRegistryService.registerListener(listener2);
    jdoListenerRegistryService.registerListener(listener3);
    jdoListenerRegistryService.registerListener(listener4);
    jdoListenerRegistryService.registerListener(listener5);
    jdoListenerRegistryService.registerListener(listener6);
}
Also used : Sample(org.motechproject.mds.annotations.internal.samples.Sample) AnotherSample(org.motechproject.mds.annotations.internal.samples.AnotherSample) MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener) InstanceLifecycleListenerType(org.motechproject.mds.annotations.InstanceLifecycleListenerType) AnotherSample(org.motechproject.mds.annotations.internal.samples.AnotherSample) Before(org.junit.Before)

Example 2 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener 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)

Example 3 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener in project motech by motech.

the class ProxyJdoListener method invokeMethods.

/**
 * Invokes service's methods which listen to concrete type of persistence events. A service class
 * is searched for the services exposed by OSGi. If it was found, method is invoked in the same transaction
 * as the event.
 *
 * @param event the persistence event
 * @param type the type of listener
 */
private void invokeMethods(Object event, InstanceLifecycleListenerType type) throws JdoListenerInvocationException {
    if (jdoListenerRegistryService == null) {
        this.jdoListenerRegistryService = OSGiServiceUtils.findService(bundleContext, JdoListenerRegistryService.class);
    }
    String entity = event.getClass().getName();
    List<MotechLifecycleListener> listeners = jdoListenerRegistryService.getListeners(entity, type);
    for (MotechLifecycleListener listener : listeners) {
        Set<String> methods = jdoListenerRegistryService.getMethods(listener, type);
        if (methods != null && !methods.isEmpty()) {
            Class clazz = listener.getService();
            Object service = OSGiServiceUtils.findService(bundleContext, clazz);
            if (service != null) {
                Class<?> serviceClass = service.getClass();
                for (String methodName : methods) {
                    try {
                        MethodUtils.invokeMethod(service, methodName, event);
                    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                        throw new JdoListenerInvocationException(String.format("There was an error invoking the method %s " + "from %s", methodName, serviceClass.getName()), ex);
                    }
                }
            } else {
                throw new JdoListenerInvocationException(String.format("JDO instance lifecycle event has taken place and is " + "tracked by the %s, but the OSGi service for this class cannot be found.", clazz.getName()));
            }
        }
    }
}
Also used : MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener) JdoListenerRegistryService(org.motechproject.mds.service.JdoListenerRegistryService) InvocationTargetException(java.lang.reflect.InvocationTargetException) JdoListenerInvocationException(org.motechproject.mds.exception.jdo.JdoListenerInvocationException)

Example 4 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener in project motech by motech.

the class JdoListenerRegistryServiceImpl method updateEntityNames.

@Override
public void updateEntityNames() {
    for (MotechLifecycleListener listener : listeners) {
        String packageName = listener.getPackageName();
        if (!packageName.isEmpty()) {
            List<String> entityNames = new ArrayList<>();
            for (EntityDto entityDto : entityService.findEntitiesByPackage(packageName)) {
                entityNames.add(entityDto.getClassName());
            }
            listener.setEntityNames(entityNames);
        }
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener)

Example 5 with MotechLifecycleListener

use of org.motechproject.mds.listener.MotechLifecycleListener in project motech by motech.

the class JdoListenerRegistryServiceImpl method registerListener.

@Override
public void registerListener(MotechLifecycleListener listener) {
    if (listeners.contains(listener)) {
        int index = listeners.indexOf(listener);
        MotechLifecycleListener replace = listeners.get(index);
        replace.addMethod(listener.getMethodsByType());
        listeners.set(index, replace);
    } else {
        listeners.add(listener);
    }
}
Also used : MotechLifecycleListener(org.motechproject.mds.listener.MotechLifecycleListener)

Aggregations

MotechLifecycleListener (org.motechproject.mds.listener.MotechLifecycleListener)8 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 InstanceLifecycleListenerType (org.motechproject.mds.annotations.InstanceLifecycleListenerType)2 Sample (org.motechproject.mds.annotations.internal.samples.Sample)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InstanceLifecycleListener (org.motechproject.mds.annotations.InstanceLifecycleListener)1 AnotherSample (org.motechproject.mds.annotations.internal.samples.AnotherSample)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 JdoListenerInvocationException (org.motechproject.mds.exception.jdo.JdoListenerInvocationException)1 JdoListenerRegistryService (org.motechproject.mds.service.JdoListenerRegistryService)1