| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| nl.toolforge.karma.core.cmd.DefaultCommand |
|
|
| 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.boot.WorkingContext; |
|
| 22 | import nl.toolforge.karma.core.bundle.BundleCache; |
|
| 23 | import nl.toolforge.karma.core.cmd.event.CommandResponseListener; |
|
| 24 | import org.apache.commons.cli.CommandLine; |
|
| 25 | import org.apache.commons.logging.Log; |
|
| 26 | import org.apache.commons.logging.LogFactory; |
|
| 27 | ||
| 28 | import java.util.ResourceBundle; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * Default stuff for a command. Provides the datastructure and some helper methods to implementing commands. |
|
| 32 | * |
|
| 33 | * @author D.A. Smedes |
|
| 34 | * @version $Id: DefaultCommand.java,v 1.25 2004/11/10 23:53:08 asmedes Exp $ |
|
| 35 | */ |
|
| 36 | public abstract class DefaultCommand implements Command { |
|
| 37 | ||
| 38 | 0 | private static final Log logger = LogFactory.getLog(DefaultCommand.class); |
| 39 | ||
| 40 | 0 | private CommandContext contextRef = null; |
| 41 | ||
| 42 | 0 | private CommandLine commandLine = null; |
| 43 | 0 | private String name = null; |
| 44 | 0 | private String alias = null; |
| 45 | 0 | private String description = null; |
| 46 | 0 | private String helpText = null; |
| 47 | ||
| 48 | 0 | private CommandResponseListener responseListener = null; |
| 49 | ||
| 50 | /** |
|
| 51 | * Creates a command by initializing the command through its <code>CommandDescriptor</code>. |
|
| 52 | * |
|
| 53 | * @param descriptor The command descriptor instance containing the basic information for this command |
|
| 54 | */ |
|
| 55 | 0 | public DefaultCommand(CommandDescriptor descriptor) { |
| 56 | ||
| 57 | 0 | if (descriptor == null) { |
| 58 | 0 | throw new IllegalArgumentException("Command descriptor cannot be null."); |
| 59 | } |
|
| 60 | 0 | name = descriptor.getName(); |
| 61 | 0 | alias = descriptor.getAlias(); |
| 62 | 0 | description = descriptor.getDescription(); |
| 63 | 0 | helpText = descriptor.getHelp(); |
| 64 | 0 | } |
| 65 | ||
| 66 | /** |
|
| 67 | * Sets the command context for this command. The command needs the command context during |
|
| 68 | * the executing phase. |
|
| 69 | * |
|
| 70 | * @param contextRef The <code>CommandContext</code> for this command. |
|
| 71 | */ |
|
| 72 | public final void setContext(CommandContext contextRef) { |
|
| 73 | 0 | if (this.contextRef != null) { |
| 74 | 0 | throw new IllegalStateException("context is already set"); |
| 75 | } |
|
| 76 | 0 | this.contextRef = contextRef; |
| 77 | 0 | } |
| 78 | ||
| 79 | /** |
|
| 80 | * Gets a command's name. |
|
| 81 | * |
|
| 82 | * @return A command's name as a <code>String</code>. |
|
| 83 | */ |
|
| 84 | public final String getName() { |
|
| 85 | 0 | return name; |
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * Gets a command's alias; the shortcut name for the command. This alias could be a comma-separated String of |
|
| 90 | * aliasses, all of which are valid aliasses. |
|
| 91 | * |
|
| 92 | * @return A command's alias as a <code>String</code>. |
|
| 93 | */ |
|
| 94 | public final String getAlias() { |
|
| 95 | 0 | return alias; |
| 96 | } |
|
| 97 | ||
| 98 | /** |
|
| 99 | * Helper to get the current <code>WorkingContext</code>. |
|
| 100 | * |
|
| 101 | * @return The current <code>WorkingContext</code>. |
|
| 102 | */ |
|
| 103 | public final WorkingContext getWorkingContext() { |
|
| 104 | 0 | return getContext().getWorkingContext(); |
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Gets a localized version of a command's description. |
|
| 109 | * |
|
| 110 | * @return A command's description as a <code>String</code>. |
|
| 111 | */ |
|
| 112 | public final String getDescription() { |
|
| 113 | 0 | return description; |
| 114 | } |
|
| 115 | ||
| 116 | public final void setCommandLine(CommandLine commandLine) { |
|
| 117 | 0 | this.commandLine = commandLine; |
| 118 | 0 | } |
| 119 | ||
| 120 | /** |
|
| 121 | * Gets the parsed command line for this command. This command line can be queried by commands to check if options |
|
| 122 | * had been set, or to retrieve application data. |
|
| 123 | * |
|
| 124 | * @return A command line instance. |
|
| 125 | */ |
|
| 126 | public final CommandLine getCommandLine() { |
|
| 127 | 0 | return commandLine; |
| 128 | } |
|
| 129 | ||
| 130 | public final void registerCommandResponseListener(CommandResponseListener responseListener) { |
|
| 131 | 0 | this.responseListener = responseListener; |
| 132 | 0 | CommandResponse commandResponse = getCommandResponse(); |
| 133 | 0 | if (commandResponse != null) { |
| 134 | 0 | commandResponse.addCommandResponseListener(responseListener); |
| 135 | } else { |
|
| 136 | 0 | logger.error("getCommandResponse() returned 'null' for command '"+getName()+"'."); |
| 137 | } |
|
| 138 | 0 | } |
| 139 | ||
| 140 | public final void deregisterCommandResponseListener(CommandResponseListener responseListener) { |
|
| 141 | 0 | CommandResponse commandResponse = getCommandResponse(); |
| 142 | 0 | if (commandResponse != null) { |
| 143 | 0 | getCommandResponse().removeCommandReponseListener(responseListener); |
| 144 | } else { |
|
| 145 | 0 | logger.error("getCommandResponse() returned 'null' for command '"+getName()+"'."); |
| 146 | } |
|
| 147 | 0 | } |
| 148 | ||
| 149 | /** |
|
| 150 | * Gets the response listener object for this command. |
|
| 151 | * |
|
| 152 | * @return The response listener object for this command. |
|
| 153 | */ |
|
| 154 | public final CommandResponseListener getResponseListener() { |
|
| 155 | 0 | return responseListener; |
| 156 | } |
|
| 157 | ||
| 158 | /** |
|
| 159 | * Accessor method for the commands' {@link CommandContext}. |
|
| 160 | * |
|
| 161 | * @return The commands' command context. |
|
| 162 | */ |
|
| 163 | public final CommandContext getContext() { |
|
| 164 | 0 | return contextRef; |
| 165 | } |
|
| 166 | ||
| 167 | /** |
|
| 168 | * A commands help text. Can be overridden for commands that have not provided xml data for the |
|
| 169 | * <code><help></code>-element. |
|
| 170 | * |
|
| 171 | * @return Help text for this command. |
|
| 172 | */ |
|
| 173 | public String getHelp() { |
|
| 174 | 0 | return helpText; |
| 175 | } |
|
| 176 | ||
| 177 | /** |
|
| 178 | * Helper method to get a resource bundle for frontend messages for commands. |
|
| 179 | * |
|
| 180 | * @return The <code>ResourceBundle</code> for the current locale for frontend messages. |
|
| 181 | */ |
|
| 182 | protected final ResourceBundle getFrontendMessages() { |
|
| 183 | 0 | return BundleCache.getInstance().getBundle(BundleCache.FRONTEND_MESSAGES_KEY); |
| 184 | } |
|
| 185 | ||
| 186 | /** |
|
| 187 | * Override to clean up stuff. |
|
| 188 | */ |
|
| 189 | public void cleanUp() { |
|
| 190 | // Nothing |
|
| 191 | 0 | } |
| 192 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |