extend processor with after returning/throwing

SHL-40
This commit is contained in:
Costin Leau
2012-07-12 20:45:24 +03:00
parent 0ae6edba37
commit d825aaeb41
3 changed files with 29 additions and 6 deletions

View File

@@ -37,9 +37,19 @@ public interface ExecutionProcessor extends CommandMarker {
ParseResult beforeInvocation(ParseResult invocationContext);
/**
* Method called after invoking the target command (described by {@link ParseResult}).
* Method called after successfully invoking the target command (described by {@link ParseResult}).
*
* @param invocationContext target command context
* @param result the invocation result
*/
void afterInvocation(ParseResult invocationContext);
void afterReturningInvocation(ParseResult invocationContext, Object result);
/**
* Method called after invoking the target command (described by {@link ParseResult}) had thrown an exception .
*
* @param invocationContext target command context
* @param thrown the thrown object
*/
void afterThrowingInvocation(ParseResult invocationContext, Throwable thrown);
}

View File

@@ -27,9 +27,18 @@ public class SimpleExecutionStrategy implements ExecutionStrategy {
ExecutionProcessor processor = ((ExecutionProcessor) target);
parseResult = processor.beforeInvocation(parseResult);
try {
return invoke(parseResult);
} finally {
processor.afterInvocation(parseResult);
Object result = invoke(parseResult);
processor.afterReturningInvocation(parseResult, result);
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);
}
}
else {

View File

@@ -46,9 +46,13 @@ public class SimpleExecutionStrategyTest {
return (beforeReturn == null ? invocationContext : beforeReturn);
}
public void afterInvocation(ParseResult invocationContext) {
public void afterReturningInvocation(ParseResult invocationContext, Object result) {
after = invocationContext;
}
public void afterThrowingInvocation(ParseResult invocationContext, Throwable thrown) {
//
}
}
private SimpleExecutionStrategy execution = new SimpleExecutionStrategy();