Package org.jboss.as.cli
Interface CommandContext
-
- All Known Implementing Classes:
CommandContextImpl,CommandExecutor.TimeoutCommandContext
public interface CommandContext- Author:
- Alexey Loubyansky
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classCommandContext.ScopeScope for entries added to context.static classCommandContext.TIMEOUT_RESET_VALUEThe command timeout reset value.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description voidaddEventListener(CliEventListener listener)Adds a listener for CLI events.voidbindClient(ModelControllerClient newClient)Bind the controller to an existing, connected client.org.jboss.dmr.ModelNodebuildRequest(String line)Builds a DMR request corresponding to the command or the operation.voidcaptureOutput(PrintStream captor)Redirect output to the given print stream.voidclear(CommandContext.Scope scope)Clear the content of a scope.voidclearScreen()Clears the screen.voidconnectController()Connects the controller client using the default controller definition.voidconnectController(String controller)Connects to the controller specified.voidconnectController(String host, int port)Deprecated.UseconnectController(String)instead.voidconnectController(String controller, String clientAddress)Connects to the controller specified.voiddisconnectController()Closes the previously established connection with the controller client.org.jboss.dmr.ModelNodeexecute(Operation op, String description)Execute an operation.org.jboss.dmr.ModelNodeexecute(org.jboss.dmr.ModelNode mn, String description)Execute an operation.default Objectget(String key)Deprecated.Useget(Scope, String)instead.Objectget(CommandContext.Scope scope, String key)Returns the value the key was associated with using the set(key, value) method above.StringgetArgumentsString()Returns the current command's arguments as a string.BatchManagergetBatchManager()Returns batch manager.CommandLineParsergetCommandLineParser()Returns the current operation request parser.intgetCommandTimeout()Returns the command execution timeout value.CliConfiggetConfig()Returns the JBoss CLI configuration.ConnectionInfogetConnectionInfo()The ConnectionInfo bean is set after the connection is established to the server.StringgetControllerHost()Returns the host the controller client is connected to or null if the connection hasn't been established yet.intgetControllerPort()Returns the port the controller client is connected to.FilegetCurrentDir()Returns current default filesystem directory.OperationRequestAddressgetCurrentNodePath()Returns the current prefix.CommandLineCompletergetDefaultCommandCompleter()Returns the default command line completer.ControllerAddressgetDefaultControllerAddress()The default address of the default controller to connect to.StringgetDefaultControllerHost()Deprecated.UsegetDefaultControllerAddress()instead.intgetDefaultControllerPort()Deprecated.UsegetDefaultControllerAddress()instead.intgetExitCode()Returns value that should be used as the exit code of the JVM process.CommandHistorygetHistory()Returns the history of all the commands and operations.ModelControllerClientgetModelControllerClient()Returns the model controller client or null if it hasn't been initialized.NodePathFormattergetNodePathFormatter()Returns the prefix formatter.OperationCandidatesProvidergetOperationCandidatesProvider()Returns the provider of operation request candidates for tab-completion.ParsedCommandLinegetParsedCommandLine()Parsed command line arguments.intgetTerminalHeight()Returns the current terminal window height in case the console has been initialized.intgetTerminalWidth()Returns the current terminal window width in case the console has been initialized.StringgetVariable(String name)Returns the value for the variable.Collection<String>getVariables()Returns a collection of all the defined variable names.voidhandle(String line)Executes a command or an operation.voidhandleSafe(String line)Executes a command or an operation.voidinteract()This method will start an interactive session.booleanisBatchMode()Checks whether the CLI is in the batch mode.booleanisDomainMode()Indicates whether the CLI is in the domain mode or standalone one (assuming established connection to the controller).booleanisResolveParameterValues()Command argument or operation parameter values may contain system properties.booleanisSilent()Whether the info or error messages should be written to the terminal output.booleanisTerminated()Checks whether the session has been terminated.booleanisWorkflowMode()Checks whether the CLI is in a workflow mode.default voidprint(String message)Prints a string to the CLI's output.voidprintColumns(Collection<String> col)Prints a collection of strings as columns to the CLI's output.default voidprintDMR(org.jboss.dmr.ModelNode node)Prints a ModelNode according to the current configuration.default voidprintDMR(org.jboss.dmr.ModelNode node, boolean compact)Prints a ModelNode according to the current configuration.voidprintLine(String message)Prints a string to the CLI's output.default voidprintLine(String message, boolean collect)Prints a string to the CLI's output.voidregisterRedirection(CommandLineRedirection redirection)After this method returns command line handling will be redirected to the passed in CommandLineRedirection instance.voidreleaseOutput()Stops redirecting output to the stream passed tocaptureOutput(java.io.PrintStream).default Objectremove(String key)Deprecated.Useremove(Scope, String)instead.Objectremove(CommandContext.Scope scope, String key)Removes the value the key was associated with using the set(key, value) method above.voidresetTimeout(CommandContext.TIMEOUT_RESET_VALUE value)Reset the timeout value.default voidset(String key, Object value)Deprecated.Useset(Scope, String, Object)instead.voidset(CommandContext.Scope scope, String key, Object value)Associates an object with key.voidsetCommandTimeout(int numSeconds)Set the command timeout to a number of seconds.voidsetCurrentDir(File dir)Changes the current default filesystem directory to the argument.voidsetResolveParameterValues(boolean resolve)Command argument or operation parameter values may contain system properties.voidsetSilent(boolean silent)Enables of disables the silent mode.voidsetVariable(String name, String value)Initializes a variable with the given name with the given value.voidterminateSession()Terminates the command line session.BatchedCommandtoBatchedCommand(String line)Builds an operation request from the passed in command line.
-
-
-
Method Detail
-
getConfig
CliConfig getConfig()
Returns the JBoss CLI configuration.- Returns:
- CLI configuration
-
getArgumentsString
String getArgumentsString()
Returns the current command's arguments as a string.- Returns:
- current command's arguments as a string or null if the command was entered w/o arguments.
-
getParsedCommandLine
ParsedCommandLine getParsedCommandLine()
Parsed command line arguments.- Returns:
- parsed command line arguments.
-
printLine
void printLine(String message)
Prints a string to the CLI's output. Terminates the message by writing the line separator string.- Parameters:
message- the message to print
-
printLine
default void printLine(String message, boolean collect)
Prints a string to the CLI's output. Terminates the message by writing the line separator string.- Parameters:
message- the message to printcollect- Whether the message to print will be collected
-
printDMR
default void printDMR(org.jboss.dmr.ModelNode node)
Prints a ModelNode according to the current configuration.- Parameters:
node- The ModelNode to print.
-
printDMR
default void printDMR(org.jboss.dmr.ModelNode node, boolean compact)Prints a ModelNode according to the current configuration.- Parameters:
node- The ModelNode to print.compact- true for content displayed onto a single line.
-
print
default void print(String message)
Prints a string to the CLI's output.- Parameters:
message- the message to print
-
printColumns
void printColumns(Collection<String> col)
Prints a collection of strings as columns to the CLI's output.- Parameters:
col- the collection of strings to print as columns.
-
clearScreen
void clearScreen()
Clears the screen.
-
terminateSession
void terminateSession()
Terminates the command line session. Also closes the connection to the controller if it's still open.
-
isTerminated
boolean isTerminated()
Checks whether the session has been terminated.- Returns:
-
set
void set(CommandContext.Scope scope, String key, Object value)
Associates an object with key. The mapping is valid until this method is called with the same key value and null as the new value for this key.- Parameters:
scope- Entry duration scopekey- the keyvalue- the value to be associated with the key
-
set
default void set(String key, Object value)
Deprecated.Useset(Scope, String, Object)instead.Associates an object with key. The mapping is valid until this method is called with the same key value and null as the new value for this key.- Parameters:
key- the keyvalue- the value to be associated with the key
-
get
Object get(CommandContext.Scope scope, String key)
Returns the value the key was associated with using the set(key, value) method above.- Parameters:
scope- Entry duration scopekey- the key to fetch the value for- Returns:
- the value associated with the key or null, if the key wasn't associated with any non-null value.
-
get
default Object get(String key)
Deprecated.Useget(Scope, String)instead.Returns the value the key was associated with using the set(key, value) method above.- Parameters:
key- the key to fetch the value for- Returns:
- the value associated with the key or null, if the key wasn't associated with any non-null value.
-
clear
void clear(CommandContext.Scope scope)
Clear the content of a scope.- Parameters:
scope- The scope to clear, can't be null.
-
remove
Object remove(CommandContext.Scope scope, String key)
Removes the value the key was associated with using the set(key, value) method above. If the key isn't associated with any value, the method will return null.- Parameters:
scope- The entry duration scopekey- the key to be removed- Returns:
- the value associated with the key or null, if the key wasn't associated with any non-null value.
-
remove
default Object remove(String key)
Deprecated.Useremove(Scope, String)instead.Removes the value the key was associated with using the set(key, value) method above. If the key isn't associated with any value, the method will return null.- Parameters:
key- the key to be removed- Returns:
- the value associated with the key or null, if the key wasn't associated with any non-null value.
-
getModelControllerClient
ModelControllerClient getModelControllerClient()
Returns the model controller client or null if it hasn't been initialized.- Returns:
- the model controller client or null if it hasn't been initialized.
-
connectController
void connectController() throws CommandLineExceptionConnects the controller client using the default controller definition. The default controller will be identified as the default specified on starting the CLI will be used, if no controller was specified on start up then the default defined in the CLI configuration will be used, if no default is defined then a connection to remote+http://localhost:9990 will be used instead.- Throws:
CommandLineException- in case the attempt to connect failed
-
connectController
void connectController(String controller) throws CommandLineException
Connects to the controller specified. If the controller is null then the default specified on starting the CLI will be used, if no controller was specified on start up then the default defined in the CLI configuration will be used, if no default is defined then a connection to remote+http://localhost:9990 will be used instead.- Parameters:
controller- the controller to connect to- Throws:
CommandLineException- in case the attempt to connect failed
-
connectController
void connectController(String controller, String clientAddress) throws CommandLineException
Connects to the controller specified. If the controller is null then the default specified on starting the CLI will be used, if no controller was specified on start up then the default defined in the CLI configuration will be used, if no default is defined then a connection to remote+http://localhost:9990 will be used instead.- Parameters:
controller- the controller to connect toclientAddress- the address the client will bind to- Throws:
CommandLineException- in case the attempt to connect failed
-
connectController
@Deprecated void connectController(String host, int port) throws CommandLineException
Deprecated.UseconnectController(String)instead.Connects the controller client using the host and the port. If the host is null, the default controller host will be used, which is localhost. If the port is less than zero, the default controller port will be used, which is 9999.- Parameters:
host- the host to connect withport- the port to connect on- Throws:
CommandLineException- in case the attempt to connect failed
-
bindClient
void bindClient(ModelControllerClient newClient)
Bind the controller to an existing, connected client.
-
disconnectController
void disconnectController()
Closes the previously established connection with the controller client. If the connection hasn't been established, the method silently returns.
-
getDefaultControllerHost
@Deprecated String getDefaultControllerHost()
Deprecated.UsegetDefaultControllerAddress()instead.Returns the default host the controller client will be connected to.- Returns:
- the default host the controller client will be connected to.
-
getDefaultControllerPort
@Deprecated int getDefaultControllerPort()
Deprecated.UsegetDefaultControllerAddress()instead.Returns the default port the controller client will be connected to.- Returns:
- the default port the controller client will be connected to.
-
getDefaultControllerAddress
ControllerAddress getDefaultControllerAddress()
The default address of the default controller to connect to.- Returns:
- The default address.
-
getControllerHost
String getControllerHost()
Returns the host the controller client is connected to or null if the connection hasn't been established yet.- Returns:
- the host the controller client is connected to or null if the connection hasn't been established yet.
-
getControllerPort
int getControllerPort()
Returns the port the controller client is connected to.- Returns:
- the port the controller client is connected to.
-
getCommandLineParser
CommandLineParser getCommandLineParser()
Returns the current operation request parser.- Returns:
- current operation request parser.
-
getCurrentNodePath
OperationRequestAddress getCurrentNodePath()
Returns the current prefix.- Returns:
- current prefix
-
getNodePathFormatter
NodePathFormatter getNodePathFormatter()
Returns the prefix formatter.- Returns:
- the prefix formatter.
-
getOperationCandidatesProvider
OperationCandidatesProvider getOperationCandidatesProvider()
Returns the provider of operation request candidates for tab-completion.- Returns:
- provider of operation request candidates for tab-completion.
-
getHistory
CommandHistory getHistory()
Returns the history of all the commands and operations.- Returns:
- the history of all the commands and operations.
-
isBatchMode
boolean isBatchMode()
Checks whether the CLI is in the batch mode.- Returns:
- true if the CLI is in the batch mode, false - otherwise.
-
isWorkflowMode
boolean isWorkflowMode()
Checks whether the CLI is in a workflow mode.- Returns:
- true if the CLI is in a workflow mode, false - otherwise.
-
getBatchManager
BatchManager getBatchManager()
Returns batch manager.- Returns:
- batch manager
-
toBatchedCommand
BatchedCommand toBatchedCommand(String line) throws CommandFormatException
Builds an operation request from the passed in command line. If the line contains a command, the command must supported the batch mode, otherwise an exception will thrown.- Parameters:
line- the command line which can be an operation request or a command that can be translated into an operation request.- Returns:
- the operation request
- Throws:
CommandFormatException- if the operation request couldn't be built.
-
buildRequest
org.jboss.dmr.ModelNode buildRequest(String line) throws CommandFormatException
Builds a DMR request corresponding to the command or the operation. If the line contains a command, the corresponding command handler must implement org.jboss.cli.OperationCommand interface, in other words the command must translate into an operation request, otherwise an exception will be thrown.- Parameters:
line- command or an operation to build a DMR request for- Returns:
- DMR request corresponding to the line
- Throws:
CommandFormatException- thrown in case the line couldn't be translated into a DMR request
-
getDefaultCommandCompleter
CommandLineCompleter getDefaultCommandCompleter()
Returns the default command line completer.- Returns:
- the default command line completer.
-
isDomainMode
boolean isDomainMode()
Indicates whether the CLI is in the domain mode or standalone one (assuming established connection to the controller).- Returns:
- true if the CLI is connected to the domain controller, otherwise false.
-
addEventListener
void addEventListener(CliEventListener listener)
Adds a listener for CLI events.- Parameters:
listener- the listener
-
getExitCode
int getExitCode()
Returns value that should be used as the exit code of the JVM process.- Returns:
- JVM exit code
-
handle
void handle(String line) throws CommandLineException
Executes a command or an operation. Or, if the context is in the batch mode and the command is allowed in the batch, adds the command (or the operation) to the currently active batch. NOTE: errors are not handled by this method, they won't affect the exit code or even be logged. Error handling is the responsibility of the caller.- Parameters:
line- command or operation to handle- Throws:
CommandFormatException- in case there was an error handling the command or operationCommandLineException
-
handleSafe
void handleSafe(String line)
Executes a command or an operation. Or, if the context is in the batch mode and the command is allowed in the batch, adds the command (or the operation) to the currently active batch. NOTE: unlike handle(String line), this method catches CommandLineException exceptions thrown by command handlers, logs them and sets the exit code status to indicate that the command or the operation has failed. It's up to the caller to check the exit code with getExitCode() to find out whether the command or the operation succeeded or failed.- Parameters:
line- command or operation to handle- Throws:
CommandFormatException- in case there was an error handling the command or operation
-
interact
void interact()
This method will start an interactive session. It requires an initialized at the construction time console.
-
getCurrentDir
File getCurrentDir()
Returns current default filesystem directory.- Returns:
- current default filesystem directory.
-
setCurrentDir
void setCurrentDir(File dir)
Changes the current default filesystem directory to the argument.- Parameters:
dir- the new default directory
-
isResolveParameterValues
boolean isResolveParameterValues()
Command argument or operation parameter values may contain system properties. If this method returns true then the CLI will try to resolve the system properties before sending the operation request to the controller. Otherwise, the resolution will happen on the server side.- Returns:
- true if system properties in the operation parameter values should be resolved by the CLI before the request is sent to the controller, false if system properties should be resolved on the server side.
-
setResolveParameterValues
void setResolveParameterValues(boolean resolve)
Command argument or operation parameter values may contain system properties. If this property is set to true then the CLI will try to resolve the system properties before sending the operation request to the controller. Otherwise, the resolution will happen on the server side.- Parameters:
resolve- true if system properties in the operation parameter values should be resolved by the CLI before the request is sent to the controller, false if system properties should be resolved on the server side.
-
isSilent
boolean isSilent()
Whether the info or error messages should be written to the terminal output. The output of the info and error messages is done in the following way: 1) the message is always logged using a logger (which is disabled in the config by default); 2) if the output target was specified on the command line using '>' it would be used; 3) if the output target was not specified, whether the message is written or not to the terminal output will depend on whether it's a silent mode or not.- Returns:
- true if the CLI is in the silent mode, i.e. not writing info and error messages to the terminal output, otherwise - false.
-
setSilent
void setSilent(boolean silent)
Enables of disables the silent mode.- Parameters:
silent- true if the CLI should go into the silent mode, false if the CLI should resume writing info and error messages to the terminal output.
-
getTerminalWidth
int getTerminalWidth()
Returns the current terminal window width in case the console has been initialized. Otherwise -1.- Returns:
- current terminal with if the console has been initialized, -1 otherwise
-
getTerminalHeight
int getTerminalHeight()
Returns the current terminal window height in case the console has been initialized. Otherwise -1.- Returns:
- current terminal height if the console has been initialized, -1 otherwise
-
setVariable
void setVariable(String name, String value) throws CommandLineException
Initializes a variable with the given name with the given value. The name of the variable must follow the rules for Java identifiers but not contain '$' character. If the variable already exists, its value will be silently overridden. Passing in null as the value will remove the variable altogether. If the variable with the given name has not been defined and the value passed in is null, the method will return silently.- Parameters:
name- name of the variablevalue- value for the variable- Throws:
CommandLineException- in case the name contains illegal characters
-
getVariable
String getVariable(String name)
Returns the value for the variable. If the variable has not been defined the method will return null.- Parameters:
name- name of the variable- Returns:
- the value of the variable or null if the variable has not been defined
-
getVariables
Collection<String> getVariables()
Returns a collection of all the defined variable names. If there no variables defined, an empty collection will be returned.- Returns:
- collection of all the defined variable names or an empty collection if no variables has been defined
-
registerRedirection
void registerRedirection(CommandLineRedirection redirection) throws CommandLineException
After this method returns command line handling will be redirected to the passed in CommandLineRedirection instance.- Parameters:
redirection- command line redirection handler- Throws:
CommandLineException- in case registration fails (e.g. in case one has already been registered
-
getConnectionInfo
ConnectionInfo getConnectionInfo()
The ConnectionInfo bean is set after the connection is established to the server.- Returns:
- information about the current connection to the server.
-
captureOutput
void captureOutput(PrintStream captor)
Redirect output to the given print stream.- Parameters:
captor- stream to which output should be written. Cannot benull- Throws:
IllegalStateException- if output is already being captured
-
releaseOutput
void releaseOutput()
Stops redirecting output to the stream passed tocaptureOutput(java.io.PrintStream).- Throws:
IllegalStateException- if output isn't currently being captured
-
setCommandTimeout
void setCommandTimeout(int numSeconds)
Set the command timeout to a number of seconds. Value equals to 0 means no timeout.- Parameters:
numSeconds- The timeout value.
-
getCommandTimeout
int getCommandTimeout()
Returns the command execution timeout value. Value equals to 0 means no timeout.- Returns:
- The timeout value.
-
resetTimeout
void resetTimeout(CommandContext.TIMEOUT_RESET_VALUE value)
Reset the timeout value.- Parameters:
value- The enumerated timeout reset value.
-
execute
org.jboss.dmr.ModelNode execute(org.jboss.dmr.ModelNode mn, String description) throws CommandLineException, IOExceptionExecute an operation. This call is guarded by the command timeout.- Parameters:
mn- The operation.description- Operation description, used in exception message.- Returns:
- The response.
- Throws:
CommandLineException- If an exception occurs.IOException- If an IOException occurs.
-
execute
org.jboss.dmr.ModelNode execute(Operation op, String description) throws CommandLineException, IOException
Execute an operation. This call is guarded by the command timeout.- Parameters:
op- The operation.description- Operation description, used in exception message.- Returns:
- The response.
- Throws:
CommandLineException- If an exception occurs.IOException- If an IOException occurs.
-
-