现在的位置: 首页 > 综合 > 正文

Groovy Interceptor — interceptor chain (类似http filter chain)

2013年11月04日 ⁄ 综合 ⁄ 共 1135字 ⁄ 字号 评论关闭

class InterceptorChain {
private List<ServiceInterceptor> siList;
private int pos;
private int totalInterceptors;
def realService;


public InterceptorChain(List<ServiceInterceptor>
interceptorList,Object realService){

if(interceptorList==null)
siList=new ArrayList<ServiceInterceptor>()
else{
this.siList=interceptorList
for(ServiceInterceptor si:siList){
si.chain=this;
}
}


this.totalInterceptors=interceptorList.size();
this.realService=realService;
}


def invokeMethod(String name, Object args){
if(pos<totalInterceptors){
ServiceInterceptor si=siList.get(pos++);
log.info("interceptor $si $name ")
si.metaClass.getMetaMethod(name, args).invoke(si,
args);

}else{
log.info("$realService $name ")
realService.metaClass.getMetaMethod(name, args).invoke(realService,
args);

}
}

}

abstract class ServiceInterceptor {
protected InterceptorChain chain;

}

abstract class AbstractXtWsInterceptor extends ServiceInterceptor implements IServiceLogic{

@Override

public void play(AuthHeaderRequest header,
Holder<AuthHeaderResponse> header0, Holder<InitBodyResponse> body)
throws XTException {
chain.init(header,header0,body);
}
}

//用法:

new InterceptorChain(interceptorList,realService);

realService implements IServiceLogic,是真正的业务逻辑处理类。

抱歉!评论已关闭.