1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package nl.toolforge.karma.core.cmd;
20
21 import nl.toolforge.karma.core.cmd.event.CommandFinishedEvent;
22 import nl.toolforge.karma.core.cmd.event.CommandResponseEvent;
23 import nl.toolforge.karma.core.cmd.event.CommandResponseListener;
24 import nl.toolforge.karma.core.cmd.event.CommandStartedEvent;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32 /***
33 * A <code>CommandResponse</code> object is used to dispatch {@link CommandResponseEvent} events to listeners that are
34 * interested in those events. These objects can be fed with <code>n</code> {@link CommandResponseListener}s.
35 *
36 * @author W.M. Oosterom
37 * @author D.A. Smedes
38 * @author W.H. Schraal
39 * @version $Id: CommandResponse.java,v 1.28 2004/11/10 23:53:08 asmedes Exp $
40 */
41 public class CommandResponse {
42
43 private static Log logger = LogFactory.getLog(CommandResponse.class);
44
45 private List listeners = new ArrayList();
46
47 /***
48 * Constructs a command response object.
49 */
50 public CommandResponse() {}
51
52 /***
53 * Dispatches the event to all {@link CommandResponseListener}s. If no listener has been registered, a warning will
54 * be written to the log system.
55 *
56 * @param event The event that should be dispatched.
57 */
58 public synchronized void addEvent(CommandResponseEvent event) {
59 if (listeners.size() > 0) {
60 for (Iterator it = listeners.iterator(); it.hasNext(); ) {
61 CommandResponseListener listener = (CommandResponseListener) it.next();
62
63 if (event instanceof CommandStartedEvent) {
64 listener.commandStarted(event);
65 } else if (event instanceof CommandFinishedEvent) {
66 listener.commandFinished(event);
67 } else {
68 listener.messageLogged(event);
69 }
70
71 }
72 } else {
73 logger.warn("No listener registered for command response (messages sent to /dev/null ...)");
74 }
75 }
76
77 /***
78 * Adds a {@link CommandResponseListener} that is interested in events added to this <code>CommandResponse</code>.
79 *
80 * @param responseListener
81 */
82 public final synchronized void addCommandResponseListener(CommandResponseListener responseListener) {
83 listeners.add(responseListener);
84 }
85
86 /***
87 * Removes <code>responseListener</code> from this <code>CommandResponse</code>.
88 */
89 public final synchronized void removeCommandReponseListener(CommandResponseListener responseListener) {
90 listeners.remove(responseListener);
91 }
92
93 }