This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.event;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import java.util.*;
026import java.util.concurrent.ConcurrentHashMap;
027
028public class EventManager implements EventSource
029{
030
031    private static final Logger log = LoggerFactory.getLogger(EventManager.class);
032
033    private final ConcurrentHashMap<EventType<? extends Event>, Set<EventHandler>> handlerMap = new ConcurrentHashMap<>();
034
035    private final Object source;
036
037    public EventManager(Object source) {
038        if (source==null) {
039            throw new IllegalArgumentException("The source may not be null");
040        }
041        this.source = source;
042    }
043
044    @Override
045    public <T extends Event> void registerEventHandler(EventType<T> type, EventHandler<? super T> eventHandler) {
046        Set<EventHandler> handlers = handlerMap.computeIfAbsent(type, t -> new LinkedHashSet<>());
047        if (!handlers.contains(eventHandler)) {
048            handlers.add(eventHandler);
049        }
050    }
051
052    @Override
053    public <T extends Event> void unregisterEventHandler(EventType<T> type, EventHandler<? super T> eventHandler) {
054        if (handlerMap.containsKey(type)) {
055            handlerMap.get(type).remove(eventHandler);
056        }
057    }
058
059    public void fireEvent(Event fireEvent) {
060        final EventType<? extends Event> type = fireEvent.getType();
061        Event event;
062        if (fireEvent.getSource()!=source) {
063            event = fireEvent.copyFor(source);
064        } else {
065            event = fireEvent;
066        }
067        for (EventType<? extends Event> handlerType : handlerMap.keySet()) {
068            if (EventType.isInstanceOf(type, handlerType)) {
069                    for (EventHandler handler : handlerMap.get(handlerType)) {
070                        try {
071                            handler.handle(event);
072                        } catch (Exception e) {
073                            // We catch all errors from handlers
074                            log.error("An error occured during event handling: {}", e.getMessage(), e);
075                        }
076                    }
077            }
078        }
079    }
080}