View Javadoc

1   /* $Id: Multiplexor.java,v 1.2 2004/05/21 23:29:45 johndavidtaylor Exp $
2   * Created on Feb 20, 2004 by John Taylor jdt@roe.ac.uk .
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          } //end while
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  *$Log: Multiplexor.java,v $
71  *Revision 1.2  2004/05/21 23:29:45  johndavidtaylor
72  *Added a clear() method
73  *
74  *Revision 1.1  2004/03/17 21:18:40  johndavidtaylor
75  *Copied across from incubation in beanpeeler
76  *
77  *Revision 1.5  2004/03/14 18:49:15  johndavidtaylor
78  *corrected comments
79  *
80  *Revision 1.4  2004/03/14 00:44:22  johndavidtaylor
81  *Added ListenerException
82  *
83  *Revision 1.3  2004/03/11 15:46:35  johndavidtaylor
84  *Applied coding standards
85  *
86  *Revision 1.2  2004/03/08 20:16:45  johndavidtaylor
87  *refactored to new package name
88  *
89  *Revision 1.1  2004/03/08 19:58:25  johndavidtaylor
90  *Initial commit -transfer  from previous repository
91  *
92  *Revision 1.1  2004/03/02 23:04:37  jdt
93  *new
94  *
95  */