Clover Coverage Report - KS Common 1.2-M3-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Jun 6 2011 05:37:04 EDT
../../../../../../img/srcFileCovDistChart0.png 54% of files have more coverage
40   133   19   4
14   85   0.47   10
10     1.9  
1    
 
  MethodArgsToObjectEhcacheAdvice       Line # 26 40 0% 19 64 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 net.sf.ehcache.CacheManager;
19    import net.sf.ehcache.Element;
20    import net.sf.ehcache.ObjectExistsException;
21   
22    import org.aopalliance.aop.Advice;
23    import org.apache.log4j.Logger;
24    import org.aspectj.lang.ProceedingJoinPoint;
25   
 
26    public class MethodArgsToObjectEhcacheAdvice implements Advice {
27    final Logger LOG = Logger.getLogger(getClass());
28   
29    private CacheManager cacheManager;
30    private String cacheName;
31    private boolean enabled;
32   
33    /**
34    *
35    */
 
36  0 toggle public MethodArgsToObjectEhcacheAdvice() {
37  0 super();
38    }
39   
40    /**
41    * @param cacheName
42    */
 
43  0 toggle public MethodArgsToObjectEhcacheAdvice(String cacheName) {
44  0 super();
45  0 this.cacheName = cacheName;
46    }
47   
 
48  0 toggle public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {
49  0 Object result = pjp.proceed();
50  0 if(enabled){
51  0 if (cacheManager == null) {
52  0 cacheManager = CacheManager.getInstance();
53  0 try {
54  0 cacheManager.addCache(cacheName);
55    } catch (ObjectExistsException e) {
56   
57    }
58    }
59  0 LOG.info("Invalidating Cache: " + cacheName);
60  0 cacheManager.getCache(cacheName).removeAll();
61    }
62  0 return result;
63    }
64   
 
65  0 toggle public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {
66  0 if(!enabled){
67  0 return pjp.proceed();
68    }
69   
70  0 if (cacheManager == null) {
71  0 cacheManager = CacheManager.getInstance();
72  0 try {
73  0 cacheManager.addCache(cacheName);
74    } catch (ObjectExistsException e) {
75   
76    }
77    }
78  0 String cacheKey = getCacheKey(pjp);
79   
80  0 Element cachedResult = cacheManager.getCache(cacheName).get(cacheKey);
81  0 Object result = null;
82  0 if (cachedResult == null) {
83  0 result = pjp.proceed();
84  0 LOG.info("Storing to Cache: " + cacheName);
85  0 cacheManager.getCache(cacheName).put(new Element(cacheKey, result));
86    } else {
87  0 LOG.info("Found in Cache: " + cacheName);
88  0 result = cachedResult.getValue();
89    }
90   
91  0 return result;
92    }
93   
 
94  0 toggle private String getCacheKey(ProceedingJoinPoint pjp) {
95  0 final StringBuffer cacheKey = new StringBuffer(pjp.getSignature().getName());
96  0 cacheKey.append("(");
97  0 for (int i = 0; i < pjp.getArgs().length; i++) {
98  0 cacheKey.append(pjp.getArgs()[i].toString());
99  0 if (i + 1 != pjp.getArgs().length) {
100  0 cacheKey.append(",");
101    }
102    }
103  0 return cacheKey.toString();
104    }
105   
106    /**
107    * @return the cacheName
108    */
 
109  0 toggle public String getCacheName() {
110  0 return cacheName;
111    }
112   
113    /**
114    * @param cacheName
115    * the cacheName to set
116    */
 
117  0 toggle public void setCacheName(String cacheName) {
118  0 this.cacheName = cacheName;
119    }
120   
 
121  0 toggle public void setCacheManager(CacheManager cacheManager) {
122  0 this.cacheManager = cacheManager;
123    }
124   
 
125  0 toggle public boolean isEnabled() {
126  0 return enabled;
127    }
128   
 
129  0 toggle public void setEnabled(boolean enabled) {
130  0 this.enabled = enabled;
131    }
132   
133    }