01: /*
02: * Copyright (c) 2006 Your Corporation. All Rights Reserved.
03: */
04:
05: package com.technoetic.xplanner.db;
06:
07: import java.lang.reflect.Method;
08: import java.util.Arrays;
09: import java.util.Collections;
10: import java.util.List;
11: import java.util.Map;
12:
13: import org.apache.commons.lang.StringUtils;
14: import org.apache.log4j.Logger;
15: import org.springframework.aop.AfterReturningAdvice;
16:
17: /**
18: * User: Mateusz Prokopowicz
19: * Date: Sep 6, 2005
20: * Time: 6:40:13 AM
21: */
22: public class MethodCacheInvalidateInterceptor implements
23: AfterReturningAdvice {
24: private Map resultByArgsByMethodName;
25: private List methodNamesToInvalidate;
26: Logger LOG = Logger
27: .getLogger(MethodCacheInvalidateInterceptor.class);
28:
29: public void setMethodsToInvalidate(List methodNamesToInvalidate) {
30: this .methodNamesToInvalidate = methodNamesToInvalidate;
31: }
32:
33: public MethodCacheInvalidateInterceptor(Map cacheMap) {
34: this .resultByArgsByMethodName = cacheMap;
35: }
36:
37: public List getMethodCacheKey(Object args[]) {
38: return Arrays.asList(args);
39: }
40:
41: public void afterReturning(Object returnValue, Method method,
42: Object[] args, Object target) throws Throwable {
43: List argumentList = getMethodCacheKey(args);
44: invalidate(argumentList);
45: }
46:
47: void invalidate(List argumentList) {
48: for (int i = 0; i < methodNamesToInvalidate.size(); i++) {
49: String methodName = (String) methodNamesToInvalidate.get(i);
50: LOG.debug("Invalidate cache for method " + methodName + "("
51: + StringUtils.join(argumentList.iterator(), ", ")
52: + ")");
53: Map methodCache = (Map) resultByArgsByMethodName
54: .get(methodName);
55: if (methodCache != null) {
56: if (!methodCache.containsKey(argumentList)) {
57: argumentList = Collections.EMPTY_LIST;
58: }
59: if (methodCache.containsKey(argumentList)) {
60: methodCache.remove(argumentList);
61: }
62: }
63: }
64: }
65: }
|