Search in sources :

Example 6 with ParseResult

use of org.springframework.shell.event.ParseResult 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;
}
Also used : ParseResult(org.springframework.shell.event.ParseResult) Method(java.lang.reflect.Method) CommandProcessingException(org.apache.geode.management.cli.CommandProcessingException) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) IOException(java.io.IOException) NotAuthorizedException(org.apache.geode.security.NotAuthorizedException) CommandProcessingException(org.apache.geode.management.cli.CommandProcessingException) ParseResult(org.springframework.shell.event.ParseResult) Result(org.apache.geode.management.cli.Result) CommentSkipHelper(org.apache.geode.management.internal.cli.util.CommentSkipHelper) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 7 with ParseResult

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;
}
Also used : ParseResult(org.springframework.shell.event.ParseResult)

Example 8 with ParseResult

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();
}
Also used : ParseResult(org.springframework.shell.event.ParseResult) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ParseResult (org.springframework.shell.event.ParseResult)8 Result (org.apache.geode.management.cli.Result)5 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)4 Method (java.lang.reflect.Method)3 CommandProcessingException (org.apache.geode.management.cli.CommandProcessingException)3 FileResult (org.apache.geode.management.internal.cli.result.FileResult)3 NotAuthorizedException (org.apache.geode.security.NotAuthorizedException)3 CliAroundInterceptor (org.apache.geode.management.internal.cli.CliAroundInterceptor)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ManagementService (org.apache.geode.management.ManagementService)1 CommandRequest (org.apache.geode.management.internal.cli.CommandRequest)1 CommandResponse (org.apache.geode.management.internal.cli.CommandResponse)1 MultiStepCommand (org.apache.geode.management.internal.cli.multistep.MultiStepCommand)1 CommandResult (org.apache.geode.management.internal.cli.result.CommandResult)1 SectionResultData (org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData)1 CommentSkipHelper (org.apache.geode.management.internal.cli.util.CommentSkipHelper)1 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)1 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)1 Test (org.junit.Test)1