第一部分 拦截器实现:
package com.infowarelab.cem.easytouch.web.interceptor;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;
import com.infowarelab.cem.easytouch.service.cache.JbossCache;
import com.infowarelab.common.log.Log;
import com.infowarelab.common.log.LogFactory;
public class FlushGroupCacheInterceptor implements MethodInterceptor,
InitializingBean {
protected Log log = LogFactory.getLog(FlushGroupCacheInterceptor.class);
private static final long serialVersionUID = 2366041803410037545L;
private JbossCache cache;
private Integer siteId;
public void setCache(JbossCache cache) {
this.cache = cache;
}
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = invocation.proceed();
flushGroupCache(invocation);
return result;
}
private void flushGroupCache(MethodInvocation invocation)
throws NoSuchMethodException {
// extends ActionSupport
// HttpSession session = ServletActionContext.getRequest().getSession();
// String
// siteId=(String)session.getAttribute(CEMConstants.SESSION_KEY_SITE_ID);
siteId = Context.getCurrentSiteId();
if (siteId == null) {
return;
}
String methodName = invocation.getMethod().getName();
log.info("--------------------");
log.info("methodName:" + methodName);
log.info("--------------------");
Class[] paramsType = invocation.getMethod().getParameterTypes();
Class methodNameClass = invocation.getThis().getClass();
Method m = methodNameClass.getMethod(methodName, paramsType);
boolean flag = m.isAnnotationPresent(OsCacheFlushGroupName.class);
if (flag) {
OsCacheFlushGroupName groupName = m
.getAnnotation(OsCacheFlushGroupName.class);
log.info("--------------------");
log.info("groupName:" + "" + siteId + groupName.value());
log.info("--------------------");
cache.flushGroup("" + siteId + groupName.value());
}
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
}
}
第2部分:web层数据传递
package com.infowarelab.cem.easytouch.web.interceptor;
public class Context {
private static ThreadLocal<Integer> currentSiteId = new ThreadLocal<Integer>();
private Context(){}
//这个方法由Filter在请求达到时根据会话(e.g HttpSession,但不限于HttpSession)情况调用设置
public static void setCurrentSiteId(Integer siteId) {
currentSiteId.set(siteId);
}
//这个方法,可被同一JVM中的任何其他方法调用。
//返回当前请求者的Id,没有登录,则返回null
public static Integer getCurrentSiteId() {
return currentSiteId.get();
}
}
第3部分:web层数据传递通过filter,或web层Interceptor实现
package com.infowarelab.cem.easytouch.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.infowarelab.cem.common.util.CEMConstants;
import com.infowarelab.cem.easytouch.web.interceptor.Context;
public class SiteIdFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
setSiteIdToContext(request);
filterChain.doFilter(request, response);
}
private void setSiteIdToContext(ServletRequest request) {
String siteId = "";
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession();
if (session.getAttribute(CEMConstants.SESSION_KEY_SITE_ID) != null) {
siteId = (String) session
.getAttribute(CEMConstants.SESSION_KEY_SITE_ID);
Context.setCurrentSiteId(new Integer(siteId));
return;
}
siteId = req.getParameter("siteId");
if (siteId != null && siteId.trim().length() > 0) {
Context.setCurrentSiteId(new Integer(siteId));
return;
}
}
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
}
}
------君临天下,舍我其谁
------