1 17 18 package org.objectweb.jac.wrappers; 19 20 import org.aopalliance.intercept.ConstructorInvocation; 21 import org.aopalliance.intercept.MethodInvocation; 22 import org.objectweb.jac.core.AspectComponent; 23 import org.objectweb.jac.core.Interaction; 24 import org.objectweb.jac.core.Wrapper; 25 26 import java.util.Vector ; 27 28 61 62 public class CacheWrapper extends Wrapper { 63 64 65 protected Vector cacheKeys = new Vector (); 66 67 protected Vector cacheValues = new Vector (); 68 69 public CacheWrapper (AspectComponent ac) { 70 super(ac); 71 } 72 73 93 94 public Object statelessCache(Interaction interaction) { 95 Object [] key = new Object [] { interaction.method, interaction.args }; 96 int i; 97 if((i = isCacheHit(key)) != -1) { 98 return getCacheValue(i); 99 } 100 Object ret; 101 setCacheValue(key, ret = proceed(interaction)); 102 return ret; 103 } 104 105 125 126 public Object stateCache(Interaction interaction) { 127 System.out.println("-> Testing the cache..."); 128 Object [] key = new Object [] { interaction.method, interaction.args, 129 interaction.wrappee }; 130 int i; 131 if((i = isCacheHit(key)) != -1) { 132 System.out.println("-> Cache hit!"); 133 return getCacheValue(i); 134 } 135 System.out.println("-> Cache miss."); 136 Object ret; 137 setCacheValue(key, ret = proceed(interaction)); 138 return ret; 139 } 140 141 149 150 public Object clearCache(Interaction interaction) { 151 cacheKeys.clear(); 152 cacheValues.clear(); 153 return proceed(interaction); 154 } 155 156 164 165 protected void setCacheValue(Object [] key, Object value) { 166 cacheKeys.add(key); 167 cacheValues.add(value); 168 } 169 170 179 protected Object getCacheValue(Object [] key) { 180 return getCacheValue(isCacheHit(key)); 181 } 182 183 189 190 protected Object getCacheValue(int index) { 191 if (index == -1) return null; 192 return cacheValues.get(index); 193 } 194 195 201 202 protected int isCacheHit(Object [] key) { 203 for (int i = 0; i < cacheKeys.size(); i ++) { 204 boolean same = true; 205 Object [] cur_key = (Object [])cacheKeys.get(i); 206 if (cur_key.length != key.length) { 207 System.out.println("A"); 208 same = false; 209 break; 210 } 211 if(!cur_key[0].equals(key[0])) { 212 System.out.println("B"); 213 same = false; 214 break; 215 } 216 if (((Object [])cur_key[1]).length != ((Object [])key[1]).length) { 217 System.out.println("C"); 218 same = false; 219 break; 220 } 221 for (int j = 0; j < ((Object [])cur_key[1]).length; j++) { 222 if(!((Object [])cur_key[1])[j].equals(((Object [])key[1])[j])) { 223 System.out.println("D" + ((Object [])cur_key[1])[j] + ((Object [])key[1])[j]); 224 same = false; 225 break; 226 } 227 } 228 if (key.length == 3) { 229 if(cur_key[2] != key[2]) { 230 System.out.println("E"); 231 same = false; 232 break; 233 } 234 } 235 if (same) { 236 return i; 237 } 238 } 239 return -1; 240 } 241 242 245 public Object invoke(MethodInvocation invocation) throws Throwable { 246 return null; 248 } 249 250 253 public Object construct(ConstructorInvocation invocation) throws Throwable { 254 return null; 256 } 257 258 } 259 260 261 262 263 264 | Popular Tags |