01: package dalma.impl;
02:
03: import dalma.EngineListener;
04: import dalma.Conversation;
05:
06: import java.util.Set;
07: import java.util.HashSet;
08: import java.util.Collections;
09: import java.util.logging.Logger;
10: import java.util.logging.Level;
11:
12: /**
13: * Set of {@link EngineListener}.
14: *
15: * Invoking the {@link EngineListener} methods on this object will fire the same event
16: * to all listeners.
17: *
18: * @author Kohsuke Kawaguchi
19: */
20: final class EngineListenerSet extends EngineListener {
21: private final Set<EngineListener> listeners = Collections
22: .synchronizedSet(new HashSet<EngineListener>());
23:
24: private static final Logger logger = Logger
25: .getLogger(EngineListenerSet.class.getName());
26:
27: public void add(EngineListener l) {
28: listeners.add(l);
29: }
30:
31: public void remove(EngineListener l) {
32: if (!listeners.remove(l))
33: throw new IllegalArgumentException();
34: }
35:
36: private EngineListener[] getListeners() {
37: return listeners.toArray(new EngineListener[listeners.size()]);
38: }
39:
40: private void error(Throwable e) {
41: logger
42: .log(Level.WARNING, "EngineListener reported an error",
43: e);
44: }
45:
46: public void onConversationStarted(Conversation conv) {
47: for (EngineListener l : getListeners()) {
48: try {
49: l.onConversationStarted(conv);
50: } catch (Throwable e) {
51: error(e);
52: }
53: }
54: }
55:
56: public synchronized void onConversationCompleted(Conversation conv) {
57: for (EngineListener l : getListeners()) {
58: try {
59: l.onConversationCompleted(conv);
60: } catch (Exception e) {
61: error(e);
62: }
63: }
64: }
65: }
|