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 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 24 | import static org.slf4j.LoggerFactory.getLogger; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 27 | * Base implementation of an event sink and a registry capable of tracking |
| 28 | * listeners and dispatching events to them as part of event sink processing. |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 29 | */ |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 30 | public class ListenerRegistry<E extends Event, L extends EventListener<E>> |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 31 | implements EventSink<E> { |
| 32 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 33 | private final Logger log = getLogger(getClass()); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 34 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 35 | private long lastStart; |
| 36 | private L lastListener; |
| 37 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 38 | /** |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 39 | * Set of listeners that have registered. |
| 40 | */ |
| 41 | protected final Set<L> listeners = new CopyOnWriteArraySet<>(); |
| 42 | |
| 43 | |
| 44 | /** |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 45 | * Adds the specified listener. |
| 46 | * |
| 47 | * @param listener listener to be added |
| 48 | */ |
| 49 | public void addListener(L listener) { |
| 50 | checkNotNull(listener, "Listener cannot be null"); |
| 51 | listeners.add(listener); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Removes the specified listener. |
| 56 | * |
| 57 | * @param listener listener to be removed |
| 58 | */ |
| 59 | public void removeListener(L listener) { |
| 60 | checkNotNull(listener, "Listener cannot be null"); |
Thomas Vachuska | 1deab53 | 2015-05-19 13:49:55 -0700 | [diff] [blame^] | 61 | if (!listeners.remove(listener)) { |
| 62 | log.warn("Listener {} not registered", listener); |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 63 | } |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void process(E event) { |
| 68 | for (L listener : listeners) { |
| 69 | try { |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 70 | lastListener = listener; |
| 71 | lastStart = System.currentTimeMillis(); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 72 | listener.event(event); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 73 | lastStart = 0; |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 74 | } catch (Exception error) { |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 75 | reportProblem(event, error); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public void onProcessLimit() { |
| 82 | if (lastStart > 0) { |
| 83 | log.error("Listener {} exceeded execution time limit: {} ms; ejected", |
| 84 | lastListener.getClass().getName(), |
| 85 | System.currentTimeMillis() - lastStart); |
| 86 | removeListener(lastListener); |
| 87 | } |
| 88 | } |
| 89 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Reports a problem encountered while processing an event. |
| 92 | * |
| 93 | * @param event event being processed |
| 94 | * @param error error encountered while processing |
| 95 | */ |
| 96 | protected void reportProblem(E event, Throwable error) { |
Thomas Vachuska | 1deab53 | 2015-05-19 13:49:55 -0700 | [diff] [blame^] | 97 | log.warn("Exception encountered while processing event " + event, error); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 98 | } |
| 99 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 100 | } |