1 package org.lsst.ccs.utilities.dispatch;
2
3
4 import java.util.concurrent.CopyOnWriteArrayList;
5 import java.util.logging.Level;
6
7 /**
8 * Registers codes interested in receiving commands.
9 * Each code should know that it is handling an instance of <TT>SynchronousCommandFor</TT>
10 * and should behave accordingly.
11 * <P>
12 * The <TT>dispatchCommand</TT> methods execute this command sequentially on each
13 * "<I>executant</I>" object. The order of calls is not specified.
14 *
15 * @author bernard AMADE
16 */
17 public class SynchronousCommandDispatcher<T> {
18 protected CopyOnWriteArrayList<T> list = new java.util.concurrent.CopyOnWriteArrayList() ;
19
20 /**
21 * Registers a code ready to receive "synchronous" commands
22 * @param executant
23 */
24 public void addExecutant(T executant) {
25 list.add(executant) ;
26 }
27
28 /**
29 * Remove the code from the listeners list
30 *
31 * @param executant
32 */
33 public void removeExecutant(T executant){
34 list.remove(executant) ;
35 }
36
37 /**
38 * Sends a command to all registered codes ready to handle it.
39 * <P>
40 * If an exception is fired it is catched and logged.
41 * proper handling of Exception should be performed by the implementing commands.
42 * </P>
43 */
44 public void dispatchCommand(SynchronousCommandFor<? super T> command) {
45 for(T executant : list) {
46 try{
47 command.invokeOn(executant);
48 } catch (Exception exc) {
49 PackCst.CURLOG.log(Level.SEVERE, "while dispatching :", exc);
50 }
51 }
52 }
53 }