1 17 18 package org.objectweb.jac.aspects.cache; 19 20 import java.util.Hashtable ; 21 import org.aopalliance.intercept.ConstructorInvocation; 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.apache.log4j.Logger; 24 import org.objectweb.jac.aspects.timestamp.Timestamps; 25 import org.objectweb.jac.core.AspectComponent; 26 import org.objectweb.jac.core.Interaction; 27 import org.objectweb.jac.core.NameRepository; 28 import org.objectweb.jac.core.Wrapper; 29 import org.objectweb.jac.util.ObjectArray; 30 31 35 public class CacheWrapper extends Wrapper { 36 static final Logger logger = Logger.getLogger("cache"); 37 38 public CacheWrapper(AspectComponent ac) { 39 super(ac); 40 } 41 42 public CacheWrapper(AspectComponent ac, String stampsName) { 43 super(ac); 44 this.stampsName = stampsName; 45 } 46 47 String stampsName; 48 Timestamps stamps; 49 50 public Object invoke(MethodInvocation invocation) throws Throwable { 51 return cache((Interaction)invocation); 52 } 53 54 Hashtable cache = new Hashtable (); 55 56 public Object cache(Interaction interaction) { 57 logger.debug("cache "+interaction+"?"); 58 MethodCache methodCache = (MethodCache)cache.get(interaction.method); 60 if (methodCache==null) { 61 if (stampsName!=null) { 62 if (stamps == null) 63 stamps = (Timestamps)NameRepository.get().getObject(stampsName); 64 } 65 methodCache = new MethodCache(stamps); 66 cache.put(interaction.method,methodCache); 67 } 68 Object [] argsArray = new Object [interaction.args.length]; 69 System.arraycopy(interaction.args, 0, argsArray, 0, interaction.args.length); 70 int[] ignoredArgs = (int[])interaction.method.getAttribute(CacheAC.IGNORED_PARAMETERS); 71 ObjectArray args = new ObjectArray(argsArray); 72 73 MethodCache.Entry entry = methodCache.getEntry(args,ignoredArgs); 74 if (entry!=null) { 75 return entry.value; 76 } 77 Object result = proceed(interaction); 78 methodCache.putEntry(args,result); 79 return result; 80 } 81 82 85 void invalidate() { 86 cache.clear(); 87 } 88 89 } 90 | Popular Tags |