org.opennms.web.notification.bobject
Class Command

java.lang.Object
  extended by org.opennms.web.notification.bobject.Command

public class Command
extends Object

This is a class to store and execute a console command

Version:
1.1.1.1
Author:
Jason Johns , OpenNMS

Constructor Summary
Command()
          Default constructor, intializes the members
 
Method Summary
 void addArgument(Argument anArgument)
          Adds a argument to the list of arguments
 void addType(String aType)
          Adds an identifier type for command
 Command copy()
          Creates a new Command objects with identical values to the current Command object.
static String[] createCommandArray(String s, char delim)
          Convenience method for creating arrays of strings suitable for use as command-line parameters when executing an external process.
 int execute()
          This method executes the command using a Process.
 List getArguments()
          Returns the list of arguments
 List<String> getArgumentSwitches()
          This method gets the list of switches that Notify knows about that will map to a given console command.
 String getCommandComments()
          Returns the comments for the command
 String getCommandName()
          Returns the command name
 String getType()
          Returns the first type in the list as a string
 boolean hasSwitch(String aSwitch)
          This method determines if this command has a given switch as a member of its arguments
 boolean isOfType(String aType)
          Returns the notification type of the command
 void setArgumentValue(String aSwitch, String aValue)
          This method sets an argument specified by the switch param to the given value param
 void setCommandComments(String someComments)
          Sets the comments for the command
 void setCommandName(String aName)
          Sets the command name
 void setUseStream(boolean aBool)
          This method sets the boolean that indicates if this command requires an input stream.
 String toString()
          Returns a String representation of the command as it would look to execute on the console.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Command

public Command()
Default constructor, intializes the members

Method Detail

copy

public Command copy()
Creates a new Command objects with identical values to the current Command object.

Returns:
a copy of this command

setCommandName

public void setCommandName(String aName)
Sets the command name

Parameters:
aName - the name of the command

getCommandName

public String getCommandName()
Returns the command name

Returns:
the name of the command

addType

public void addType(String aType)
Adds an identifier type for command

Parameters:
aType - the notification type

isOfType

public boolean isOfType(String aType)
Returns the notification type of the command

Returns:
true if contains the type

getType

public String getType()
Returns the first type in the list as a string

Returns:
the type

setCommandComments

public void setCommandComments(String someComments)
Sets the comments for the command

Parameters:
someComments - the comments for the command

getCommandComments

public String getCommandComments()
Returns the comments for the command

Returns:
the comments for the command

addArgument

public void addArgument(Argument anArgument)
Adds a argument to the list of arguments

Parameters:
anArgument - the argument to be added.

setUseStream

public void setUseStream(boolean aBool)
This method sets the boolean that indicates if this command requires an input stream.

Parameters:
aBool - true if a stream should be used, false otherwise

getArguments

public List getArguments()
Returns the list of arguments

Returns:
the list of arguments

setArgumentValue

public void setArgumentValue(String aSwitch,
                             String aValue)
This method sets an argument specified by the switch param to the given value param

Parameters:
aSwitch - the argument to set
aValue - the value to set

hasSwitch

public boolean hasSwitch(String aSwitch)
This method determines if this command has a given switch as a member of its arguments

Parameters:
aSwitch - the switch to check for
Returns:
true if the command has the switch, false otherwise

getArgumentSwitches

public List<String> getArgumentSwitches()
This method gets the list of switches that Notify knows about that will map to a given console command.

Returns:
a list of parameter switches

execute

public int execute()
This method executes the command using a Process. The method will decide if an input stream needs to be used.

Returns:
the return code of the command

toString

public String toString()
Returns a String representation of the command as it would look to execute on the console.

Overrides:
toString in class Object
Returns:
a string representation

createCommandArray

public static String[] createCommandArray(String s,
                                          char delim)
Convenience method for creating arrays of strings suitable for use as command-line parameters when executing an external process.

The default Runtime.exec(java.lang.String[])method will split a single string based on spaces, but it does not respect spaces within quotation marks, and it will leave the quotation marks in the resulting substrings. This method solves those problems by replacing all in-quote spaces with the given delimiter, removes the quotes, and then splits the resulting string by the remaining out-of-quote spaces. It then goes through each substring and replaces the delimiters with spaces.

Caveat: This method does not respect escaped quotes! It will simply remove them and leave the stray escape characters.

Parameters:
s - the string to split
delim - a char that does not already exist in s
Returns:
An array of strings split by spaces outside of quotes.
Throws:
IllegalArgumentException - If s is null or if delim already exists in s.


Copyright © 2009. All Rights Reserved.