@Override
@SuppressWarnings("unchecked")
public int execute(ConsoleOperation output) {
if (output != null && output.getBuffer().trim().length() > 0) {
ResultHandler resultHandler = null;
try (CommandContainer commandContainer = getCommand(
Parser.findFirstWord(output.getBuffer()),
output.getBuffer())) {
CommandLine commandLine = commandContainer.getParser()
.parse(output.getBuffer());
resultHandler = commandContainer.getParser().getCommand().getResultHandler();
commandContainer
.getParser()
.getCommandPopulator()
.populateObject(commandContainer.getCommand(),
commandLine, invocationProviders, getAeshContext(), true);
// validate the command before execute, only call if no
// options with overrideRequired is not set
if (commandContainer.getParser().getCommand()
.getValidator() != null
&& !commandLine.hasOptionWithOverrideRequired())
commandContainer.getParser().getCommand()
.getValidator()
.validate(commandContainer.getCommand());
result = commandContainer
.getCommand()
.execute(
commandInvocationServices
.getCommandInvocationProvider(
commandInvocationProvider)
.enhanceCommandInvocation(
new AeshCommandInvocation(console,
output.getControlOperator(), this)));
if(result == CommandResult.SUCCESS)
resultHandler.onSuccess();
else
resultHandler.onFailure(result);
}
catch (CommandLineParserException e) {
getShell().out().println(e.getMessage());
result = CommandResult.FAILURE;
if(resultHandler != null)
resultHandler.onValidationFailure(result, e);
}
catch (CommandNotFoundException e) {
if (commandNotFoundHandler != null) {
commandNotFoundHandler.handleCommandNotFound(
output.getBuffer(), getShell());
}
else {
getShell().err().print(
"Command not found: "
+ Parser.findFirstWord(output
.getBuffer())
+ Config.getLineSeparator());
}
result = CommandResult.FAILURE;
}
catch (OptionValidatorException e) {
getShell().out().println(e.getMessage());
result = CommandResult.FAILURE;
if(resultHandler != null)
resultHandler.onValidationFailure(result, e);
}
catch (CommandValidatorException e) {
getShell().out().println(e.getMessage());
result = CommandResult.FAILURE;
if(resultHandler != null)
resultHandler.onValidationFailure(result, e);
}
catch (Exception e) {
logger.log(Level.SEVERE, "Exception when parsing/running: "
+ output.getBuffer(), e);
getShell().out().println(
"Exception when parsing/running: "
+ output.getBuffer() + ", "
+ e.getMessage());
result = CommandResult.FAILURE;
if(resultHandler != null)
resultHandler.onValidationFailure(result, e);
}
}
// empty line
else if (output != null) {
result = CommandResult.FAILURE;