首页 > 应用开发 > Java > Interceptor组成的链是如何进行调用的
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public interface Interceptor extends Serializable {
/**
* Called to let an interceptor clean up any resources it has allocated.
*/
void destroy();
/**
* Called after an interceptor is created, but before any requests are processed using
* {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving
* the Interceptor a chance to initialize any needed resources.
*/
void init();
/**
* Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
* request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.
*
* @param invocation the action invocation
* @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.
* @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.
*/
String intercept(ActionInvocation invocation) throws Exception;
}
|
|
1
|
String intercept(ActionInvocation invocation)
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/**
* ActionInvocation代表了Action的执行状态。由ActionInvocation对象可以获取到拦截器(Interceptors)和Action实例。
* An ActionInvocation represents the execution state of an Action. It holds the Interceptors and the Action instance.
* By repeated re-entrant execution of the <code>invoke() method, initially by the {@link ActionProxy}, then by the Interceptors, the
* Interceptors are all executed, and then the {@link Action} and the {@link Result}.
*
* @author Jason Carreira
* @see com.opensymphony.xwork2.ActionProxy
*/
public interface ActionInvocation extends Serializable {
/**
* Invokes the next step in processing this ActionInvocation.
*
* If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit
* ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor
* to execute. If there are no more Interceptors to be applied, the Action is executed.
* If the {@link ActionProxy#getExecuteResult()} method returns <tt>true</tt>, the Result is also executed.
*
* @throws Exception can be thrown.
* @return the return code.
*/
String invoke() throws Exception;
//其它代码省略
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class DefaultActionInvocation implements ActionInvocation {
protected ActionProxy proxy;
protected Iterator<InterceptorMapping> interceptors;
protected String resultCode;
protected void createInterceptors(ActionProxy proxy) {
// get a new List so we don't get problems with the iterator if someone changes the list
List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
interceptors = interceptorList.iterator();
}
public String invoke() throws Exception {
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = interceptors.next();
String interceptorMsg = "interceptor: " + interceptor.getName();
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
} else {
resultCode = invokeActionOnly();
}
return resultCode;
}
}
|
|
1
|
List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
|
|
1
|
proxy.getConfig().getInterceptors()
|
|
1
2
3
|
ActionProxy proxy = invocation.getProxy();//ActionInvocation--->ActionProxy
ActionConfig config = proxy.getConfig();//ActionProxy-->ActionConfig
List<InterceptorMapping> interceptors = config.getInterceptors();//ActionConfig-->得到所有的拦截器
|
|
1
2
|
List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
interceptors = interceptorList.iterator();
|
|
1
2
3
4
5
6
7
8
9
10
11
12
|
public String invoke() throws Exception {
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = interceptors.next();
String interceptorMsg = "interceptor: " + interceptor.getName();
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
} else {
resultCode = invokeActionOnly();
}
return resultCode;
}
|
|
1
|
interceptors.hasNext()
|
|
1
|
resultCode = invokeActionOnly();
|
|
1
|
final InterceptorMapping interceptor = interceptors.next();
|
|
1
|
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
|
|
1
2
3
4
5
6
7
8
9
10
|
ActionProxy proxy = invocation.getProxy();//ActionInvocation--->ActionProxy
ActionConfig config = proxy.getConfig();//ActionProxy-->ActionConfig
List<InterceptorMapping> interceptors = config.getInterceptors();//ActionConfig-->得到所有的拦截器
Iterator<InterceptorMapping> iterator = interceptors.iterator();
if(iterator.hasNext())
{
InterceptorMapping interceptorMapping = iterator.next();
Interceptor interceptor = interceptorMapping.getInterceptor();
interceptor.intercept(invocation);
}
|
|
1
|
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);
|
|
1
|
DefaultActionInvocation.this
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29917475/viewspace-2121240/ ,如需转载,请注明出处,否则将追究法律责任。