use of org.apache.geode.management.internal.cli.util.CommentSkipHelper 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.internal.cli.util.CommentSkipHelper 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