blob: d9e6484f32be68f0850090bbec78c277c6265f29 [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 */
tomd7356722014-08-26 01:07:39 -070016package org.onlab.onos.event;
17
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 */
tom96dfcab2014-08-28 09:26:03 -070031public class AbstractListenerRegistry<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
36 private final Set<L> listeners = new CopyOnWriteArraySet<>();
Thomas Vachuska7d693f52014-10-21 19:17:57 -070037 private volatile boolean shutdown = false;
tomd7356722014-08-26 01:07:39 -070038
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);
tom19bf4212014-08-29 13:08:29 -070064 } catch (Exception error) {
tomd7356722014-08-26 01:07:39 -070065 reportProblem(event, error);
66 }
67 }
68 }
69
tomb36046e2014-08-27 00:22:24 -070070 /**
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 Vachuska7d693f52014-10-21 19:17:57 -070077 if (!shutdown) {
78 log.warn("Exception encountered while processing event " + event, error);
79 }
tomd7356722014-08-26 01:07:39 -070080 }
81
Thomas Vachuska7d693f52014-10-21 19:17:57 -070082 /**
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
tomd7356722014-08-26 01:07:39 -070097}