01: /*
02: * This software is OSI Certified Open Source Software.
03: * OSI Certified is a certification mark of the Open Source Initiative. The
04: * license (Mozilla version 1.0) can be read at the MMBase site. See
05: * http://www.MMBase.org/license
06: */
07: package org.mmbase.core.event;
08:
09: import java.util.*;
10: import java.util.concurrent.CopyOnWriteArraySet;
11: import org.mmbase.util.logging.Logger;
12: import org.mmbase.util.logging.Logging;
13:
14: /**
15: * An EventBroker which administrates the listeners in a {@link java.util.concurrent.CopyOnWriteArraySet}.
16: *
17: * @author Ernst Bunders
18: * @since MMBase-1.8
19: * @version $Id: AbstractEventBroker.java,v 1.20 2008/02/03 17:33:58 nklasens Exp $
20: */
21: public abstract class AbstractEventBroker extends EventBroker {
22:
23: private static final Logger log = Logging
24: .getLoggerInstance(AbstractEventBroker.class);
25:
26: private final Set<EventListener> listeners = new CopyOnWriteArraySet<EventListener>();
27:
28: protected Collection<EventListener> backing() {
29: return listeners;
30: }
31:
32: public boolean addListener(EventListener listener) {
33: if (canBrokerForListener(listener)) {
34: if (!listeners.add(listener)) {
35: if (log.isDebugEnabled()) {
36: log.debug("" + listener + " was already in "
37: + getClass() + ". Ignored.");
38: }
39: return false;
40: } else if (log.isDebugEnabled()) {
41: log.debug("listener added to " + getClass());
42: }
43: return true;
44: } else {
45: log.warn("Ignored listener for" + getClass()
46: + " because it cannot broker for that.");
47: }
48: return false;
49: }
50:
51: public void removeListener(EventListener listener) {
52: if (!listeners.remove(listener)) {
53: log.warn("Tried to remove " + listener + " from "
54: + getClass() + " but it was not found. Ignored.");
55: }
56:
57: }
58:
59: public String toString() {
60: return "Abstract Event Broker";
61: }
62:
63: }
|