use of org.apache.geode.management.cli.CommandProcessingException in project geode by apache.
the class GfshExecutionStrategy method execute.
//////////////// ExecutionStrategy interface Methods Start ///////////////////
///////////////////////// Implemented Methods ////////////////////////////////
/**
* Executes the method indicated by the {@link ParseResult} which would always be
* {@link GfshParseResult} for GemFire defined commands. If the command Method is decorated with
* {@link CliMetaData#shellOnly()} set to <code>false</code>, {@link OperationInvoker} is used to
* send the command for processing on a remote GemFire node.
*
* @param parseResult that should be executed (never presented as null)
* @return an object which will be rendered by the {@link Shell} implementation (may return null)
* @throws RuntimeException which is handled by the {@link Shell} implementation
*/
@Override
public Object execute(ParseResult parseResult) {
Result result = null;
Method method = parseResult.getMethod();
try {
// Check if it's a multi-step command
MultiStepCommand cmd = method.getAnnotation(MultiStepCommand.class);
if (cmd != null) {
return execCLISteps(logWrapper, shell, parseResult);
}
// check if it's a shell only command
if (isShellOnly(method)) {
Assert.notNull(parseResult, "Parse result required");
synchronized (mutex) {
Assert.isTrue(isReadyForCommands(), "ProcessManagerHostedExecutionStrategy not yet ready for commands");
return ReflectionUtils.invokeMethod(parseResult.getMethod(), parseResult.getInstance(), parseResult.getArguments());
}
}
// check if it's a GfshParseResult
if (!GfshParseResult.class.isInstance(parseResult)) {
throw new IllegalStateException("Configuration error!");
}
result = executeOnRemote((GfshParseResult) parseResult);
} catch (NotAuthorizedException e) {
result = ResultBuilder.createGemFireUnAuthorizedErrorResult("Unauthorized. Reason: " + e.getMessage());
} catch (JMXInvocationException | IllegalStateException e) {
Gfsh.getCurrentInstance().logWarning(e.getMessage(), e);
} catch (CommandProcessingException e) {
Gfsh.getCurrentInstance().logWarning(e.getMessage(), null);
Object errorData = e.getErrorData();
if (errorData != null && errorData instanceof Throwable) {
logWrapper.warning(e.getMessage(), (Throwable) errorData);
} else {
logWrapper.warning(e.getMessage());
}
} catch (Exception e) {
Gfsh.getCurrentInstance().logWarning("Unexpected exception occurred. " + e.getMessage(), e);
// Log other exceptions in gfsh log
logWrapper.warning("Unexpected error occurred while executing command : " + ((GfshParseResult) parseResult).getUserInput(), e);
}
return result;
}
use of org.apache.geode.management.cli.CommandProcessingException in project geode by apache.
the class ScriptExecutionDetails method executeScript.
public Result executeScript(File scriptFile, boolean quiet, boolean continueOnError) {
Result result = null;
String initialIsQuiet = getEnvProperty(ENV_APP_QUIET_EXECUTION);
try {
this.isScriptRunning = true;
if (scriptFile == null) {
throw new IllegalArgumentException("Given script file is null.");
} else if (!scriptFile.exists()) {
throw new IllegalArgumentException("Given script file does not exist.");
} else if (scriptFile.exists() && scriptFile.isDirectory()) {
throw new IllegalArgumentException(scriptFile.getPath() + " is a directory.");
}
ScriptExecutionDetails scriptInfo = new ScriptExecutionDetails(scriptFile.getPath());
if (scriptFile.exists()) {
setEnvProperty(ENV_APP_QUIET_EXECUTION, String.valueOf(quiet));
this.supressScriptCmdOutput = quiet;
BufferedReader reader = new BufferedReader(new FileReader(scriptFile));
String lineRead = "";
StringBuilder linesBuffer = new StringBuilder();
// used to check whether the string in a buffer contains a
String linesBufferString = "";
// ";".
int commandSrNum = 0;
CommentSkipHelper commentSkipper = new CommentSkipHelper();
LINEREAD_LOOP: while (exitShellRequest == null && (lineRead = reader.readLine()) != null) {
if (linesBuffer == null) {
linesBuffer = new StringBuilder();
}
String lineWithoutComments = commentSkipper.skipComments(lineRead);
if (lineWithoutComments == null || lineWithoutComments.isEmpty()) {
continue;
}
if (linesBuffer.length() != 0) {
// add " " between lines
linesBuffer.append(" ");
}
linesBuffer.append(lineWithoutComments);
linesBufferString = linesBuffer.toString();
// NOTE: Similar code is in promptLoop()
if (!linesBufferString.endsWith(GfshParser.CONTINUATION_CHARACTER)) {
// see 45893
// String command = null;
List<String> commandList = MultiCommandHelper.getMultipleCommands(linesBufferString);
for (String cmdLet : commandList) {
if (!cmdLet.isEmpty()) {
String redactedCmdLet = ArgumentRedactor.redact(cmdLet);
++commandSrNum;
Gfsh.println(commandSrNum + ". Executing - " + redactedCmdLet);
Gfsh.println();
boolean executeSuccess = executeScriptLine(cmdLet);
if (!executeSuccess) {
setLastExecutionStatus(-1);
}
scriptInfo.addCommandAndStatus(cmdLet, getLastExecutionStatus() == -1 || getLastExecutionStatus() == -2 ? "FAILED" : "PASSED");
if ((getLastExecutionStatus() == -1 || getLastExecutionStatus() == -2) && !continueOnError) {
break LINEREAD_LOOP;
}
}
}
// reset buffer
linesBuffer = null;
linesBufferString = null;
} else {
linesBuffer.deleteCharAt(linesBuffer.length() - 1);
}
}
reader.close();
} else {
throw new CommandProcessingException(scriptFile.getPath() + " doesn't exist.", CommandProcessingException.ARGUMENT_INVALID, scriptFile);
}
result = scriptInfo.getResult();
scriptInfo.logScriptExecutionInfo(gfshFileLogger, result);
if (quiet) {
// Create empty result when in quiet mode
result = ResultBuilder.createInfoResult("");
}
} catch (IOException e) {
throw new CommandProcessingException("Error while reading file " + scriptFile, CommandProcessingException.RESOURCE_ACCESS_ERROR, e);
} finally {
// reset to original Quiet Execution value
setEnvProperty(ENV_APP_QUIET_EXECUTION, initialIsQuiet);
this.isScriptRunning = false;
}
return result;
}
use of org.apache.geode.management.cli.CommandProcessingException in project geode by apache.
the class CommandProcessor method executeCommand.
public Result executeCommand(CommandStatement cmdStmt) {
Object result = null;
Result commandResult = null;
CommentSkipHelper commentSkipper = new CommentSkipHelper();
String commentLessLine = commentSkipper.skipComments(cmdStmt.getCommandString());
if (commentLessLine != null && !commentLessLine.isEmpty()) {
CommandExecutionContext.setShellEnv(cmdStmt.getEnv());
final RemoteExecutionStrategy executionStrategy = getExecutionStrategy();
try {
ParseResult parseResult = ((CommandStatementImpl) cmdStmt).getParseResult();
if (parseResult == null) {
parseResult = parseCommand(commentLessLine);
if (parseResult == null) {
// TODO-Abhishek: Handle this in GfshParser Implementation
setLastExecutionStatus(1);
return ResultBuilder.createParsingErrorResult(cmdStmt.getCommandString());
}
((CommandStatementImpl) cmdStmt).setParseResult(parseResult);
}
// do general authorization check here
Method method = parseResult.getMethod();
ResourceOperation resourceOperation = method.getAnnotation(ResourceOperation.class);
this.securityService.authorize(resourceOperation);
result = executionStrategy.execute(parseResult);
if (result instanceof Result) {
commandResult = (Result) result;
} else {
if (logWrapper.fineEnabled()) {
logWrapper.fine("Unknown result type, using toString : " + String.valueOf(result));
}
commandResult = ResultBuilder.createInfoResult(String.valueOf(result));
}
} catch (CommandProcessingException e) {
// expected from Parser
setLastExecutionStatus(1);
if (logWrapper.infoEnabled()) {
logWrapper.info("Could not parse \"" + cmdStmt.getCommandString() + "\".", e);
}
return ResultBuilder.createParsingErrorResult(e.getMessage());
} catch (NotAuthorizedException e) {
setLastExecutionStatus(1);
if (logWrapper.infoEnabled()) {
logWrapper.info("Could not execute \"" + cmdStmt.getCommandString() + "\".", e);
}
// for NotAuthorizedException, will catch this later in the code
throw e;
} catch (RuntimeException e) {
setLastExecutionStatus(1);
if (logWrapper.infoEnabled()) {
logWrapper.info("Could not execute \"" + cmdStmt.getCommandString() + "\".", e);
}
return ResultBuilder.createGemFireErrorResult("Error while processing command <" + cmdStmt.getCommandString() + "> Reason : " + e.getMessage());
} catch (Exception e) {
setLastExecutionStatus(1);
if (logWrapper.warningEnabled()) {
logWrapper.warning("Could not execute \"" + cmdStmt.getCommandString() + "\".", e);
}
return ResultBuilder.createGemFireErrorResult("Unexpected error while processing command <" + cmdStmt.getCommandString() + "> Reason : " + e.getMessage());
}
if (logWrapper.fineEnabled()) {
logWrapper.fine("Executed " + commentLessLine);
}
setLastExecutionStatus(0);
}
return commandResult;
}
Aggregations