1 15 package com.jdon.aop.interceptor; 16 17 import java.lang.reflect.*; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.aopalliance.intercept.*; 22 import com.jdon.controller.model.*; 23 import com.jdon.model.*; 24 import com.jdon.util.*; 25 26 39 public class CacheInterceptor implements MethodInterceptor { 40 private final static String module = CacheInterceptor.class.getName(); 41 42 private ModelManager modelManager; 43 44 public String match_MethodName = "get"; 45 46 private List isModelCache = new ArrayList (); 47 48 public CacheInterceptor(ModelManager modelManager) { 49 this.modelManager = modelManager; 50 } 51 52 public Object invoke(MethodInvocation invocation) throws Throwable { 53 Method method = invocation.getMethod(); 54 55 if (!methodMatchsModelGET(method)) { 56 return invocation.proceed(); } 60 Debug.logVerbose("[JdonFramework] enter cacheInteceptor method:" + method.getName(), 61 module); 62 try { 63 String dataKey = getArguments(invocation); 64 if (dataKey == null) return invocation.proceed(); 65 Class modelClass = method.getReturnType(); 66 ModelKey modelKey = new ModelKey(dataKey, modelClass); 67 ModelIF model = modelManager.getCache(modelKey); 68 if (model == null){ 69 model = (ModelIF) invocation.proceed(); Debug.logVerbose("[JdonFramework] save to cache", module); 71 modelManager.addCache(modelKey, model); 72 } 73 return model; 74 } catch (Exception e) { 75 Debug.logError("[JdonFramework]CacheInterceptor Exception error:" + e, module); 76 } 77 return invocation.proceed(); 78 } 79 80 81 90 private boolean methodMatchsModelGET(Method method) { 91 boolean condition = false; 92 try { 93 if (isModelCache.contains(method)) { 94 condition = true; 95 return condition; 96 } 97 98 String mehtodName = method.getName(); 99 if (method.getReturnType() == null) 100 return condition; Class returnClass = method.getReturnType(); 102 if (returnClass.getSuperclass() == null) 103 return condition; 105 Debug.logVerbose("[JdonFramework]methodMatchsModelGET: returnClassName = " 106 + returnClass.getName(), module); 107 if (ModelIF.class.isAssignableFrom(returnClass)) { 108 if (mehtodName.indexOf(match_MethodName) != -1) { 109 condition = true; 110 isModelCache.add(method); 113 } 114 } 115 } catch (Exception ex) { 116 Debug.logError("[JdonFramework]Exception error:" + ex, module); 117 } catch (Throwable the) { 118 Debug.logError("[JdonFramework]Throwable error:" + the, module); 119 } 120 return condition; 121 122 } 123 124 131 public String getArguments(MethodInvocation invocation) { 132 133 try { 134 Object [] args = invocation.getArguments(); 135 if (args == null) return null; 136 if (args.length > 1) return null; 138 return args[0].toString(); 139 } catch (Exception ex) { 140 Debug.logError("[JdonFramework] method:"+ invocation.getMethod().getName() 141 +" args is null or has not implements method: toString() ", module); 142 return null; 143 } 144 145 } 146 147 148 public String getMatch_MethodName() { 149 return match_MethodName; 150 } 151 152 public void setMatch_MethodName(String match_MethodName) { 153 this.match_MethodName = match_MethodName; 154 } 155 156 } 157 | Popular Tags |