JFinal Interceptor 增强

JFinal Interceptor的使用过程中有没有力不从心的赶脚?

今天给大家带来更加灵活的Intercepter;

1.一个全局Intercenpter

public class RequestCheckInterceptor implements Interceptor{

    private Map<Class, Set<Check>> checkMap = new HashMap<Class, Set<Check>>();

    public RequestCheckInterceptor(){
        ClassKit.loop(new ClassCatch() {
            @Override
            public void doCatch(Class<?> clazz) {
                if (AbstractCheck.class.isAssignableFrom(clazz) && clazz != AbstractCheck.class) {

                    CheckBind bind = clazz.getAnnotation(CheckBind.class);
                    try {

                        if (checkMap.get(bind.value()) == null){
                            checkMap.put(bind.value(), new HashSet<Check>());
                        }
                        checkMap.get(bind.value()).add((Check)clazz.newInstance());
                    } catch (InstantiationException e) {
                        Log.e(e);
                    } catch (IllegalAccessException e) {
                        Log.e(e);
                    }
                }

            }
        });
    }
    @Override
    public void intercept(ActionInvocation ai) {

        if(checkAction(ai) != 0){
            return;
        }
        if(checkCtrl(ai) != 0){
            return;
        }
        ai.invoke();
    }

    private int checkAction(ActionInvocation ai){
        for(Annotation anno: ai.getMethod().getAnnotations()){
            try {
                Class annoClass = Class.forName(anno.annotationType().getName());
                Set<Check> checks = checkMap.get(annoClass);
                if (checks != null){
                    for (Check check:checks){
                        int code = check.onRequest(ai.getMethod().getAnnotation(annoClass), ai);
                        if(code != 0){
                            return code;
                        }
                    }
                }
            } catch (ClassNotFoundException e) {
                Log.e(e);
            }
        }
        return 0;
    }

    private int checkCtrl(ActionInvocation ai){
        for(Annotation anno: ai.getController().getClass().getAnnotations()){
            try {
                Class annoClass = Class.forName(anno.annotationType().getName());
                Set<Check> checks = checkMap.get(annoClass);
                if (checks != null){
                    for (Check check:checks){
                        int code = check.onRequest(ai.getController().getClass().getAnnotation(annoClass), ai);
                        if(0 != code){
                            return code;
                        }
                    }
                }
            } catch (ClassNotFoundException e) {
                Log.e(e);
            }
        }
        return 0;
    }

    public void addCheck(Class clazz, Check check){
        if (checkMap.get(clazz) == null){
            checkMap.put(clazz, new HashSet<Check>());
        }
        checkMap.get(clazz).add(check);
    }
}



好了,大功告成。

现在我们尝试一个自定义的Check,

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface CheckRunTime {
}



@CheckBind(CheckRunTime.class)
public class RunTimeCheck  extends AbstractCheck{
    @Override
    public int onRequest(Object anno, ActionInvocation ai) {
        Long start = System.currentTimeMillis();
        ai.invoke();
        Long stop = System.currentTimeMillis();
        Log.i("METHOD ->[" + ai.getMethodName() + "]<- cost::" + (stop - start));
        return 0;
    }
}



在Controller中

@AllowRole({"USER"})
    @CheckRunTime
    public void comment(){

    }





你可能感兴趣的:(java,Interceptor,jFinal)