最近项目上要用到拦截器的实现框架。本来打算用xworks的interceptor框架,奈何xworks的Interceptor框架过于重量。
只好参照xworks的实现方式自实现了一个。先上代码,以后再解释:
(1)ActionInterceptor
public interface ActionInterceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
}
(2)AbstractActionInterceptor
public abstract class AbstractActionInterceptor implements ActionInterceptor {
private static final long serialVersionUID = -6354317063874561256L;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
}
(3)ActionInterceptorHoler
public class ActionInterceptorHoler {
private static ActionInterceptorHoler instance = new ActionInterceptorHoler();
private HashMap<Class<?>, List<ActionInterceptor>> interceptorMap;
private ActionInterceptorHoler() {
init();
}
private void init() {
interceptorMap = new HashMap<Class<?>, List<ActionInterceptor>>();
}
public static ActionInterceptorHoler getInstance() {
return instance;
}
public List<ActionInterceptor> getInterceptor(Class<?> cls) {
return interceptorMap.get(cls);
}
public void putInterceptor(Class<?> cls, ActionInterceptor interceptor) {
List<ActionInterceptor> clsInterceptors = interceptorMap.get(cls);
if (clsInterceptors == null) {
clsInterceptors = new LinkedList<ActionInterceptor>();
interceptorMap.put(cls, clsInterceptors);
}
clsInterceptors.add(interceptor);
}
}
(4)ActionInvocation
public interface ActionInvocation {
void invoke() throws Exception;
void invokeActionOnly() throws Exception;
Iterator<ActionInterceptor> getInterceptors();
void setInterceptors(Iterator<ActionInterceptor> interceptors);
Object getInterceptedObject();
void setInterceptedObject(Object interceptedObject);
Method getInterceptedMethod();
void setInterceptedMethod(Method interceptedMethod);
Object[] getArguments();
void setArguments(Object[] arguments);
}
(5)DefaultActionInvocation
public class DefaultActionInvocation implements ActionInvocation {
private Object interceptedObject;
private Method interceptedMethod;
private Object[] arguments;
private Iterator<ActionInterceptor> interceptors;
@Override
public void invoke() throws Exception {
if (interceptors != null && interceptors.hasNext()) {
interceptors.next().intercept(this);
} else {
this.invokeActionOnly();
}
}
@Override
public void invokeActionOnly() throws Exception {
interceptedMethod.invoke(interceptedObject, arguments);
}
public Object getInterceptedObject() {
return interceptedObject;
}
public void setInterceptedObject(Object interceptedObject) {
this.interceptedObject = interceptedObject;
}
public Method getInterceptedMethod() {
return interceptedMethod;
}
public void setInterceptedMethod(Method interceptedMethod) {
this.interceptedMethod = interceptedMethod;
}
public Object[] getArguments() {
return arguments;
}
public void setArguments(Object[] arguments) {
this.arguments = arguments;
}
public Iterator<ActionInterceptor> getInterceptors() {
return interceptors;
}
public void setInterceptors(Iterator<ActionInterceptor> interceptors) {
this.interceptors = interceptors;
}
}
(6)Interceptable
public interface Interceptable {
List<ActionInterceptor> getInterceptors();
void setInterceptors(List<ActionInterceptor> interceptors);
}
(7)ActionProxyBuilder
public class ActionProxyBuilder {
public static ActionProxyBuilder builder = new ActionProxyBuilder();
private ActionProxyBuilder() {
}
@SuppressWarnings("unchecked")
public <T> T buildProxy(Class<T> clazz, Object object) {
Class<?>[] clazzs = { clazz };
T t = (T) Proxy.newProxyInstance(object.getClass().getClassLoader(),
clazzs, new ActionInvocatioHandler(object));
return t;
}
}
例子:
public interface Handler {
void handle();
}
public class MyHandler implements Handler, Interceptable {
private List<ActionInterceptor> interceptors;
@Override
public void handle() {
System.out.println("My Handler handle.");
}
@Override
public List<ActionInterceptor> getInterceptors() {
return this.interceptors;
}
@Override
public void setInterceptors(List<ActionInterceptor> interceptors) {
this.interceptors = interceptors;
}
}
public class MyTest {
public static void main(String args[]) {
ActionInterceptorHoler.getInstance().putInterceptor(MyHandler.class,
new FirstInterceptor());
ActionInterceptorHoler.getInstance().putInterceptor(MyHandler.class,
new SecondInterceptor());
MyHandler handler = new MyHandler();
List<ActionInterceptor> l = new LinkedList<ActionInterceptor>();
l.add(new ZeroInterceptor());
handler.setInterceptors(l);
Handler proxy = ActionProxyBuilder.builder.buildProxy(Handler.class,
handler);
proxy.handle();
}
}
class FirstInterceptor extends AbstractActionInterceptor {
private static final long serialVersionUID = 2897651507879363873L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("1 before");
invocation.invoke();
System.out.println("1 after");
return null;
}
}
class SecondInterceptor extends AbstractActionInterceptor {
private static final long serialVersionUID = 2897651507879363873L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("2 before");
invocation.invoke();
System.out.println("2 after");
return null;
}
}
class ZeroInterceptor extends AbstractActionInterceptor {
private static final long serialVersionUID = 2897651507879363873L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("0 before");
invocation.invoke();
System.out.println("0 after");
return null;
}
}