1
2
3
4
5 package org.abraracourcix.alipes.pipes;
6
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Iterator;
10
11 import org.abraracourcix.alipes.common.Event;
12 import org.abraracourcix.alipes.listeners.Listener;
13 import org.abraracourcix.alipes.listeners.ListenerException;
14 import org.abraracourcix.alipes.monitors.Monitor;
15
16 /***
17 * Multiplexes an event to several listeners
18 * @author jdt
19 */
20 public class Multiplexor implements Monitor, Listener {
21 /***
22 * Collection of Listeners
23 */
24 private final Collection listeners = new ArrayList();
25 /***
26 * Add a new one
27 * @param listener Listener to add
28 */
29 public final void addListener(final Listener listener) {
30 listeners.add(listener);
31 }
32
33 /***
34 * something happened - pass the word on
35 * @param event the event
36 * @param resource happened to this resouce
37 * @throws MultiplexorException if one or more of the underlying listeners throws an exception, but processing continues across all the listeners even it does
38 * @see org.abraracourcix.alipes.listeners.Listener#eventOccurred(org.abraracourcix.alipes.common.Event, java.lang.Object)
39 */
40 public final void eventOccurred(final Event event, final Object resource) throws MultiplexorException {
41 final Iterator it = listeners.iterator();
42 MultiplexorException exception=null;
43 while (it.hasNext()) {
44 final Listener listener = (Listener) it.next();
45 try {
46 listener.eventOccurred(event, resource);
47 } catch (ListenerException e) {
48 if (exception==null) {
49 exception=new MultiplexorException("One or more of my listeners has thrown an exception",e);
50 } else {
51 exception.addCause(e);
52 }
53 }
54 }
55 if (exception!=null) {
56 throw exception;
57 }
58 }
59
60 /***
61 * Clear the list of listeners
62 */
63 public void clear() {
64 listeners.clear();
65 }
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95