Coverage Report - org.kuali.student.common.util.spring.MethodArgsToObjectEhcacheAdvice
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodArgsToObjectEhcacheAdvice
0%
0/48
0%
0/14
2
 
 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  0
         final Logger LOG = Logger.getLogger(getClass());
 28  
 
 29  
         private CacheManager cacheManager;
 30  
         private String cacheName;
 31  
         private boolean enabled;
 32  
 
 33  
         /**
 34  
          * 
 35  
          */
 36  
         public MethodArgsToObjectEhcacheAdvice() {
 37  0
                 super();
 38  0
         }
 39  
 
 40  
         /**
 41  
          * @param cacheName
 42  
          */
 43  
         public MethodArgsToObjectEhcacheAdvice(String cacheName) {
 44  0
                 super();
 45  0
                 this.cacheName = cacheName;
 46  0
         }
 47  
 
 48  
         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  
                                 try {
 54  0
                                         cacheManager.addCache(cacheName);
 55  0
                                 } catch (ObjectExistsException e) {
 56  
         
 57  0
                                 }
 58  
                         }
 59  0
                         LOG.info("Invalidating Cache: " + cacheName);
 60  0
                         cacheManager.getCache(cacheName).removeAll();
 61  
                 }
 62  0
                 return result;
 63  
         }
 64  
 
 65  
         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  
                         try {
 73  0
                                 cacheManager.addCache(cacheName);
 74  0
                         } catch (ObjectExistsException e) {
 75  
 
 76  0
                         }
 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  
         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  
         public String getCacheName() {
 110  0
                 return cacheName;
 111  
         }
 112  
 
 113  
         /**
 114  
          * @param cacheName
 115  
          *            the cacheName to set
 116  
          */
 117  
         public void setCacheName(String cacheName) {
 118  0
                 this.cacheName = cacheName;
 119  0
         }
 120  
 
 121  
         public void setCacheManager(CacheManager cacheManager) {
 122  0
                 this.cacheManager = cacheManager;
 123  0
         }
 124  
 
 125  
         public boolean isEnabled() {
 126  0
                 return enabled;
 127  
         }
 128  
 
 129  
         public void setEnabled(boolean enabled) {
 130  0
                 this.enabled = enabled;
 131  0
         }
 132  
 
 133  
 }