use of org.springframework.shell.event.ParseResult in project geode by apache.
the class RemoteExecutionStrategy method execute.
public Object execute(ParseResult parseResult) throws RuntimeException {
Result result = null;
try {
Assert.notNull(parseResult, "Parse result required");
if (!GfshParseResult.class.isInstance(parseResult)) {
// TODO: should this message be more specific?
throw new IllegalArgumentException("Command Configuration/Definition error.");
}
GfshParseResult gfshParseResult = (GfshParseResult) parseResult;
Method method = gfshParseResult.getMethod();
if (!isShellOnly(method)) {
Boolean fromShell = CommandExecutionContext.isShellRequest();
boolean sentFromShell = fromShell != null && fromShell.booleanValue();
String interceptorClass = getInterceptor(gfshParseResult.getMethod());
CliAroundInterceptor interceptor = null;
// 1. Pre Execution
if (!sentFromShell && !CliMetaData.ANNOTATION_NULL_VALUE.equals(interceptorClass)) {
try {
interceptor = (CliAroundInterceptor) ClassPathLoader.getLatest().forName(interceptorClass).newInstance();
} catch (InstantiationException e) {
logWrapper.info(e.getMessage());
} catch (IllegalAccessException e) {
logWrapper.info(e.getMessage());
} catch (ClassNotFoundException e) {
logWrapper.info(e.getMessage());
}
if (interceptor != null) {
Result preExecResult = interceptor.preExecution(gfshParseResult);
if (Status.ERROR.equals(preExecResult.getStatus())) {
return preExecResult;
} else if (preExecResult instanceof FileResult) {
FileResult fileResult = (FileResult) preExecResult;
byte[][] fileData = fileResult.toBytes();
CommandExecutionContext.setBytesFromShell(fileData);
}
} else {
return ResultBuilder.createBadConfigurationErrorResult("Interceptor Configuration Error");
}
}
logWrapper.info("Executing " + gfshParseResult.getUserInput());
result = (Result) ReflectionUtils.invokeMethod(gfshParseResult.getMethod(), gfshParseResult.getInstance(), gfshParseResult.getArguments());
if (result != null && Status.ERROR.equals(result.getStatus())) {
logWrapper.info("Error occurred while executing \"" + gfshParseResult.getUserInput() + "\".");
}
if (interceptor != null) {
Result postExecResult = interceptor.postExecution(gfshParseResult, result, null);
if (postExecResult != null) {
if (Status.ERROR.equals(postExecResult.getStatus())) {
logWrapper.warning(postExecResult.toString(), null);
} else if (logWrapper.fineEnabled()) {
logWrapper.fine(String.valueOf(postExecResult));
}
result = postExecResult;
}
// for remote commands with bytes
CommandExecutionContext.setBytesFromShell(null);
}
} else {
throw new IllegalArgumentException("Only Remote command can be executed through " + ManagementService.class.getSimpleName() + ".processCommand() or ManagementMBean's processCommand " + "operation. Please refer documentation for the list of " + "commands.");
}
} catch (RuntimeException e) {
throw e;
}
return result;
}
use of org.springframework.shell.event.ParseResult 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.springframework.shell.event.ParseResult in project geode by apache.
the class GfshParserParsingTest method parseParams.
private Map<String, String> parseParams(String input, String commandMethod) {
ParseResult parseResult = parser.parse(input);
GfshParseResult gfshParseResult = (GfshParseResult) parseResult;
Map<String, String> params = gfshParseResult.getParamValueStrings();
for (String param : params.keySet()) {
System.out.println(param + "=" + params.get(param));
}
assertThat(gfshParseResult.getMethod().getName()).isEqualTo(commandMethod);
assertThat(gfshParseResult.getUserInput()).isEqualTo(input.trim());
return params;
}
use of org.springframework.shell.event.ParseResult in project geode by apache.
the class GfshParserConverterTest method testUnspecifiedValueToStringArray.
@Test
public void testUnspecifiedValueToStringArray() {
String command = "change loglevel --loglevel=finer --groups=group1,group2";
ParseResult result = parser.parse(command);
String[] memberIdValue = (String[]) result.getArguments()[0];
assertThat(memberIdValue).isNull();
}
use of org.springframework.shell.event.ParseResult in project geode by apache.
the class GfshParser method parse.
@Override
public GfshParseResult parse(String userInput) {
String rawInput = convertToSimpleParserInput(userInput);
// User SimpleParser to parse the input
ParseResult result = super.parse(rawInput);
if (result == null) {
return null;
}
return new GfshParseResult(result.getMethod(), result.getInstance(), result.getArguments(), userInput);
}
Aggregations