⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspectfilter.java

📁 达内Java培训实训项目ECPORT原码。 包括逐步优化的四个版本: 简单工厂模式实现 简单Spring-IoC实现 Spring声明式事务实现 Spring-AOP集成
💻 JAVA
字号:
package com.royee.ecport.aop;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.CodeSignature;

import com.royee.ecport.exception.AnnotationConflictException;
import com.royee.ecport.exception.ApplicationNotRunningException;

@SuppressWarnings("unchecked")
public abstract class AspectFilter {
	private final static Map<Method, Map<Class, Set>> annotationCache = new HashMap();

	/**
	 * 取当前request
	 * 
	 * @return
	 * @throws ApplicationNotRunningException
	 */
	protected HttpServletRequest request()
			throws ApplicationNotRunningException {
		HttpServletRequest req = ServletActionContext.getRequest();
		if (req == null)
			throw new ApplicationNotRunningException();
		return req;
	}

	/**
	 * 取当前response
	 * 
	 * @return
	 * @throws ApplicationNotRunningException
	 */
	protected HttpServletResponse response()
			throws ApplicationNotRunningException {
		HttpServletResponse resp = ServletActionContext.getResponse();
		if (resp == null)
			throw new ApplicationNotRunningException();
		return resp;
	}

	/**
	 * 取当前session
	 * 
	 * @return
	 * @throws ApplicationNotRunningException
	 */
	protected HttpSession session() throws ApplicationNotRunningException {
		return request().getSession();
	}

	public abstract void doFilter(JoinPoint jp);

	/**
	 * 获取方法同类型注解列表
	 * 
	 * @param <T>
	 * @param jp
	 * @param type
	 * @return
	 */
	protected <T extends Annotation> Set<T> getAnnotationList(JoinPoint jp,
			Class<T> type) {
		Set<Method> mList = new HashSet<Method>();
		Set<T> ret;
		Class clazz = jp.getTarget().getClass();
		String name = jp.getStaticPart().getSignature().getName();
		Class[] types = ((CodeSignature) jp.getStaticPart().getSignature())
				.getParameterTypes();
		Method method = null;
		try {
			method = clazz.getMethod(name, types);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//Cache处理
		if (cache(method, type) == null) {
			ret = new HashSet<T>();
			mList.addAll(searchMethod(clazz, name, types));
			for (Method m : mList) {
				T ann = m.getAnnotation(type);
				if (ann != null)
					ret.add(ann);
			}
			cache(method, type, ret);
		} else {
			ret = cache(method, type);
		}

		return ret;
	}

	/**
	 * 安全获取唯一注解,同一个方法在多出注解会发生异常
	 * 
	 * @param <T>
	 * @param jp
	 * @param type
	 * @return
	 */
	protected <T extends Annotation> T getAnnotation(JoinPoint jp, Class<T> type) {
		Set<T> aList = getAnnotationList(jp, type);
		if (aList.size() == 0)
			return null;
		else if (aList.size() != 1) {
			logger(jp).debug("实现类与超类或接口注解有冲突");
			throw new AnnotationConflictException();
		} else {
			return aList.iterator().next();
		}
	}

	/**
	 * 获取目标类Logger
	 * 
	 * @param jp
	 * @return
	 */
	protected Logger logger(JoinPoint jp) {
		return Logger.getLogger(jp.getTarget().getClass());
	}

	/**
	 * 递归搜索方法
	 * 
	 * @param clazz
	 * @param name
	 * @param types
	 * @return
	 */
	private Set<Method> searchMethod(Class clazz, String name, Class[] types) {
		Set ret = new HashSet();
		if (clazz == null)
			return ret;
		try {
			ret.add(clazz.getMethod(name, types));
		} catch (SecurityException e) {
		} catch (NoSuchMethodException e) {
		}

		if (ret.size() != 0) {
			Class[] intf = clazz.getInterfaces();
			ret.addAll(searchMethod(clazz.getSuperclass(), name, types));
			for (Class c : intf) {
				ret.addAll(searchMethod(c, name, types));
			}
		}
		return ret;
	}

	// Cache操作
	private void cache(Method m, Class type, Set s) {
		if (annotationCache.get(m) == null)
			annotationCache.put(m, new HashMap());
		if (annotationCache.get(m).get(type) == null)
			annotationCache.get(m).put(type, s);
	}

	private Set cache(Method m, Class type) {
		Set ret = null;
		try {
			ret = annotationCache.get(m).get(type);
		} catch (NullPointerException e) {
		}
		return ret;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -