| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| nl.toolforge.karma.core.cmd.CommandResponse |
|
|
| 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 | 48 | public class CommandResponse { |
| 42 | 66 | |
| 43 | 186 | private static Log logger = LogFactory.getLog(CommandResponse.class); |
| 44 | 198 | |
| 45 | 126 | private List listeners = new ArrayList(); |
| 46 | ||
| 47 | /** |
|
| 48 | 288 | * Constructs a command response object. |
| 49 | 396 | */ |
| 50 | 252 | 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 | 0 | if (listeners.size() > 0) { |
| 60 | 0 | for (Iterator it = listeners.iterator(); it.hasNext(); ) { |
| 61 | 0 | CommandResponseListener listener = (CommandResponseListener) it.next(); |
| 62 | ||
| 63 | 0 | if (event instanceof CommandStartedEvent) { |
| 64 | 0 | listener.commandStarted(event); |
| 65 | 0 | } else if (event instanceof CommandFinishedEvent) { |
| 66 | 0 | listener.commandFinished(event); |
| 67 | } else { |
|
| 68 | 0 | listener.messageLogged(event); |
| 69 | } |
|
| 70 | ||
| 71 | } |
|
| 72 | } else { |
|
| 73 | 0 | logger.warn("No listener registered for command response (messages sent to /dev/null ...)"); |
| 74 | } |
|
| 75 | 0 | } |
| 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 | 0 | listeners.add(responseListener); |
| 84 | 0 | } |
| 85 | ||
| 86 | /** |
|
| 87 | * Removes <code>responseListener</code> from this <code>CommandResponse</code>. |
|
| 88 | */ |
|
| 89 | public final synchronized void removeCommandReponseListener(CommandResponseListener responseListener) { |
|
| 90 | 0 | listeners.remove(responseListener); |
| 91 | 0 | } |
| 92 | ||
| 93 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |