j2ee页面静态化方案encache web cache框架源码分析2(一)

2014-11-24 08:12:19 · 作者: · 浏览: 16

encache的web cache代码分析

1.抽象filter分析
[java]
public abstract class Filter implements javax.servlet.Filter {
......
public final void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
throws ServletException, IOException {
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpServletResponse httpResponse = (HttpServletResponse) response;
try {
//NO_FILTER set for RequestDispatcher forwards to avoid double gzipping
if (filterNotDisabled(httpRequest)) { //判断是否需要进行改cache filter的处理,防止再次进入,简单的通过获取attribute属性来判断
doFilter(httpRequest, httpResponse, chain);
} else {
chain.doFilter(request, response);
}
} catch (final Throwable throwable) {
logThrowable(throwable, httpRequest);
}
}
protected boolean filterNotDisabled(final HttpServletRequest httpRequest) {
return httpRequest.getAttribute(NO_FILTER) == null;
}
......
//提供几个抽象方法供子类覆盖
/**
* A template method that performs any Filter specific destruction tasks.
* Called from {@link #destroy()}
*/
protected abstract void doDestroy();


/**
* A template method that performs the filtering for a request.
* Called from {@link #doFilter(ServletRequest,ServletResponse,FilterChain)}.

*/
protected abstract void doFilter(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse,
final FilterChain chain) throws Throwable;

/**
* A template method that performs any Filter specific initialisation tasks.
* Called from {@link #init(FilterConfig)}.
* @param filterConfig
*/
protected abstract void doInit(FilterConfig filterConfig) throws Exception;
}

2.实现上面filter的CachingFilter类分析
[java]
public abstract class CachingFilter extends Filter{
//初始化参数列表
public void doInit(FilterConfig filterConfig) throws CacheException {
synchronized (this.getClass()) {
if (blockingCache == null) {
setCacheNameIfAnyConfigured(filterConfig); //从web.xml里面取出encache对应配置的cache名称
final String localCacheName = getCacheName();
Ehcache cache = getCacheManager().getEhcache(localCacheName); //根据cachename从encache管理器获取cache实例
if (cache == null) {
throw new CacheException("cache '" + localCacheName
+ "' not found in configuration");
}
if (!(cache instanceof BlockingCache)) {
// decorate and substitute
BlockingCache newBlockingCache = new BlockingCache(cache);
getCacheManager().replaceCacheWithDecoratedCache(cache,
newBlockingCache);
}
blockingCache = (BlockingCache) getCacheManager().getEhcache(
localCacheName); //这里使用的是blockingCache
Integer blockingTimeoutMillis = parseBlockingCacheTimeoutMillis(filterConfig);//从init-param里面获取设置的blocking的超时时间
if (blockingTimeoutMillis != null && blockingTimeoutMillis > 0) {