add custom logging in case the commands fail

This commit is contained in:
Costin Leau
2012-07-16 14:28:33 +03:00
parent 168dce2b50
commit e8efc552d3

View File

@@ -15,7 +15,10 @@
*/
package org.springframework.shell.core;
import java.util.logging.Logger;
import org.springframework.shell.event.ParseResult;
import org.springframework.shell.support.logging.HandlerUtils;
import org.springframework.shell.support.util.Assert;
import org.springframework.shell.support.util.ReflectionUtils;
@@ -29,12 +32,14 @@ import org.springframework.shell.support.util.ReflectionUtils;
*/
public class SimpleExecutionStrategy implements ExecutionStrategy {
private static final Logger logger = HandlerUtils.getLogger(SimpleExecutionStrategy.class);
private final Class<?> mutex = SimpleExecutionStrategy.class;
public Object execute(ParseResult parseResult) throws RuntimeException {
Assert.notNull(parseResult, "Parse result required");
synchronized (mutex) {
Assert.isTrue(isReadyForCommands(), "ProcessManagerHostedExecutionStrategy not yet ready for commands");
Assert.isTrue(isReadyForCommands(), "SimpleExecutionStrategy not yet ready for commands");
Object target = parseResult.getInstance();
if (target instanceof ExecutionProcessor) {
ExecutionProcessor processor = ((ExecutionProcessor) target);
@@ -45,13 +50,7 @@ public class SimpleExecutionStrategy implements ExecutionStrategy {
return result;
} catch (Throwable th) {
processor.afterThrowingInvocation(parseResult, th);
if (th instanceof Error) {
throw ((Error) th);
}
if (th instanceof RuntimeException) {
throw ((RuntimeException) th);
}
throw new RuntimeException(th);
return handleThrowable(th);
}
}
else {
@@ -61,7 +60,22 @@ public class SimpleExecutionStrategy implements ExecutionStrategy {
}
private Object invoke(ParseResult parseResult) {
return ReflectionUtils.invokeMethod(parseResult.getMethod(), parseResult.getInstance(), parseResult.getArguments());
try {
return ReflectionUtils.invokeMethod(parseResult.getMethod(), parseResult.getInstance(), parseResult.getArguments());
} catch (Throwable th) {
logger.severe("Command failed " + th);
return handleThrowable(th);
}
}
private Object handleThrowable(Throwable th) {
if (th instanceof Error) {
throw ((Error) th);
}
if (th instanceof RuntimeException) {
throw ((RuntimeException) th);
}
throw new RuntimeException(th);
}
public boolean isReadyForCommands() {