public abstract class AbstractCommand extends Object implements Command
Command objects that may be executed by a command processor.| Constructor and Description |
|---|
AbstractCommand()
Constructor for
AbstractCommand that defines just the type without any parameters. |
AbstractCommand(Command commandToTransform)
Constructor for
AbstractCommand that acts as a pseudo-copy constructor and a command
decorator/transformer. |
AbstractCommand(Map<String,Object> commandParameters)
Constructor for
AbstractCommand that allows the caller to define both the name and parameter default
values. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
allowAnyParameter()
Returns
true if this command does not utilize
parameter definitions to restrict what parameters it can accept. |
protected abstract CommandType |
buildCommandType()
Builds the command type of this command.
|
protected abstract ParameterDefinition[] |
buildParameterDefinitions()
Builds the set of parameter definitions that this command will use.
|
void |
checkParameterValidity(boolean convertIfNecessary)
This method verifies the validity of the parameters.
|
void |
convertParameters()
This method will attempt to convert all parameters whose types did not match that of its corresponding parameter
definition.
|
CommandType |
getCommandType()
Returns an object that identifies the type of command.
|
Properties |
getConfiguration()
Returns a map of name/value pairs that can be used to configure this particular instance of the command.
|
ParameterDefinition |
getParameterDefinition(String paramName)
Returns the definition of the named parameter.
|
ParameterDefinition[] |
getParameterDefinitions()
Returns the set of parameter definitions for all allowable parameters this command accepts.
|
Object |
getParameterValue(String paramName)
Returns the value of a parameter that is to be passed to the command.
|
Map<String,Object> |
getParameterValues()
Returns the values of all parameters that are to be passed to the command in a
Map, with the keys in
the map being the parameter names. |
protected Map |
getParameterValuesInternalMap()
Returns a
Map containing all optional parameters set on this command. |
boolean |
hasParameterValue(String paramName)
Checks to see if the given parameter has explicitly been given a value (
null or otherwise). |
protected void |
initializeMetadata()
Initializes the newly constructed command so its type and parameter definition metadata are built properly.
|
boolean |
isCommandInResponse()
If
true is returned, then this Command that was executed will be included in the returned
response. |
void |
removeParameterValue(String paramName)
Removes a parameter from this command.
|
void |
removeParameterValues()
Removes all parameters from this command.
|
void |
setCommandInResponse(boolean flag)
Sets a flag to determine if this
Command object is to be returned with the response after the command has
executed. |
void |
setParameterValue(String paramName,
Object paramValue)
Adds a parameter value under the given parameter name.
|
protected void |
setParameterValuesInternalMap(Map<String,Object> commandParameters)
Sets the parameter map, overwriting any previously existing map of parameters.
|
String |
toString()
Returns the string representation of this command that includes the parameter values but not the parameter
definitions.
|
String |
toString(boolean includeParameters,
boolean includeParameterDefs)
Returns a toString representation of the command but provides the caller the option to include or omit the
parameters values and/or the parameter definitions.
|
public AbstractCommand()
throws IllegalArgumentException,
InvalidParameterDefinitionException
AbstractCommand that defines just the type without any parameters.IllegalArgumentException - if commandType was not defined by the command subclassInvalidParameterDefinitionException - if more than one parameter is defined with the same namebuildCommandType(),
buildParameterDefinitions(),
AbstractCommand(Map)public AbstractCommand(Map<String,Object> commandParameters) throws IllegalArgumentException, InvalidParameterDefinitionException
AbstractCommand that allows the caller to define both the name and parameter default
values.commandParameters - optional set of parameters to be passed with the command (may be empty or
null)IllegalArgumentException - if commandType was not defined by the command subclassInvalidParameterDefinitionException - if more than one parameter is defined with the same namebuildCommandType(),
buildParameterDefinitions()public AbstractCommand(Command commandToTransform) throws InvalidParameterValueException
AbstractCommand that acts as a pseudo-copy constructor and a command
decorator/transformer. The given command's parameter values will be copied to this new command object. However,
the command type and the parameter definitions will be those defined by the subclass'
buildParameterDefinitions(), not those of the given command.
So, as you can see, this isn't truely a follower of the Decorator pattern or a copy-constructor; instead,
think of this constructor as a transformer from one command implementation to another, while maintaining the
command's original parameter values (as well as the flag).
This is typically used when the given command is a generic command (one with no parameter definitions for example) and the caller wants to convert it to a more concrete command implementation. This is usually due to the fact that the creator of the given command object did not know at compile time the specific concrete command type it needed.
Transforming a command allows the caller to "decorate" the given command with a concrete command implementation's API (which typically has more strongly typed methods to extract out command parameters).
If the given commandToTransform
allowed any and all parameters but the newly transformed command (this
object) does not, this method forces the current set of parameters to be validated (after possibly being
converted to the appropriate types). If the parameters do not validate, an exception will be thrown.
The only thing a subclass must do in order to support this transformer constructor is to override it and call
it via super.
commandToTransform - the command object to transform into this class typeInvalidParameterValueException - if the original command's parameters are not valid for this commandpublic CommandType getCommandType()
CommandgetCommandType in interface Commandnull)Command.getCommandType()public boolean isCommandInResponse()
Commandtrue is returned, then this Command that was executed will be included in the returned
response. This is useful if executed asynchronously and the command requestor will need to know the command and
the parameters that were issued. If false, then the response returned by the server after the
command has executed will be unavailable (i.e. null).isCommandInResponse in interface Commandtrue if this object will be returned in the response to the command executionCommand.isCommandInResponse()public void setCommandInResponse(boolean flag)
CommandCommand object is to be returned with the response after the command has
executed. Using this will affect performance as it requires not only for the actual response data to be
(un)marshalled, but also the Command must be (un)marshalled and set over the network as well. Use this if
the command was executed asynchronously and the original Command object is no longer available on the
client.setCommandInResponse in interface Commandflag - true to get this object returned with the command response.Command.setCommandInResponse(boolean)public boolean allowAnyParameter()
Commandtrue if this command does not utilize
parameter definitions to restrict what parameters it can accept.
true means this command will accept any and all parameter values and types. false means that
this command restricts the kinds of parameters it will accept. The definitions of the allowable parameters are
determined by a call to Command.getParameterDefinitions().allowAnyParameter in interface Commandtrue if there are no restrictions to the number, types and names of parameters accepted by
the command; false means all parameters must pass validity checks based on parameter
definitions found in Command.getParameterDefinitions()Command.allowAnyParameter()public ParameterDefinition getParameterDefinition(String paramName) throws IllegalArgumentException, NoParameterDefinitionsException
Commandnull
is returned. If a command does not define parameter definitions (e.g. Command.allowAnyParameter() returns
true}), an exception is thrown.getParameterDefinition in interface CommandparamName - the name of the parameter whose definition is to be returned (must not be null)null if the command does not accept a parameter with
the given nameIllegalArgumentException - if paramName is nullNoParameterDefinitionsException - if the command has not defined any parameter definitionsCommand.getParameterDefinition(String)public ParameterDefinition[] getParameterDefinitions() throws NoParameterDefinitionsException
CommandWhether a command accepts any and all parameters can also be determined by calling
Command.allowAnyParameter().
Command implementations may choose to make the order of the returned array significant.
getParameterDefinitions in interface Commandnull)NoParameterDefinitionsException - if no parameter definitions have been defined by the command; this
indicates the command will accept any and all parametersCommand.getParameterDefinitions()public boolean hasParameterValue(String paramName) throws IllegalArgumentException
Commandnull or otherwise).hasParameterValue in interface CommandparamName - the name of the parameter whose existence is to be checkedtrue if a parameter named paramName has a value explicitly defined in this
command; false if there is no parameter of that name definedIllegalArgumentException - if paramName is nullCommand.hasParameterValue(String)public Map<String,Object> getParameterValues() throws IllegalArgumentException
CommandMap, with the keys in
the map being the parameter names.
Note that the returned Map is a copy - changes to the Map will not reflect back into
the command. However, each individual value object is the original reference; it is recommended that the caller
not modify the underlying value objects found in the returned map; instead,
Command.setParameterValue(String, Object) should be used.
getParameterValues in interface Commandnull but may be empty)IllegalArgumentExceptionCommand.getParameterValues()public Object getParameterValue(String paramName)
CommandgetParameterValue in interface CommandparamName - the name of the parameter whose value is to be returned (must not be null)null)Command.getParameterValue(String)public void setParameterValue(String paramName, Object paramValue) throws IllegalArgumentException
CommandparamValue. Note that any parameter name or value may be added
with this method - validity checking is not performed. To ensure a command's parameters are valid, call
Command.checkParameterValidity(boolean).
Note that implementations should not rely on clients using this method - instead, implementations of this
interface should provide additional, more strongly typed, setter methods. This provides two things: first, it
does not ask the client to know the actual parameter name and second it provides stronger type checking (as
opposed to being able to just pass in an Object as this method allows).
setParameterValue in interface CommandparamName - the name of the new parameterparamValue - the value of the new parameterIllegalArgumentException - if paramName is nullCommand.setParameterValue(String, Object)public void removeParameterValue(String paramName)
CommandCommand.checkParameterValidity(boolean).
Also see Command.setParameterValue(String, Object) for the recommendation of defining strongly-typed methods
in implementation classes.
removeParameterValue in interface CommandparamName - a key to identify the parameter to remove (may be null)Command.removeParameterValue(String)public void removeParameterValues()
CommandCommand.checkParameterValidity(boolean).removeParameterValues in interface CommandCommand.removeParameterValues()public String toString()
toString(boolean, boolean) if you want the string to include the definitions or omit
the parameter values.toString in class ObjectObject.toString()public String toString(boolean includeParameters, boolean includeParameterDefs)
Parameter definitions are noisy and can make the string very verbose.
includeParameters - if true, show the parameter values in the returned stringincludeParameterDefs - if true, show the parameter definitions in the returned stringObject.toString()public void checkParameterValidity(boolean convertIfNecessary)
throws InvalidParameterValueException
CommandIf convertIfNecessary is true, this method will attempt to convert a parameter whose
value did not match that of its parameter definition. This conversion is not guaranteed to be successful; if the
conversion fails, the original parameter value remains intact and the validity check fails. If the conversion
succeeds, the newly converted parameter value replaces the invalid value.
If all parameters are deemed valid and the command can be invoked, this method does nothing.
checkParameterValidity in interface CommandconvertIfNecessary - if true, then attempt to convert any invalid parameter valuesInvalidParameterValueException - if one or more parameters are invalidCommand.checkParameterValidity(boolean)public void convertParameters()
CommandconvertParameters in interface CommandCommand.convertParameters()public Properties getConfiguration()
CommandgetConfiguration in interface CommandCommand.getConfiguration()protected Map getParameterValuesInternalMap()
Map containing all optional parameters set on this command. This may return
null in which case no parameters will be sent along with the command when it is to be executed.
Note: the returned Map is not a copy - changes made to the returned object are reflected back
into this command object.
null)protected void setParameterValuesInternalMap(Map<String,Object> commandParameters)
protected - users of commands should not be allowed to insert a new map of parameters; rather, subclasses
should provide setters for individual parameter values.commandParameters - the new map of parameters (may be null)protected void initializeMetadata()
throws IllegalArgumentException,
InvalidParameterDefinitionException
This method is called from the constructors. However, it is protected to allow for specialized command implementations to be able to re-initialize the command metadata.
IllegalArgumentException - if the command type was not specified by the command subclassInvalidParameterDefinitionException - if more than one parameter is defined with the same namebuildCommandType(),
buildParameterDefinitions(),
AbstractCommand(Map)protected abstract CommandType buildCommandType()
null.
This method is called by the command's constructors.
nullprotected abstract ParameterDefinition[] buildParameterDefinitions()
null if the command accepts any and all parameters, regardless of name or typeThis method is called by the command's constructors.
nullCopyright © 2008-2013 Red Hat, Inc.. All Rights Reserved.