KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > core > event > AbstractEventBroker


1 /*
2  * This software is OSI Certified Open Source Software.
3  * OSI Certified is a certification mark of the Open Source Initiative. The
4  * license (Mozilla version 1.0) can be read at the MMBase site. See
5  * http://www.MMBase.org/license
6  */

7 package org.mmbase.core.event;
8
9 import java.util.*;
10
11 import javax.swing.border.TitledBorder JavaDoc;
12
13 import org.mmbase.util.HashCodeUtil;
14 import org.mmbase.util.logging.Logger;
15 import org.mmbase.util.logging.Logging;
16
17 import sun.rmi.runtime.GetThreadPoolAction;
18 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;
19
20 /**
21  * This is the base class for all event brokers in mmbase. the function of an
22  * event broker is to know about a specific kind of event, as well as a specific
23  * kind of event listener. All events should be derived from the
24  * org.mmbase.core.event.Event class, and all listeners from the
25  * org.mmbase.core.event.EventListener interface.<br/> Allthough event
26  * listeners have to implement the EventListener interface, the actual method
27  * that will be called to pass on the event is not part of this interface, as it
28  * is specific for the kind of event you want to listen for. This is a contract
29  * between the broker implementation and the event listerer interface.<br/>
30  * This class dous most of the work of keeping references to all the listeners
31  * and allowing for adding/removing them. Only a fiew type specific actions are
32  * delegated to the super class.<br/> The EventListener also provides a method
33  * for passing on constraint properties to a event broker. If you want to create
34  * your own event type you can use this feature to accomplish that not all
35  * events of your type are propagated to the listener.
36  *
37  * @author Ernst Bunders
38  * @since MMBase-1.8
39  */

40 public abstract class AbstractEventBroker {
41
42     private static final Logger log = Logging.getLoggerInstance(AbstractEventBroker.class);
43
44     protected Set listeners = new CopyOnWriteArraySet();
45
46     /**
47      * this method should return true if this broker can accept and propagate
48      * events to the listener of this type. There are no fixed criteria for this.
49
50      * Most implementions do <code>event instanceof &lt;EventListener associated with this broker&gt;</code>
51      *
52      * @param listener
53      */

54     public abstract boolean canBrokerForListener(EventListener listener);
55
56     /**
57      * this method should return true if this event broker can broker for
58      * events of this type. There are no fixed criteria for this.
59      *
60      * Most implementions do <code>event instanceof &lt;EvenType associated with this broker&gt;</code>
61      *
62      * @param event
63      */

64     public abstract boolean canBrokerForEvent(Event event);
65
66     /**
67      * This method has two functions. It must cast both event and listener to
68      * the proper type and invoke the event on the listener. But it must allso
69      * check if the listener has constraint properties set. if so it must use
70      * them to decide if the event should be invoked on this listener.
71      *
72      * @param event
73      * @param listener
74      * @throws ClassCastException
75      */

76     protected abstract void notifyEventListener(Event event, EventListener listener) throws ClassCastException JavaDoc;
77
78     public boolean addListener(EventListener listener) {
79         if (canBrokerForListener(listener)) {
80             if (! listeners.add(listener)) {
81                 if (log.isDebugEnabled()) {
82                     log.debug("" + listener + " was already in " + getClass() + ". Ignored.");
83                 }
84                 return false;
85             } else if (log.isDebugEnabled()) {
86                 log.debug("listener added to " + getClass());
87             }
88             return true;
89         } else {
90             log.warn("Ignored listener for" + getClass() + " because it cannot broker for that.");
91         }
92         return false;
93     }
94
95     public void removeListener(EventListener listener) {
96         if (! listeners.remove(listener)) {
97             log.warn("Tried to remove " + listener + " from " + getClass()+ " but it was not found. Ignored.");
98         }
99
100     }
101
102     public void notifyForEvent(Event event) {
103         if(log.isDebugEnabled())log.debug("will notify " + listeners.size() + " listeners");
104         for (Iterator i = listeners.iterator(); i.hasNext();) {
105             EventListener listener = (EventListener) i.next();
106             try {
107                 notifyEventListener(event, listener);
108             } catch (ClassCastException JavaDoc e) {
109                 // (this should never happen)
110
log.error("could not notify listener " + listener + " of event " + event);
111             }
112         }
113     }
114
115     public String JavaDoc toString(){
116         return "Abstract Event Broker";
117     }
118
119     public boolean equals(Object JavaDoc o) {
120         // we can only have one instance so this will do to prevent adding more instances of an envent broker
121
return this.getClass().getName().equals(o.getClass().getName());
122     }
123
124     public int hashCode() {
125         int result = 0;
126         result = HashCodeUtil.hashCode(result, toString());
127         result = HashCodeUtil.hashCode(result, this.getClass().getName());
128         return result;
129     }
130 }
131
Popular Tags