Interceptor框架的简单实现

最近项目上要用到拦截器的实现框架。本来打算用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;
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值