1 package com.atlassian.plugin.event.impl;
2
3 import static com.atlassian.plugin.util.Assertions.notNull;
4
5 import com.atlassian.event.api.EventListener;
6 import com.atlassian.event.api.EventPublisher;
7 import com.atlassian.event.config.ListenerHandlersConfiguration;
8 import com.atlassian.event.internal.AsynchronousAbleEventDispatcher;
9 import com.atlassian.event.internal.EventExecutorFactoryImpl;
10 import com.atlassian.event.internal.EventPublisherImpl;
11 import com.atlassian.event.internal.EventThreadPoolConfigurationImpl;
12 import com.atlassian.event.spi.EventDispatcher;
13 import com.atlassian.event.spi.EventExecutorFactory;
14 import com.atlassian.event.spi.ListenerHandler;
15 import com.atlassian.plugin.event.NotificationException;
16 import com.atlassian.plugin.event.PluginEventManager;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21
22
23
24
25
26
27 public class DefaultPluginEventManager implements PluginEventManager
28 {
29 private final EventPublisher publisher;
30
31
32
33
34
35
36 public DefaultPluginEventManager(final ListenerMethodSelector... selectors)
37 {
38 final ListenerHandlersConfiguration configuration = new ListenerHandlersConfiguration()
39 {
40 public List<ListenerHandler> getListenerHandlers()
41 {
42 final List<ListenerHandler> handlers = new ArrayList<ListenerHandler>(selectors.length);
43 for (final ListenerMethodSelector selector : selectors)
44 {
45 handlers.add(new MethodSelectorListenerHandler(selector));
46 }
47 return handlers;
48 }
49 };
50
51 final EventExecutorFactory executorFactory = new EventExecutorFactoryImpl(new EventThreadPoolConfigurationImpl());
52 final EventDispatcher eventDispatcher = new AsynchronousAbleEventDispatcher(executorFactory);
53 publisher = new EventPublisherImpl(eventDispatcher, configuration);
54 }
55
56 public DefaultPluginEventManager()
57 {
58 this(defaultMethodSelectors());
59 }
60
61
62
63
64 public DefaultPluginEventManager(final EventPublisher publisher)
65 {
66 this.publisher = notNull("publisher", publisher);
67 }
68
69 public void register(final Object listener)
70 {
71 publisher.register(notNull("listener", listener));
72 }
73
74 public void unregister(final Object listener)
75 {
76 publisher.unregister(notNull("listener", listener));
77 }
78
79 public void broadcast(final Object event) throws NotificationException
80 {
81 notNull("event", event);
82 try
83 {
84 publisher.publish(event);
85 }
86 catch (final RuntimeException e)
87 {
88 throw new NotificationException(e);
89 }
90 }
91
92 static ListenerMethodSelector[] defaultMethodSelectors()
93 {
94 final ListenerMethodSelector methodNames = new MethodNameListenerMethodSelector();
95 final ListenerMethodSelector pluginEvent = new AnnotationListenerMethodSelector();
96 final ListenerMethodSelector eventListener = new AnnotationListenerMethodSelector(EventListener.class);
97 return new ListenerMethodSelector[] { methodNames, pluginEvent, eventListener };
98 }
99 }