add basic pre/post processing aka poor man's AOP

SHL-40
This commit is contained in:
Costin Leau
2012-07-12 20:12:20 +03:00
parent 873e12a9ac
commit 0ae6edba37
3 changed files with 152 additions and 2 deletions

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.roo.shell.ParseResult;
/**
* Extension interface allowing command provider to be called
* in a generic fashion just before, and right after, executing a command.
*
* @author Costin Leau
*/
public interface ExecutionProcessor extends CommandMarker {
/**
* Method called before invoking the target command (described by {@link ParseResult}).
* Additionally, for advanced cases, the parse result itself effectively changing the invocation
* calling site.
*
* @param invocationContext target command context
* @return the invocation target
*/
ParseResult beforeInvocation(ParseResult invocationContext);
/**
* Method called after invoking the target command (described by {@link ParseResult}).
*
* @param invocationContext target command context
*/
void afterInvocation(ParseResult invocationContext);
}

View File

@@ -1,22 +1,47 @@
package org.springframework.shell;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.roo.shell.ExecutionStrategy;
import org.springframework.roo.shell.ParseResult;
import org.springframework.roo.support.util.Assert;
import org.springframework.roo.support.util.ReflectionUtils;
/**
* Simple execution strategy for invoking a target method.
* Supports pre/post processing to allow {@link CommandMarker}s for aop-like behavior (
* typically used for controlling stateful objects).
*
* @author Mark Pollack
* @author Costin Leau
*/
public class SimpleExecutionStrategy implements ExecutionStrategy {
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");
return ReflectionUtils.invokeMethod(parseResult.getMethod(), parseResult.getInstance(), parseResult.getArguments());
Object target = parseResult.getInstance();
if (target instanceof ExecutionProcessor) {
ExecutionProcessor processor = ((ExecutionProcessor) target);
parseResult = processor.beforeInvocation(parseResult);
try {
return invoke(parseResult);
} finally {
processor.afterInvocation(parseResult);
}
}
else {
return invoke(parseResult);
}
}
}
private Object invoke(ParseResult parseResult) {
return ReflectionUtils.invokeMethod(parseResult.getMethod(), parseResult.getInstance(), parseResult.getArguments());
}
public boolean isReadyForCommands() {
return true;
}