1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.kuali.student.common.util.spring; |
17 |
|
|
18 |
|
import java.util.ArrayList; |
19 |
|
import java.util.List; |
20 |
|
|
21 |
|
import net.sf.ehcache.CacheManager; |
22 |
|
import net.sf.ehcache.Element; |
23 |
|
import net.sf.ehcache.ObjectExistsException; |
24 |
|
|
25 |
|
import org.aopalliance.aop.Advice; |
26 |
|
import org.apache.commons.collections.keyvalue.MultiKey; |
27 |
|
import org.apache.log4j.Logger; |
28 |
|
import org.aspectj.lang.ProceedingJoinPoint; |
29 |
|
|
|
|
| 0% |
Uncovered Elements: 62 (62) |
Complexity: 18 |
Complexity Density: 0.45 |
|
30 |
|
public class MethodArgsToObjectEhcacheAdvice implements Advice { |
31 |
|
final Logger LOG = Logger.getLogger(getClass()); |
32 |
|
|
33 |
|
private CacheManager cacheManager; |
34 |
|
private String cacheName; |
35 |
|
private boolean enabled; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
40 |
0
|
public MethodArgsToObjectEhcacheAdvice() {... |
41 |
0
|
super(); |
42 |
|
} |
43 |
|
|
44 |
|
|
45 |
|
@param |
46 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
47 |
0
|
public MethodArgsToObjectEhcacheAdvice(String cacheName) {... |
48 |
0
|
super(); |
49 |
0
|
this.cacheName = cacheName; |
50 |
|
} |
51 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
52 |
0
|
public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {... |
53 |
0
|
Object result = pjp.proceed(); |
54 |
0
|
if(enabled){ |
55 |
0
|
if (cacheManager == null) { |
56 |
0
|
cacheManager = CacheManager.getInstance(); |
57 |
0
|
try { |
58 |
0
|
cacheManager.addCache(cacheName); |
59 |
|
} catch (ObjectExistsException e) { |
60 |
|
|
61 |
|
} |
62 |
|
} |
63 |
0
|
LOG.info("Invalidating Cache: " + cacheName); |
64 |
0
|
cacheManager.getCache(cacheName).removeAll(); |
65 |
|
} |
66 |
0
|
return result; |
67 |
|
} |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 5 |
Complexity Density: 0.31 |
|
69 |
0
|
public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {... |
70 |
0
|
if(!enabled){ |
71 |
0
|
return pjp.proceed(); |
72 |
|
} |
73 |
|
|
74 |
0
|
if (cacheManager == null) { |
75 |
0
|
cacheManager = CacheManager.getInstance(); |
76 |
0
|
try { |
77 |
0
|
cacheManager.addCache(cacheName); |
78 |
|
} catch (ObjectExistsException e) { |
79 |
|
|
80 |
|
} |
81 |
|
} |
82 |
0
|
MultiKey cacheKey = getCacheKey(pjp); |
83 |
|
|
84 |
0
|
Element cachedResult = cacheManager.getCache(cacheName).get(cacheKey); |
85 |
0
|
Object result = null; |
86 |
0
|
if (cachedResult == null) { |
87 |
0
|
result = pjp.proceed(); |
88 |
0
|
LOG.info("Storing to Cache: " + cacheName); |
89 |
0
|
cacheManager.getCache(cacheName).put(new Element(cacheKey, result)); |
90 |
|
} else { |
91 |
0
|
LOG.info("Found in Cache: " + cacheName); |
92 |
0
|
result = cachedResult.getValue(); |
93 |
|
} |
94 |
|
|
95 |
0
|
return result; |
96 |
|
} |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
98 |
0
|
private MultiKey getCacheKey(ProceedingJoinPoint pjp) {... |
99 |
0
|
List<Object> keyList = new ArrayList<Object>(); |
100 |
0
|
keyList.add(pjp.getSignature().getName()); |
101 |
0
|
for(Object arg : pjp.getArgs()){ |
102 |
0
|
if(arg==null){ |
103 |
0
|
keyList.add("_null_"); |
104 |
|
}else{ |
105 |
0
|
keyList.add(arg.toString()); |
106 |
|
} |
107 |
|
} |
108 |
0
|
return new MultiKey(keyList.toArray()); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
@return |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
0
|
public String getCacheName() {... |
115 |
0
|
return cacheName; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
@param |
120 |
|
|
121 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
0
|
public void setCacheName(String cacheName) {... |
123 |
0
|
this.cacheName = cacheName; |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
0
|
public void setCacheManager(CacheManager cacheManager) {... |
127 |
0
|
this.cacheManager = cacheManager; |
128 |
|
} |
129 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
130 |
0
|
public boolean isEnabled() {... |
131 |
0
|
return enabled; |
132 |
|
} |
133 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
134 |
0
|
public void setEnabled(boolean enabled) {... |
135 |
0
|
this.enabled = enabled; |
136 |
|
} |
137 |
|
|
138 |
|
} |