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,是真正的业务逻辑处理类。