blob: 6a570c5bb42509386f0cbf6b8022ef52997f5d9e [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
tomd7356722014-08-26 01:07:39 -070023import static com.google.common.base.Preconditions.checkNotNull;
tom5f38b3a2014-08-27 23:50:54 -070024import static org.slf4j.LoggerFactory.getLogger;
tomd7356722014-08-26 01:07:39 -070025
26/**
tom96dfcab2014-08-28 09:26:03 -070027 * 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.
tomd7356722014-08-26 01:07:39 -070029 */
Simon Huntff663742015-05-14 13:33:05 -070030public class ListenerRegistry<E extends Event, L extends EventListener<E>>
tomd7356722014-08-26 01:07:39 -070031 implements EventSink<E> {
32
tom5f38b3a2014-08-27 23:50:54 -070033 private final Logger log = getLogger(getClass());
tomd7356722014-08-26 01:07:39 -070034
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070035 private long lastStart;
36 private L lastListener;
37
tomd7356722014-08-26 01:07:39 -070038 /**
Simon Huntff663742015-05-14 13:33:05 -070039 * Set of listeners that have registered.
40 */
41 protected final Set<L> listeners = new CopyOnWriteArraySet<>();
42
43
44 /**
tomd7356722014-08-26 01:07:39 -070045 * 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 Vachuska1deab532015-05-19 13:49:55 -070061 if (!listeners.remove(listener)) {
62 log.warn("Listener {} not registered", listener);
Simon Huntff663742015-05-14 13:33:05 -070063 }
tomd7356722014-08-26 01:07:39 -070064 }
65
66 @Override
67 public void process(E event) {
68 for (L listener : listeners) {
69 try {
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070070 lastListener = listener;
71 lastStart = System.currentTimeMillis();
tomd7356722014-08-26 01:07:39 -070072 listener.event(event);
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070073 lastStart = 0;
tom19bf4212014-08-29 13:08:29 -070074 } catch (Exception error) {
tomd7356722014-08-26 01:07:39 -070075 reportProblem(event, error);
76 }
77 }
78 }
79
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070080 @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
tomb36046e2014-08-27 00:22:24 -070090 /**
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 Vachuska1deab532015-05-19 13:49:55 -070097 log.warn("Exception encountered while processing event " + event, error);
tomd7356722014-08-26 01:07:39 -070098 }
99
tomd7356722014-08-26 01:07:39 -0700100}