KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > busimpl > BusEventProcessor


1 package org.objectweb.celtix.bus.busimpl;
2
3 import java.util.*;
4 import java.util.logging.Level JavaDoc;
5 import java.util.logging.Logger JavaDoc;
6
7 import org.objectweb.celtix.Bus;
8 import org.objectweb.celtix.BusEvent;
9 import org.objectweb.celtix.BusEventCache;
10 import org.objectweb.celtix.BusEventFilter;
11 import org.objectweb.celtix.BusEventListener;
12 import org.objectweb.celtix.BusException;
13 import org.objectweb.celtix.common.i18n.Message;
14 import org.objectweb.celtix.common.logging.LogUtils;
15
16
17 public class BusEventProcessor {
18     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(BusEventProcessor.class);
19     protected Bus theBus;
20     protected List<BusEventListenerInfo> listenerList;
21     protected BusEventCache cache;
22     
23
24     public BusEventProcessor(Bus bus, BusEventCache eventCache) {
25         theBus = bus;
26         listenerList = new ArrayList<BusEventListenerInfo>();
27         cache = eventCache;
28     }
29
30     public void addListener(BusEventListener l, BusEventFilter filter) throws BusException {
31         if (l == null) {
32             throw new BusException(new Message("Listener can't be null", LOG));
33         }
34
35         synchronized (listenerList) {
36             listenerList.add(new BusEventListenerInfo(l, filter));
37         }
38     }
39
40
41     public void removeListener(BusEventListener l) throws BusException {
42         boolean found = false;
43         BusEventListenerInfo li;
44         synchronized (listenerList) {
45             for (Iterator<BusEventListenerInfo> i = listenerList.iterator(); i.hasNext();) {
46                 li = i.next();
47                 if (li.listener == l) {
48                     i.remove();
49                     found = true;
50                 }
51             }
52         }
53
54         if (!found) {
55             throw new BusException(
56                       new Message("Error while removing listener. Specified listener is not found.",
57                                   LOG));
58         }
59     }
60
61
62     public void processEvent(BusEvent e) {
63         if (e == null) {
64             return;
65         }
66
67         BusEventListenerInfo li;
68         boolean eventProcessed = false;
69
70         synchronized (listenerList) {
71             for (int i = 0; i < listenerList.size(); i++) {
72                 li = (BusEventListenerInfo)listenerList.get(i);
73
74                 if ((li.filter == null) || (li.filter.isEventEnabled(e))) {
75                     eventProcessed = true;
76
77                     try {
78                         li.listener.processEvent(e);
79                     } catch (BusException ex) {
80                         //NOTE now just log the exception and not throw the exception to bus
81
LOG.log(Level.WARNING, "PROCESS_EVENT_FAILURE_MSG",
82                                 new Object JavaDoc[] {li.getClass().getName(), e.getID(), ex});
83                     }
84                 }
85             }
86         }
87
88         if (!eventProcessed) {
89             cache.addEvent(e);
90         }
91     }
92
93     class BusEventListenerInfo {
94         BusEventListener listener;
95         BusEventFilter filter;
96
97         public BusEventListenerInfo(BusEventListener l, BusEventFilter f) {
98             listener = l;
99             filter = f;
100         }
101     }
102 }
103
Popular Tags