Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.event; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 17 | |
| 18 | import org.slf4j.Logger; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 19 | |
| 20 | import java.util.Set; |
| 21 | import java.util.concurrent.CopyOnWriteArraySet; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 25 | import static org.slf4j.LoggerFactory.getLogger; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 28 | * Base implementation of an event sink and a registry capable of tracking |
| 29 | * listeners and dispatching events to them as part of event sink processing. |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 30 | */ |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 31 | public class AbstractListenerRegistry<E extends Event, L extends EventListener<E>> |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 32 | implements EventSink<E> { |
| 33 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 34 | private final Logger log = getLogger(getClass()); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 35 | |
| 36 | private final Set<L> listeners = new CopyOnWriteArraySet<>(); |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 37 | private volatile boolean shutdown = false; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Adds the specified listener. |
| 41 | * |
| 42 | * @param listener listener to be added |
| 43 | */ |
| 44 | public void addListener(L listener) { |
| 45 | checkNotNull(listener, "Listener cannot be null"); |
| 46 | listeners.add(listener); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Removes the specified listener. |
| 51 | * |
| 52 | * @param listener listener to be removed |
| 53 | */ |
| 54 | public void removeListener(L listener) { |
| 55 | checkNotNull(listener, "Listener cannot be null"); |
| 56 | checkArgument(listeners.remove(listener), "Listener not registered"); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void process(E event) { |
| 61 | for (L listener : listeners) { |
| 62 | try { |
| 63 | listener.event(event); |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 64 | } catch (Exception error) { |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 65 | reportProblem(event, error); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Reports a problem encountered while processing an event. |
| 72 | * |
| 73 | * @param event event being processed |
| 74 | * @param error error encountered while processing |
| 75 | */ |
| 76 | protected void reportProblem(E event, Throwable error) { |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 77 | if (!shutdown) { |
| 78 | log.warn("Exception encountered while processing event " + event, error); |
| 79 | } |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Prepares the registry for normal operation. |
| 84 | */ |
| 85 | public void activate() { |
| 86 | shutdown = false; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Prepares the registry for shutdown. |
| 91 | */ |
| 92 | public void deactivate() { |
| 93 | shutdown = true; |
| 94 | } |
| 95 | |
| 96 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 97 | } |