View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  }