Clover Coverage Report - Kuali Student 1.2 (Aggregated)
Coverage timestamp: Thu Oct 27 2011 21:38:40 UTC
../../../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
38   135   17   3.8
10   86   0.45   10
10     1.7  
1    
 
  MethodArgsToObjectEhcacheAdvice       Line # 31 38 0% 17 58 0% 0.0
 
No Tests
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.common.util.spring;
17   
18    import java.util.ArrayList;
19    import java.util.Arrays;
20    import java.util.List;
21   
22    import net.sf.ehcache.CacheManager;
23    import net.sf.ehcache.Element;
24    import net.sf.ehcache.ObjectExistsException;
25   
26    import org.aopalliance.aop.Advice;
27    import org.apache.commons.collections.keyvalue.MultiKey;
28    import org.apache.log4j.Logger;
29    import org.aspectj.lang.ProceedingJoinPoint;
30   
 
31    public class MethodArgsToObjectEhcacheAdvice implements Advice {
32    final Logger LOG = Logger.getLogger(getClass());
33   
34    private CacheManager cacheManager;
35    private String cacheName;
36    private boolean enabled;
37   
38    /**
39    *
40    */
 
41  0 toggle public MethodArgsToObjectEhcacheAdvice() {
42  0 super();
43    }
44   
45    /**
46    * @param cacheName
47    */
 
48  0 toggle public MethodArgsToObjectEhcacheAdvice(String cacheName) {
49  0 super();
50  0 this.cacheName = cacheName;
51    }
52   
 
53  0 toggle public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {
54  0 Object result = pjp.proceed();
55  0 if(enabled){
56  0 if (cacheManager == null) {
57  0 cacheManager = CacheManager.getInstance();
58  0 try {
59  0 cacheManager.addCache(cacheName);
60    } catch (ObjectExistsException e) {
61   
62    }
63    }
64  0 LOG.info("Invalidating Cache: " + cacheName);
65  0 cacheManager.getCache(cacheName).removeAll();
66    }
67  0 return result;
68    }
69   
 
70  0 toggle public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {
71  0 if(!enabled){
72  0 return pjp.proceed();
73    }
74   
75  0 if (cacheManager == null) {
76  0 cacheManager = CacheManager.getInstance();
77  0 try {
78  0 cacheManager.addCache(cacheName);
79    } catch (ObjectExistsException e) {
80   
81    }
82    }
83  0 MultiKey cacheKey = getCacheKey(pjp);
84   
85  0 Element cachedResult = cacheManager.getCache(cacheName).get(cacheKey);
86  0 Object result = null;
87  0 if (cachedResult == null) {
88  0 result = pjp.proceed();
89  0 LOG.info("Storing to Cache: " + cacheName);
90  0 cacheManager.getCache(cacheName).put(new Element(cacheKey, result));
91    } else {
92  0 LOG.info("Found in Cache: " + cacheName);
93  0 result = cachedResult.getValue();
94    }
95   
96  0 return result;
97    }
98   
 
99  0 toggle private MultiKey getCacheKey(ProceedingJoinPoint pjp) {
100  0 List<Object> keyList = new ArrayList<Object>();
101  0 keyList.add(pjp.getSignature().getName());
102  0 for(Object arg : pjp.getArgs()){
103  0 keyList.add(arg.toString());
104    }
105  0 return new MultiKey(keyList.toArray());
106    }
107   
108    /**
109    * @return the cacheName
110    */
 
111  0 toggle public String getCacheName() {
112  0 return cacheName;
113    }
114   
115    /**
116    * @param cacheName
117    * the cacheName to set
118    */
 
119  0 toggle public void setCacheName(String cacheName) {
120  0 this.cacheName = cacheName;
121    }
122   
 
123  0 toggle public void setCacheManager(CacheManager cacheManager) {
124  0 this.cacheManager = cacheManager;
125    }
126   
 
127  0 toggle public boolean isEnabled() {
128  0 return enabled;
129    }
130   
 
131  0 toggle public void setEnabled(boolean enabled) {
132  0 this.enabled = enabled;
133    }
134   
135    }