package com.jarvis.cache.aop.nutz;

import java.lang.reflect.Method;

import org.nutz.aop.InterceptorChain;
import org.nutz.aop.MethodInterceptor;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.jarvis.cache.AbstractCacheManager;
import com.jarvis.cache.annotation.Cache;
import com.jarvis.cache.aop.CacheAopProxyChain;

public class AutoLoadCacheAopInterceptor implements MethodInterceptor {

	private final static Log log = Logs.get();

	private AbstractCacheManager cachePointCut;

	private Cache cache;

	private boolean haveCache;

	public AutoLoadCacheAopInterceptor(AbstractCacheManager cacheManager, Cache cache, Method method) {
		this.cachePointCut = cacheManager;
		this.cache = cache;
		if (method.isAnnotationPresent(Cache.class)) {
			if (log.isDebugEnabled()) {
				log.debugf("class %s , method %s", method.getDeclaringClass().getName(), method.getName());
			}
			this.haveCache = true;
		}
	}

	public void filter(final InterceptorChain chain) throws Throwable {
		try {
			if (haveCache) {
				Object obj = cachePointCut.proceed(new CacheAopProxyChain() {

					@Override
					public Class<?> getTargetClass() {
						return chain.getCallingMethod().getDeclaringClass();
					}

					@Override
					public Method getMethod() {
						return chain.getCallingMethod();
					}

					@Override
					public Object[] getArgs() {
						return chain.getArgs();
					}

					@Override
					public Object doProxyChain(Object[] arguments) throws Throwable {
						chain.doChain();
						return chain.getReturn();
					}
				}, cache);
				chain.setReturnValue(obj);
			} else {
				chain.doChain();
			}
		} catch (Throwable e) {
			throw e;
		}
	}
}

@@ -140,16 +140,45 @@
 ceptor;%0A
+import org.nutz.lang.Mirror;%0A
 import o
@@ -194,16 +194,16 @@
 og.Log;%0A
-
 import o
@@ -521,17 +521,16 @@
 intCut;%0A
-%0A
 %09private
@@ -543,17 +543,16 @@
  cache;%0A
-%0A
 %09private
@@ -570,16 +570,58 @@
 veCache;
+%0A%09private Mirror%3CInterceptorChain%3E mirror;
 %0A%0A%09publi
@@ -985,16 +985,62 @@
 ue;%0A%09%09%7D%0A
+%09%09mirror = Mirror.me(InterceptorChain.class);%0A
 %09%7D%0A%0A%09pub
@@ -1509,32 +1509,32 @@
 %0A%09%09%09%09%09@Override%0A
-
 %09%09%09%09%09public Obje
@@ -1584,24 +1584,73 @@
 Throwable %7B%0A
+%09%09%09%09%09%09mirror.setValue(chain, %22args%22, arguments);%0A
 %09%09%09%09%09%09chain.

fix 缓存