01: package com.teamkonzept.webman.mainint.events;
02:
03: import java.util.*;
04:
05: import com.teamkonzept.web.*;
06: import com.teamkonzept.lib.*;
07: import org.apache.log4j.Category;
08:
09: /**
10: Defaultimplementierung eines EventVerteilers, nur isHandler ist noch
11: abstrakt, cached die Events !, d.h. es gibt nur EINEN Handler pro Event
12: */
13: public abstract class CachedEventDistributor extends
14: DefaultEventDistributor implements ErrorCodes {
15: private Hashtable cache = new Hashtable();
16:
17: /** for logging */
18: static Category cat = Category
19: .getInstance(CachedEventDistributor.class);
20:
21: public void handleEvent(TKEvent evt) throws TKException {
22: cat.debug("Got Event: " + evt);
23: TKEventHandler handler = (TKEventHandler) cache.get(evt
24: .getName());
25: if (handler == null) {
26: for (int i = 0; i < handlerList.size(); i++) {
27: handler = (TKEventHandler) handlerList.elementAt(i);
28: if (handler.isHandler(evt)) {
29: cache.put(evt.getName(), handler);
30: handler.handleEvent(evt);
31: return;
32: }
33: }
34: throw new TKException("Invalid Event !" + evt.getName(),
35: INVALID_EVENT, NORMAL_SEVERITY, false, null);
36: }
37: handler.handleEvent(evt);
38: }
39:
40: /**
41: überladen, da der Cache geändert werden muss
42: */
43: public void removeEventHandler(TKEventHandler handler) {
44: // aus Cache entfernen <-> value, nicht key muss entfernt werden !
45: Enumeration keys = cache.keys();
46: while (keys.hasMoreElements()) {
47: Object obj = keys.nextElement();
48: if (cache.get(obj) == handler)
49: cache.remove(obj);
50: }
51: super.removeEventHandler(handler);
52: }
53:
54: }
|