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.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 private static final Log logger = LogFactory.getLog(DefaultCommand.class);
39
40 private CommandContext contextRef = null;
41
42 private CommandLine commandLine = null;
43 private String name = null;
44 private String alias = null;
45 private String description = null;
46 private String helpText = null;
47
48 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 public DefaultCommand(CommandDescriptor descriptor) {
56
57 if (descriptor == null) {
58 throw new IllegalArgumentException("Command descriptor cannot be null.");
59 }
60 name = descriptor.getName();
61 alias = descriptor.getAlias();
62 description = descriptor.getDescription();
63 helpText = descriptor.getHelp();
64 }
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 if (this.contextRef != null) {
74 throw new IllegalStateException("context is already set");
75 }
76 this.contextRef = contextRef;
77 }
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 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 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 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 return description;
114 }
115
116 public final void setCommandLine(CommandLine commandLine) {
117 this.commandLine = commandLine;
118 }
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 return commandLine;
128 }
129
130 public final void registerCommandResponseListener(CommandResponseListener responseListener) {
131 this.responseListener = responseListener;
132 CommandResponse commandResponse = getCommandResponse();
133 if (commandResponse != null) {
134 commandResponse.addCommandResponseListener(responseListener);
135 } else {
136 logger.error("getCommandResponse() returned 'null' for command '"+getName()+"'.");
137 }
138 }
139
140 public final void deregisterCommandResponseListener(CommandResponseListener responseListener) {
141 CommandResponse commandResponse = getCommandResponse();
142 if (commandResponse != null) {
143 getCommandResponse().removeCommandReponseListener(responseListener);
144 } else {
145 logger.error("getCommandResponse() returned 'null' for command '"+getName()+"'.");
146 }
147 }
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 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 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 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 return BundleCache.getInstance().getBundle(BundleCache.FRONTEND_MESSAGES_KEY);
184 }
185
186 /***
187 * Override to clean up stuff.
188 */
189 public void cleanUp() {
190
191 }
192 }