View Javadoc
1   package com.atlassian.plugin.event.impl;
2   
3   import com.atlassian.event.api.EventListener;
4   import com.atlassian.event.api.EventPublisher;
5   import com.atlassian.event.config.ListenerHandlersConfiguration;
6   import com.atlassian.event.internal.AsynchronousAbleEventDispatcher;
7   import com.atlassian.event.internal.DirectEventExecutorFactory;
8   import com.atlassian.event.internal.EventPublisherImpl;
9   import com.atlassian.event.internal.EventThreadPoolConfigurationImpl;
10  import com.atlassian.event.spi.EventDispatcher;
11  import com.atlassian.event.spi.EventExecutorFactory;
12  import com.atlassian.event.spi.ListenerHandler;
13  import com.atlassian.plugin.event.NotificationException;
14  import com.atlassian.plugin.event.PluginEventManager;
15  import com.atlassian.plugin.scope.ScopeManager;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import static com.atlassian.plugin.util.Assertions.notNull;
21  
22  /**
23   * Simple, synchronous event manager that uses one or more method selectors to determine event listeners.
24   * <p>
25   * The default method selectors are {@link MethodNameListenerMethodSelector} and
26   * {@link AnnotationListenerMethodSelector}.
27   */
28  public class DefaultPluginEventManager implements PluginEventManager {
29      private final EventPublisher eventPublisher;
30  
31      public DefaultPluginEventManager() {
32          this(defaultMethodSelectors());
33      }
34  
35      /**
36       * Uses the supplied selectors to determine listener methods.
37       *
38       * @param selectors used to determine which are listener methods
39       */
40      public DefaultPluginEventManager(final ListenerMethodSelector... selectors) {
41          final ListenerHandlersConfiguration configuration = new ListenerHandlersConfiguration() {
42              public List<ListenerHandler> getListenerHandlers() {
43                  final List<ListenerHandler> handlers = new ArrayList<>(selectors.length);
44                  for (final ListenerMethodSelector selector : selectors) {
45                      handlers.add(new MethodSelectorListenerHandler(selector));
46                  }
47                  return handlers;
48              }
49          };
50  
51          final EventExecutorFactory executorFactory = new DirectEventExecutorFactory(new EventThreadPoolConfigurationImpl());
52          final EventDispatcher eventDispatcher = new AsynchronousAbleEventDispatcher(executorFactory);
53          eventPublisher = new EventPublisherImpl(eventDispatcher, configuration);
54      }
55  
56      /**
57       * @deprecated in 5.0 for removal in 6.0 when {@link ScopeManager} will be removed.
58       *             Use {@link #DefaultPluginEventManager()} instead.
59       */
60      @Deprecated
61      public DefaultPluginEventManager(ScopeManager scopeManager) {
62          this(defaultMethodSelectors());
63      }
64  
65      /**
66       * @deprecated in 5.0 for removal in 6.0 when {@link ScopeManager} will be removed.
67       *             Use {@link #DefaultPluginEventManager(ListenerMethodSelector[])} instead.
68       */
69      @Deprecated
70      public DefaultPluginEventManager(ScopeManager scopeManager, final ListenerMethodSelector... selectors) {
71          this(selectors);
72      }
73  
74      /**
75       * Delegate all event publication to the supplied {@code EventPublisher}.
76       */
77      public DefaultPluginEventManager(final EventPublisher eventPublisher) {
78          this.eventPublisher = notNull("eventPublisher", eventPublisher);
79      }
80  
81      public void register(final Object listener) {
82          eventPublisher.register(notNull("listener", listener));
83      }
84  
85      public void unregister(final Object listener) {
86          eventPublisher.unregister(notNull("listener", listener));
87      }
88  
89      public void broadcast(final Object event) throws NotificationException {
90          notNull("event", event);
91          try {
92              eventPublisher.publish(event);
93          } catch (final RuntimeException e) {
94              throw new NotificationException(e);
95          }
96      }
97  
98      public EventPublisher getEventPublisher() {
99          return eventPublisher;
100     }
101 
102     static ListenerMethodSelector[] defaultMethodSelectors() {
103         final ListenerMethodSelector methodNames = new MethodNameListenerMethodSelector();
104         final ListenerMethodSelector pluginEvent = new AnnotationListenerMethodSelector();
105         final ListenerMethodSelector eventListener = new AnnotationListenerMethodSelector(EventListener.class);
106         return new ListenerMethodSelector[]{methodNames, pluginEvent, eventListener};
107     }
108 }