This project has retired. For details please refer to its Attic page.
EventManager xref
View Javadoc
1   package org.apache.archiva.event;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.util.*;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  public class EventManager implements EventSource
29  {
30  
31      private static final Logger log = LoggerFactory.getLogger(EventManager.class);
32  
33      private final ConcurrentHashMap<EventType<? extends Event>, Set<EventHandler>> handlerMap = new ConcurrentHashMap<>();
34  
35      private final Object source;
36  
37      public EventManager(Object source) {
38          if (source==null) {
39              throw new IllegalArgumentException("The source may not be null");
40          }
41          this.source = source;
42      }
43  
44      @Override
45      public <T extends Event> void registerEventHandler(EventType<T> type, EventHandler<? super T> eventHandler) {
46          Set<EventHandler> handlers = handlerMap.computeIfAbsent(type, t -> new LinkedHashSet<>());
47          if (!handlers.contains(eventHandler)) {
48              handlers.add(eventHandler);
49          }
50      }
51  
52      @Override
53      public <T extends Event> void unregisterEventHandler(EventType<T> type, EventHandler<? super T> eventHandler) {
54          if (handlerMap.containsKey(type)) {
55              handlerMap.get(type).remove(eventHandler);
56          }
57      }
58  
59      public void fireEvent(Event fireEvent) {
60          final EventType<? extends Event> type = fireEvent.getType();
61          Event event;
62          if (fireEvent.getSource()!=source) {
63              event = fireEvent.copyFor(source);
64          } else {
65              event = fireEvent;
66          }
67          for (EventType<? extends Event> handlerType : handlerMap.keySet()) {
68              if (EventType.isInstanceOf(type, handlerType)) {
69                      for (EventHandler handler : handlerMap.get(handlerType)) {
70                          try {
71                              handler.handle(event);
72                          } catch (Exception e) {
73                              // We catch all errors from handlers
74                              log.error("An error occured during event handling: {}", e.getMessage(), e);
75                          }
76                      }
77              }
78          }
79      }
80  }