blob: c2e1aed0fb7b40583f96617e66457300787b3cd3 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.event;
tomd7356722014-08-26 01:07:39 -070017
18import org.slf4j.Logger;
tomd7356722014-08-26 01:07:39 -070019
20import java.util.Set;
21import java.util.concurrent.CopyOnWriteArraySet;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
tom5f38b3a2014-08-27 23:50:54 -070025import static org.slf4j.LoggerFactory.getLogger;
tomd7356722014-08-26 01:07:39 -070026
27/**
tom96dfcab2014-08-28 09:26:03 -070028 * 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.
tomd7356722014-08-26 01:07:39 -070030 */
Simon Huntff663742015-05-14 13:33:05 -070031public class ListenerRegistry<E extends Event, L extends EventListener<E>>
tomd7356722014-08-26 01:07:39 -070032 implements EventSink<E> {
33
tom5f38b3a2014-08-27 23:50:54 -070034 private final Logger log = getLogger(getClass());
tomd7356722014-08-26 01:07:39 -070035
Thomas Vachuska7d693f52014-10-21 19:17:57 -070036 private volatile boolean shutdown = false;
tomd7356722014-08-26 01:07:39 -070037
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070038 private long lastStart;
39 private L lastListener;
40
tomd7356722014-08-26 01:07:39 -070041 /**
Simon Huntff663742015-05-14 13:33:05 -070042 * Set of listeners that have registered.
43 */
44 protected final Set<L> listeners = new CopyOnWriteArraySet<>();
45
46
47 /**
tomd7356722014-08-26 01:07:39 -070048 * 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 Huntff663742015-05-14 13:33:05 -070064 if (checkForNonRegistrant()) {
65 checkArgument(listeners.remove(listener), "Listener not registered");
66 }
tomd7356722014-08-26 01:07:39 -070067 }
68
69 @Override
70 public void process(E event) {
71 for (L listener : listeners) {
72 try {
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070073 lastListener = listener;
74 lastStart = System.currentTimeMillis();
tomd7356722014-08-26 01:07:39 -070075 listener.event(event);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070076 lastStart = 0;
tom19bf4212014-08-29 13:08:29 -070077 } catch (Exception error) {
tomd7356722014-08-26 01:07:39 -070078 reportProblem(event, error);
79 }
80 }
81 }
82
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070083 @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
tomb36046e2014-08-27 00:22:24 -070093 /**
Simon Huntff663742015-05-14 13:33:05 -070094 * 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 /**
tomb36046e2014-08-27 00:22:24 -0700107 * 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 Vachuska7d693f52014-10-21 19:17:57 -0700113 if (!shutdown) {
114 log.warn("Exception encountered while processing event " + event, error);
115 }
tomd7356722014-08-26 01:07:39 -0700116 }
117
Thomas Vachuska7d693f52014-10-21 19:17:57 -0700118 /**
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 }
tomd7356722014-08-26 01:07:39 -0700131}