Created
August 31, 2014 18:34
-
-
Save crudh/18de0d2114b52a6efbe9 to your computer and use it in GitHub Desktop.
Simple not optimized Apache Wicket IEventDispatcher implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyPayloadEventDispatcher implements IEventDispatcher { | |
@Override | |
public void dispatchEvent(Object sink, IEvent event, Component component) { | |
// Check that the payload isn't null and that the sink, the receiver, has the annotation | |
if (event.getPayload() != null && sink.getClass().isAnnotationPresent(EventPayloadReceiver.class)) { | |
EventPayloadReceiver eventReceiver = sink.getClass().getAnnotation(EventPayloadReceiver.class); | |
// Check that the annotation is configured with the current event | |
if (Arrays.asList(eventReceiver.value()).contains(event.getPayload().getClass())) { | |
// Go through all methods | |
for (Method method : sink.getClass().getDeclaredMethods()) { | |
// Check so the annoation is on the method | |
if (method.isAnnotationPresent(OnEventPayload.class)) { | |
OnEventPayload onEvent = method.getAnnotation(OnEventPayload.class); | |
// Check that the method annotation is configured with the current event | |
if (Arrays.asList(onEvent.value()).contains(event.getPayload().getClass())) { | |
try { | |
Class[] parTypes = method.getParameterTypes(); | |
// Check that the method accepts one parameter and that it is the same type as the event | |
if (parTypes.length == 1 && parTypes[0].isAssignableFrom(event.getPayload().getClass())) { | |
// Deliver the event | |
method.invoke(sink, event.getPayload()); | |
} | |
} catch (Exception e) { | |
throw new RuntimeException("Exception when delivering event object " + event.getPayload().getClass() + " to component " + sink.getClass() + " and method " + method.getName(), e); | |
} | |
// We only deliver an event once to a single component, if there are multiple methods configured for it we ignore all but one | |
return; | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment