KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > events > EventHandlerRegistry


1 package za.org.coefficient.events;
2
3 import java.util.*;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.apache.log4j.Logger;
8
9
10 /**
11  * Class: EventHandlerRegistry
12  * Description: EventHandlerRegistry maintains a registry of event handlers. It is used to determine which
13  * handlers should receive event notifications.
14  * NOTE: This class is implemented using the singleton patttern, which doesn't really work too well in a
15  * distributed EJB environment. It is assumed, therefore, that the app is not running in a clustered
16  * environment - i.e. this is a singleton for the entire app. Alternative approaches include storing this stuff in the
17  * database, and reading from there, or using an external RMI interface via JNDI.
18  * @author tfogwill
19  */

20 public class EventHandlerRegistry {
21     
22     /**
23      * Logger
24      */

25     private static final Logger log = org.apache.log4j.Logger.getLogger(EventHandlerRegistry.class);
26     
27     /**
28      * Singleton instance
29      */

30     private static EventHandlerRegistry instance;
31     
32     /**
33      * Map to hold the registry data
34      */

35     private Map JavaDoc registry = new HashMap JavaDoc();
36     
37     /**
38      * Private constructor to prevent instantiation.
39      */

40     private EventHandlerRegistry(){}
41     
42     /**
43      * Get the singleton instance
44      * @return the EventHandlerRegistry instance
45      */

46     public static EventHandlerRegistry getInstance(){
47         if (instance == null) instance = new EventHandlerRegistry();
48         return instance;
49     }
50     
51     /**
52      * Get the handlers for the spercified event type
53      * @param eventType The event type
54      * @return the appropriate handlers
55      */

56     public synchronized CoefficientEventHandler[] getHandlers(String JavaDoc eventType){
57         try {
58             return getHandlers(Class.forName(eventType));
59         } catch (ClassNotFoundException JavaDoc e) {
60             e.printStackTrace();
61             return new CoefficientEventHandler[]{};
62         }
63     }
64     
65     /**
66      * Get the handlers for the spercified event type
67      * @param eventType The event type
68      * @return the appropriate handlers
69      */

70     public synchronized CoefficientEventHandler[] getHandlers(Class JavaDoc eventType){
71         log.info("REGISTRY: getting handlers for class " + eventType.getName());
72         Set handlers = new HashSet();
73         Object JavaDoc obj = registry.get(eventType);
74         if (obj != null && obj instanceof List) handlers.addAll((List)obj);
75         Class JavaDoc cls = eventType;
76         while (!Object JavaDoc.class.equals(cls)){
77             obj = registry.get(cls);
78             if (obj != null && obj instanceof List) handlers.addAll((List)obj);
79             cls = cls.getSuperclass();
80         }
81         log.info("REGISTRY: found " + handlers.size() + " handlers for class " + eventType.getName());
82         log.debug("REGISTRY: Handlers for " + eventType.getName() + ":");
83         for (Iterator iter = handlers.iterator(); iter.hasNext();) {
84             CoefficientEventHandler h = (CoefficientEventHandler) iter.next();
85             log.debug("\t" + h.getClass().getName() + " : " + h);
86         }
87         return (CoefficientEventHandler[]) handlers.toArray(new CoefficientEventHandler[handlers.size()]);
88     }
89     
90     /**
91      * Register a handler with the registry. This will cause it to be notified when an event occurs
92      * that matches the specified event type. Matching is done down the class hierarchy, so a handler will
93      * be notified if events occur that are subclasses of the type it's registered to handle.
94      * @param eventType The base event type(class) to receive notifications about
95      * @param handler The handler which will be notified.
96      */

97     public synchronized void registerEventHandler(Class JavaDoc eventType, CoefficientEventHandler handler){
98         if (eventType == null || handler == null) return;
99         List handlers = (List) registry.get(eventType);
100         if (handlers == null){
101             handlers = new ArrayList();
102             registry.put(eventType, handlers);
103         }
104         handlers.add(handler);
105     }
106
107 }
108
Popular Tags