1 package dst.ass2.aop.event;
3 import java.util.ArrayList;
6 import org.springframework.aop.support.AopUtils;
8 import dst.ass2.aop.IPluginExecutable;
11 * Stateful event bus that stores events triggered by executable plugins.
13 * Note that this implementation is thread safe.
15 public class EventBus {
16 private static final EventBus instance = new EventBus();
17 private final List<Event> events = new ArrayList<Event>();
19 public static EventBus getInstance() {
27 * Returns all events of the certain type(s).<br/>
28 * If no types are specified, all events are returned instead.
30 * @param types the event types
31 * @return list of all events of the given types.
33 public List<Event> getEvents(EventType... types) {
34 synchronized (events) {
35 if (types == null || types.length == 0) {
36 return new ArrayList<Event>(events);
38 List<Event> list = new ArrayList<Event>();
39 for (Event event : events) {
40 for (EventType type : types) {
41 if (type == event.getType()) {
52 * Resets the event bus by purging the event history.
54 public synchronized void reset() {
55 synchronized (events) {
61 * Adds a new event of a certain type triggered by the given plugin.
63 * @param type the event type
64 * @param pluginExecutable the plugin that triggered the event
65 * @param message the event message
67 @SuppressWarnings("unchecked")
68 public void add(EventType type, IPluginExecutable pluginExecutable, String message) {
69 add(type, (Class<? extends IPluginExecutable>) AopUtils.getTargetClass(pluginExecutable), message);
73 * Adds a new event of a certain type triggered by a plugin of the given type.
75 * @param type the event type
76 * @param pluginType the type of the plugin
77 * @param message the event message
79 public void add(EventType type, Class<? extends IPluginExecutable> pluginType, String message) {
80 Event event = new Event(type, pluginType, message);
81 synchronized (events) {
87 * Returns the number of events of a certain type fired by this event bus.
89 * @param type the event type
90 * @return number of events
92 public int count(EventType type) {
94 synchronized (events) {
95 for (Event event : events) {
96 if (event.getType() == type) {
105 * Returns the number of events fired so far.
107 * @return number of events
110 return events.size();
114 * Checks if there was at least one event of a certain type triggered by a plugin with the given full-qualified
117 * If {@code pluginType} is {@code null}, the type of the plugin is not checked. The same is true for {@code type}.
118 * If all parameters are {@code null}, {@code true} is returned if there is at least one event.
120 * @param pluginType the class name of the plugin
121 * @param type the type of the event
122 * @return {@code true} if there is at least one event matching the criteria, {@code false} otherwise
124 public boolean has(String pluginType, EventType type) {
125 synchronized (events) {
126 for (Event event : events) {
127 if ((pluginType == null || pluginType.equals(event.getPluginClass().getName()))
128 && (type == null || type == event.getType())) {