Class CommandContextImpl

    • Method Detail

      • addShutdownHook

        protected void addShutdownHook()
      • getExitCode

        public int getExitCode()
        Description copied from interface: CommandContext
        Returns value that should be used as the exit code of the JVM process.
        Specified by:
        getExitCode in interface CommandContext
        Returns:
        JVM exit code
      • isTerminated

        public boolean isTerminated()
        Description copied from interface: CommandContext
        Checks whether the session has been terminated.
        Specified by:
        isTerminated in interface CommandContext
        Returns:
      • handle

        public void handle​(String line)
                    throws CommandLineException
        Description copied from interface: CommandContext
        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.
        Specified by:
        handle in interface CommandContext
        Parameters:
        line - command or operation to handle
        Throws:
        CommandFormatException - in case there was an error handling the command or operation
        CommandLineException
      • execute

        public org.jboss.dmr.ModelNode execute​(org.jboss.dmr.ModelNode mn,
                                               String description)
                                        throws CommandLineException,
                                               IOException
        Description copied from interface: CommandContext
        Execute an operation. This call is guarded by the command timeout.
        Specified by:
        execute in interface CommandContext
        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.
      • handleSafe

        public void handleSafe​(String line)
        Description copied from interface: CommandContext
        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.
        Specified by:
        handleSafe in interface CommandContext
        Parameters:
        line - command or operation to handle
      • getArgumentsString

        public String getArgumentsString()
        Description copied from interface: CommandContext
        Returns the current command's arguments as a string.
        Specified by:
        getArgumentsString in interface CommandContext
        Returns:
        current command's arguments as a string or null if the command was entered w/o arguments.
      • terminateSession

        public void terminateSession()
        Description copied from interface: CommandContext
        Terminates the command line session. Also closes the connection to the controller if it's still open.
        Specified by:
        terminateSession in interface CommandContext
      • print

        public void print​(String message,
                          boolean newLine,
                          boolean collect)
      • printDMR

        public void printDMR​(org.jboss.dmr.ModelNode node,
                             boolean compact)
        Description copied from interface: CommandContext
        Prints a ModelNode according to the current configuration.
        Specified by:
        printDMR in interface CommandContext
        Parameters:
        node - The ModelNode to print.
        compact - true for content displayed onto a single line.
      • isColorOutput

        public final boolean isColorOutput()
      • printLine

        public void printLine​(String message)
        Description copied from interface: CommandContext
        Prints a string to the CLI's output. Terminates the message by writing the line separator string.
        Specified by:
        printLine in interface CommandContext
        Parameters:
        message - the message to print
      • printLine

        public void printLine​(String message,
                              boolean collect)
        Description copied from interface: CommandContext
        Prints a string to the CLI's output. Terminates the message by writing the line separator string.
        Specified by:
        printLine in interface CommandContext
        Parameters:
        message - the message to print
        collect - Whether the message to print will be collected
      • printLine

        public void printLine​(String message,
                              String outcome)
      • print

        public void print​(String message)
        Description copied from interface: CommandContext
        Prints a string to the CLI's output.
        Specified by:
        print in interface CommandContext
        Parameters:
        message - the message to print
      • error

        protected void error​(String message)
        Set the exit code of the process to indicate an error and output the error message. WARNING This method should only be called for unrecoverable errors as once the exit code is set subsequent operations may not be possible.
        Parameters:
        message - The message to display.
      • printColumns

        public void printColumns​(Collection<String> col)
        Description copied from interface: CommandContext
        Prints a collection of strings as columns to the CLI's output.
        Specified by:
        printColumns in interface CommandContext
        Parameters:
        col - the collection of strings to print as columns.
      • set

        public void set​(CommandContext.Scope scope,
                        String key,
                        Object value)
        Description copied from interface: CommandContext
        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.
        Specified by:
        set in interface CommandContext
        Parameters:
        scope - Entry duration scope
        key - the key
        value - the value to be associated with the key
      • get

        public Object get​(CommandContext.Scope scope,
                          String key)
        Description copied from interface: CommandContext
        Returns the value the key was associated with using the set(key, value) method above.
        Specified by:
        get in interface CommandContext
        Parameters:
        scope - Entry duration scope
        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.
      • remove

        public Object remove​(CommandContext.Scope scope,
                             String key)
        Description copied from interface: CommandContext
        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.
        Specified by:
        remove in interface CommandContext
        Parameters:
        scope - The entry duration scope
        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.
      • connectController

        public void connectController()
                               throws CommandLineException
        Description copied from interface: CommandContext
        Connects 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.
        Specified by:
        connectController in interface CommandContext
        Throws:
        CommandLineException - in case the attempt to connect failed
      • connectController

        public void connectController​(String controller)
                               throws CommandLineException
        Description copied from interface: CommandContext
        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.
        Specified by:
        connectController in interface CommandContext
        Parameters:
        controller - the controller to connect to
        Throws:
        CommandLineException - in case the attempt to connect failed
      • connectController

        public void connectController​(String controller,
                                      String clientAddress)
                               throws CommandLineException
        Description copied from interface: CommandContext
        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.
        Specified by:
        connectController in interface CommandContext
        Parameters:
        controller - the controller to connect to
        clientAddress - the address the client will bind to
        Throws:
        CommandLineException - in case the attempt to connect failed
      • connectController

        @Deprecated
        public void connectController​(String host,
                                      int port)
                               throws CommandLineException
        Deprecated.
        Description copied from interface: CommandContext
        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.
        Specified by:
        connectController in interface CommandContext
        Parameters:
        host - the host to connect with
        port - the port to connect on
        Throws:
        CommandLineException - in case the attempt to connect failed
      • getCurrentDir

        public File getCurrentDir()
        Description copied from interface: CommandContext
        Returns current default filesystem directory.
        Specified by:
        getCurrentDir in interface CommandContext
        Returns:
        current default filesystem directory.
      • setCurrentDir

        public void setCurrentDir​(File dir)
        Description copied from interface: CommandContext
        Changes the current default filesystem directory to the argument.
        Specified by:
        setCurrentDir in interface CommandContext
        Parameters:
        dir - the new default directory
      • disconnectController

        public void disconnectController()
        Description copied from interface: CommandContext
        Closes the previously established connection with the controller client. If the connection hasn't been established, the method silently returns.
        Specified by:
        disconnectController in interface CommandContext
      • getDefaultControllerHost

        @Deprecated
        public String getDefaultControllerHost()
        Deprecated.
        Description copied from interface: CommandContext
        Returns the default host the controller client will be connected to.
        Specified by:
        getDefaultControllerHost in interface CommandContext
        Returns:
        the default host the controller client will be connected to.
      • getDefaultControllerPort

        @Deprecated
        public int getDefaultControllerPort()
        Deprecated.
        Description copied from interface: CommandContext
        Returns the default port the controller client will be connected to.
        Specified by:
        getDefaultControllerPort in interface CommandContext
        Returns:
        the default port the controller client will be connected to.
      • getControllerHost

        public String getControllerHost()
        Description copied from interface: CommandContext
        Returns the host the controller client is connected to or null if the connection hasn't been established yet.
        Specified by:
        getControllerHost in interface CommandContext
        Returns:
        the host the controller client is connected to or null if the connection hasn't been established yet.
      • getControllerPort

        public int getControllerPort()
        Description copied from interface: CommandContext
        Returns the port the controller client is connected to.
        Specified by:
        getControllerPort in interface CommandContext
        Returns:
        the port the controller client is connected to.
      • getHistory

        public CommandHistory getHistory()
        Description copied from interface: CommandContext
        Returns the history of all the commands and operations.
        Specified by:
        getHistory in interface CommandContext
        Returns:
        the history of all the commands and operations.
      • isBatchMode

        public boolean isBatchMode()
        Description copied from interface: CommandContext
        Checks whether the CLI is in the batch mode.
        Specified by:
        isBatchMode in interface CommandContext
        Returns:
        true if the CLI is in the batch mode, false - otherwise.
      • isWorkflowMode

        public boolean isWorkflowMode()
        Description copied from interface: CommandContext
        Checks whether the CLI is in a workflow mode.
        Specified by:
        isWorkflowMode in interface CommandContext
        Returns:
        true if the CLI is in a workflow mode, false - otherwise.
      • toBatchedCommand

        public BatchedCommand toBatchedCommand​(String line)
                                        throws CommandFormatException
        Description copied from interface: CommandContext
        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.
        Specified by:
        toBatchedCommand in interface CommandContext
        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

        public org.jboss.dmr.ModelNode buildRequest​(String line)
                                             throws CommandFormatException
        Description copied from interface: CommandContext
        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.
        Specified by:
        buildRequest in interface CommandContext
        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
      • isDomainMode

        public boolean isDomainMode()
        Description copied from interface: CommandContext
        Indicates whether the CLI is in the domain mode or standalone one (assuming established connection to the controller).
        Specified by:
        isDomainMode in interface CommandContext
        Returns:
        true if the CLI is connected to the domain controller, otherwise false.
      • notifyListeners

        protected void notifyListeners​(CliEvent event)
      • interact

        public void interact()
        Description copied from interface: CommandContext
        This method will start an interactive session. It requires an initialized at the construction time console.
        Specified by:
        interact in interface CommandContext
      • isResolveParameterValues

        public boolean isResolveParameterValues()
        Description copied from interface: CommandContext
        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.
        Specified by:
        isResolveParameterValues in interface CommandContext
        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

        public void setResolveParameterValues​(boolean resolve)
        Description copied from interface: CommandContext
        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.
        Specified by:
        setResolveParameterValues in interface CommandContext
        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

        public boolean isSilent()
        Description copied from interface: CommandContext
        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.
        Specified by:
        isSilent in interface CommandContext
        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

        public void setSilent​(boolean silent)
        Description copied from interface: CommandContext
        Enables of disables the silent mode.
        Specified by:
        setSilent in interface CommandContext
        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

        public int getTerminalWidth()
        Description copied from interface: CommandContext
        Returns the current terminal window width in case the console has been initialized. Otherwise -1.
        Specified by:
        getTerminalWidth in interface CommandContext
        Returns:
        current terminal with if the console has been initialized, -1 otherwise
      • getTerminalHeight

        public int getTerminalHeight()
        Description copied from interface: CommandContext
        Returns the current terminal window height in case the console has been initialized. Otherwise -1.
        Specified by:
        getTerminalHeight in interface CommandContext
        Returns:
        current terminal height if the console has been initialized, -1 otherwise
      • setVariable

        public void setVariable​(String name,
                                String value)
                         throws CommandLineException
        Description copied from interface: CommandContext
        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.
        Specified by:
        setVariable in interface CommandContext
        Parameters:
        name - name of the variable
        value - value for the variable
        Throws:
        CommandLineException - in case the name contains illegal characters
      • getVariable

        public String getVariable​(String name)
        Description copied from interface: CommandContext
        Returns the value for the variable. If the variable has not been defined the method will return null.
        Specified by:
        getVariable in interface CommandContext
        Parameters:
        name - name of the variable
        Returns:
        the value of the variable or null if the variable has not been defined
      • getVariables

        public Collection<String> getVariables()
        Description copied from interface: CommandContext
        Returns a collection of all the defined variable names. If there no variables defined, an empty collection will be returned.
        Specified by:
        getVariables in interface CommandContext
        Returns:
        collection of all the defined variable names or an empty collection if no variables has been defined
      • getConnectionInfo

        public ConnectionInfo getConnectionInfo()
        Description copied from interface: CommandContext
        The ConnectionInfo bean is set after the connection is established to the server.
        Specified by:
        getConnectionInfo in interface CommandContext
        Returns:
        information about the current connection to the server.
      • captureOutput

        public void captureOutput​(PrintStream captor)
        Description copied from interface: CommandContext
        Redirect output to the given print stream.
        Specified by:
        captureOutput in interface CommandContext
        Parameters:
        captor - stream to which output should be written. Cannot be null
      • setCommandTimeout

        public final void setCommandTimeout​(int numSeconds)
        Description copied from interface: CommandContext
        Set the command timeout to a number of seconds. Value equals to 0 means no timeout.
        Specified by:
        setCommandTimeout in interface CommandContext
        Parameters:
        numSeconds - The timeout value.
      • getCommandTimeout

        public final int getCommandTimeout()
        Description copied from interface: CommandContext
        Returns the command execution timeout value. Value equals to 0 means no timeout.
        Specified by:
        getCommandTimeout in interface CommandContext
        Returns:
        The timeout value.
      • getConsole

        public ReadlineConsole getConsole()
        Public for testing purpose only.
        Returns:
      • newTimeoutCommandContext

        public CommandContext newTimeoutCommandContext()