Search in sources :

Example 1 with JdoListenerRegistryService

use of org.motechproject.mds.service.JdoListenerRegistryService 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)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 JdoListenerInvocationException (org.motechproject.mds.exception.jdo.JdoListenerInvocationException)1 MotechLifecycleListener (org.motechproject.mds.listener.MotechLifecycleListener)1 JdoListenerRegistryService (org.motechproject.mds.service.JdoListenerRegistryService)1