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 | */ |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 31 | public class ListenerRegistry<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 | |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 36 | private volatile boolean shutdown = false; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 37 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame^] | 38 | private long lastStart; |
| 39 | private L lastListener; |
| 40 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 41 | /** |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 42 | * Set of listeners that have registered. |
| 43 | */ |
| 44 | protected final Set<L> listeners = new CopyOnWriteArraySet<>(); |
| 45 | |
| 46 | |
| 47 | /** |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 48 | * Adds the specified listener. |
| 49 | * |
| 50 | * @param listener listener to be added |
| 51 | */ |
| 52 | public void addListener(L listener) { |
| 53 | checkNotNull(listener, "Listener cannot be null"); |
| 54 | listeners.add(listener); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Removes the specified listener. |
| 59 | * |
| 60 | * @param listener listener to be removed |
| 61 | */ |
| 62 | public void removeListener(L listener) { |
| 63 | checkNotNull(listener, "Listener cannot be null"); |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 64 | if (checkForNonRegistrant()) { |
| 65 | checkArgument(listeners.remove(listener), "Listener not registered"); |
| 66 | } |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void process(E event) { |
| 71 | for (L listener : listeners) { |
| 72 | try { |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame^] | 73 | lastListener = listener; |
| 74 | lastStart = System.currentTimeMillis(); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 75 | listener.event(event); |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame^] | 76 | lastStart = 0; |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 77 | } catch (Exception error) { |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 78 | reportProblem(event, error); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame^] | 83 | @Override |
| 84 | public void onProcessLimit() { |
| 85 | if (lastStart > 0) { |
| 86 | log.error("Listener {} exceeded execution time limit: {} ms; ejected", |
| 87 | lastListener.getClass().getName(), |
| 88 | System.currentTimeMillis() - lastStart); |
| 89 | removeListener(lastListener); |
| 90 | } |
| 91 | } |
| 92 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 93 | /** |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 94 | * Predicate indicating whether we should throw an exception if the |
| 95 | * argument to {@link #removeListener} is not in the current set of |
| 96 | * listeners. |
| 97 | * <p> |
| 98 | * This default implementation returns <code>true</code>. |
| 99 | * |
| 100 | * @return true if non-listed listeners should cause exception on remove |
| 101 | */ |
| 102 | protected boolean checkForNonRegistrant() { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 107 | * Reports a problem encountered while processing an event. |
| 108 | * |
| 109 | * @param event event being processed |
| 110 | * @param error error encountered while processing |
| 111 | */ |
| 112 | protected void reportProblem(E event, Throwable error) { |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 113 | if (!shutdown) { |
| 114 | log.warn("Exception encountered while processing event " + event, error); |
| 115 | } |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Prepares the registry for normal operation. |
| 120 | */ |
| 121 | public void activate() { |
| 122 | shutdown = false; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Prepares the registry for shutdown. |
| 127 | */ |
| 128 | public void deactivate() { |
| 129 | shutdown = true; |
| 130 | } |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 131 | } |