Search in sources :

Example 1 with CLIExecution

use of org.jboss.as.cli.impl.aesh.AeshCommands.CLIExecution in project wildfly-core by wildfly.

the class CommandContextImpl method handleCommand.

private void handleCommand(ParsedCommandLine parsed) throws CommandFormatException, CommandLineException {
    String line = parsed.getOriginalLine();
    try {
        List<CLIExecution> executions = aeshCommands.newExecutions(parsed);
        for (CLIExecution exec : executions) {
            CLICommandInvocation invContext = exec.getInvocation();
            this.invocationContext = invContext;
            String opLine = exec.getLine();
            // implies a split of the main command)
            if (opLine != null && !opLine.equals(line)) {
                resetArgs(opLine);
            }
            Throwable originalException = null;
            try {
                // Could be an operation.
                if (exec.isOperation()) {
                    handleOperation(parsedCmd);
                    continue;
                }
                // Could be a legacy command.
                CommandHandler handler = exec.getLegacyHandler();
                if (handler != null) {
                    handleLegacyCommand(exec.getLine(), handler, false);
                    continue;
                }
            } catch (Throwable ex) {
                if (ex instanceof InterruptedException) {
                    Thread.currentThread().interrupt();
                }
                originalException = ex;
            } finally {
                // OK to be null
                Throwable suppressed = originalException;
                // when calling exec.execute something that we are not doing here.
                if (invContext.getConfiguration().getOutputRedirection() != null) {
                    try {
                        invContext.getConfiguration().getOutputRedirection().close();
                    } catch (IOException ex) {
                        // Message must contain the Exception and the localized message.
                        if (ex instanceof AccessDeniedException) {
                            String message = ex.getMessage();
                            suppressed = new CommandLineException((message != null ? message : line) + " (Access denied)");
                        } else {
                            suppressed = new CommandLineException(ex.toString());
                        }
                        if (originalException != null) {
                            originalException.addSuppressed(suppressed);
                            suppressed = originalException;
                        }
                    }
                }
                if (suppressed != null) {
                    if (suppressed instanceof RuntimeException) {
                        throw (RuntimeException) suppressed;
                    }
                    if (suppressed instanceof Error) {
                        throw (Error) suppressed;
                    }
                    if (suppressed instanceof CommandLineException) {
                        throw (CommandLineException) suppressed;
                    }
                    if (suppressed instanceof CommandLineParserException) {
                        throw (CommandLineParserException) suppressed;
                    }
                    if (suppressed instanceof OptionValidatorException) {
                        throw (OptionValidatorException) suppressed;
                    }
                }
            }
            // child command. This is caused by aesh 2.0 behavior.
            if (isBatchMode()) {
                exec.populateCommand();
            }
            BatchCompliantCommand bc = exec.getBatchCompliant();
            if (isBatchMode() && bc != null) {
                try {
                    Batch batch = getBatchManager().getActiveBatch();
                    BatchCompliantCommand.BatchResponseHandler request = bc.buildBatchResponseHandler(this, batch.getAttachments());
                    // Wrap into legacy API.
                    ResponseHandler rh = null;
                    if (request != null) {
                        rh = (ModelNode step, OperationResponse response) -> {
                            request.handleResponse(step, response);
                        };
                    }
                    BatchedCommand batchedCmd = new DefaultBatchedCommand(this, line, bc.buildRequest(this, batch.getAttachments()), rh);
                    batch.add(batchedCmd);
                } catch (CommandFormatException e) {
                    throw new CommandFormatException("Failed to add to batch '" + line + "'", e);
                }
            } else {
                execute(() -> {
                    executor.execute(aeshCommands.newExecutableBuilder(exec), timeout, TimeUnit.SECONDS);
                    return null;
                }, line);
            }
        }
    } catch (CommandNotFoundException ex) {
        // Commands that are not exposed in completion.
        if (parsedCmd.getFormat() != OperationFormat.INSTANCE) {
            CommandHandler h = cmdRegistry.getCommandHandler(ex.getCommandName().toLowerCase());
            if (h != null) {
                handleLegacyCommand(line, h, false);
                return;
            }
        }
        throw new CommandLineException("Unexpected command '" + line + "'. Type 'help --commands' for the list of supported commands.");
    } catch (IOException ex) {
        throw new CommandLineException(ex);
    } catch (CommandLineParserException | OptionValidatorException ex) {
        throw new CommandFormatException(ex);
    }
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) ResponseHandler(org.jboss.as.cli.handlers.ResponseHandler) CLICommandInvocation(org.wildfly.core.cli.command.aesh.CLICommandInvocation) CLIExecution(org.jboss.as.cli.impl.aesh.AeshCommands.CLIExecution) CommandHandler(org.jboss.as.cli.CommandHandler) CommandCommandHandler(org.jboss.as.cli.handlers.CommandCommandHandler) CommandLineException(org.jboss.as.cli.CommandLineException) Batch(org.jboss.as.cli.batch.Batch) DefaultBatchedCommand(org.jboss.as.cli.batch.impl.DefaultBatchedCommand) OptionValidatorException(org.aesh.command.validator.OptionValidatorException) IOException(java.io.IOException) CommandLineParserException(org.aesh.command.parser.CommandLineParserException) CommandFormatException(org.jboss.as.cli.CommandFormatException) ModelNode(org.jboss.dmr.ModelNode) OperationResponse(org.jboss.as.controller.client.OperationResponse) CommandNotFoundException(org.aesh.command.CommandNotFoundException) BatchCompliantCommand(org.wildfly.core.cli.command.BatchCompliantCommand) DefaultBatchedCommand(org.jboss.as.cli.batch.impl.DefaultBatchedCommand) BatchedCommand(org.jboss.as.cli.batch.BatchedCommand)

Aggregations

IOException (java.io.IOException)1 AccessDeniedException (java.nio.file.AccessDeniedException)1 CommandNotFoundException (org.aesh.command.CommandNotFoundException)1 CommandLineParserException (org.aesh.command.parser.CommandLineParserException)1 OptionValidatorException (org.aesh.command.validator.OptionValidatorException)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandHandler (org.jboss.as.cli.CommandHandler)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 Batch (org.jboss.as.cli.batch.Batch)1 BatchedCommand (org.jboss.as.cli.batch.BatchedCommand)1 DefaultBatchedCommand (org.jboss.as.cli.batch.impl.DefaultBatchedCommand)1 CommandCommandHandler (org.jboss.as.cli.handlers.CommandCommandHandler)1 ResponseHandler (org.jboss.as.cli.handlers.ResponseHandler)1 CLIExecution (org.jboss.as.cli.impl.aesh.AeshCommands.CLIExecution)1 OperationResponse (org.jboss.as.controller.client.OperationResponse)1 ModelNode (org.jboss.dmr.ModelNode)1 BatchCompliantCommand (org.wildfly.core.cli.command.BatchCompliantCommand)1 CLICommandInvocation (org.wildfly.core.cli.command.aesh.CLICommandInvocation)1