1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.kuali.student.common.test.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.log4j.Logger; |
27 |
|
import org.aspectj.lang.ProceedingJoinPoint; |
28 |
|
|
|
|
| 53.7% |
Uncovered Elements: 31 (67) |
Complexity: 18 |
Complexity Density: 0.4 |
|
29 |
|
public class IdToObjectEhcacheAdvice implements Advice { |
30 |
|
final Logger LOG = Logger.getLogger(IdToObjectEhcacheAdvice.class); |
31 |
|
|
32 |
|
private CacheManager cacheManager; |
33 |
|
private String cacheName; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
38 |
3
|
public IdToObjectEhcacheAdvice() {... |
39 |
3
|
super(); |
40 |
|
} |
41 |
|
|
42 |
|
|
43 |
|
@param |
44 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
45 |
1
|
public IdToObjectEhcacheAdvice(String cacheName) {... |
46 |
1
|
super(); |
47 |
1
|
this.cacheName = cacheName; |
48 |
|
} |
49 |
|
|
|
|
| 55.6% |
Uncovered Elements: 4 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
50 |
2
|
public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {... |
51 |
2
|
if (cacheManager == null) { |
52 |
0
|
cacheManager = CacheManager.getInstance(); |
53 |
0
|
try { |
54 |
0
|
cacheManager.addCache(cacheName); |
55 |
|
} catch (ObjectExistsException e) { |
56 |
|
|
57 |
|
} |
58 |
|
} |
59 |
2
|
LOG.info("Invalidating Cache"); |
60 |
2
|
cacheManager.getCache(cacheName).remove(pjp.getArgs()[0]); |
61 |
2
|
return pjp.proceed(); |
62 |
|
} |
63 |
|
|
|
|
| 46.8% |
Uncovered Elements: 25 (47) |
Complexity: 11 |
Complexity Density: 0.33 |
|
64 |
7
|
@SuppressWarnings("unchecked")... |
65 |
|
public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable { |
66 |
7
|
if (cacheManager == null) { |
67 |
3
|
cacheManager = CacheManager.getInstance(); |
68 |
3
|
try { |
69 |
3
|
cacheManager.addCache(cacheName); |
70 |
|
} catch (ObjectExistsException e) { |
71 |
|
|
72 |
|
} |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
7
|
if (pjp.getArgs().length == 1 && pjp.getArgs()[0] instanceof List) { |
77 |
0
|
List<Object> results = new ArrayList<Object>(); |
78 |
0
|
List<String> uncachedIdList = new ArrayList<String>(); |
79 |
0
|
for (String id : (List<String>) pjp.getArgs()[0]) { |
80 |
|
|
81 |
0
|
LOG.info("Looking in Cache"); |
82 |
0
|
Element cachedResult = cacheManager.getCache(cacheName).get(id); |
83 |
0
|
if (cachedResult == null) { |
84 |
0
|
uncachedIdList.add(id); |
85 |
|
} else { |
86 |
0
|
results.add(cachedResult.getValue()); |
87 |
|
} |
88 |
|
} |
89 |
0
|
if (uncachedIdList.size() > 0) { |
90 |
0
|
List<Idable> uncachedResults = (List<Idable>) pjp.proceed(); |
91 |
0
|
if (uncachedResults != null) { |
92 |
0
|
for (Idable uncachedResult : uncachedResults) { |
93 |
|
|
94 |
0
|
LOG.info("Storing to Cache"); |
95 |
0
|
results.add(uncachedResult); |
96 |
0
|
cacheManager.getCache(cacheName).put( |
97 |
|
new Element(uncachedResult.getId(), |
98 |
|
uncachedResult)); |
99 |
|
} |
100 |
|
} |
101 |
|
} |
102 |
0
|
return results; |
103 |
|
} |
104 |
7
|
if (pjp.getArgs().length == 1 && pjp.getArgs()[0] instanceof String) { |
105 |
7
|
String id = (String) pjp.getArgs()[0]; |
106 |
7
|
LOG.info("Looking in Cache"); |
107 |
7
|
Element resultElement = cacheManager.getCache(cacheName).get(id); |
108 |
7
|
Object result; |
109 |
7
|
if (resultElement == null) { |
110 |
6
|
result = pjp.proceed(); |
111 |
6
|
LOG.info("Storing to Cache"); |
112 |
6
|
cacheManager.getCache(cacheName).put(new Element(id, result)); |
113 |
|
} else { |
114 |
1
|
result = resultElement.getValue(); |
115 |
|
} |
116 |
7
|
return result; |
117 |
|
} |
118 |
|
|
119 |
0
|
return pjp.proceed(); |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
@return |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
0
|
public String getCacheName() {... |
126 |
0
|
return cacheName; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
@param |
131 |
|
|
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
3
|
public void setCacheName(String cacheName) {... |
134 |
3
|
this.cacheName = cacheName; |
135 |
|
} |
136 |
|
|
137 |
|
} |