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);
}
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);
}
}
}
}
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()));
}
}
}
}
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);
}
}
}
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);
}
}
Aggregations