Coverage Report - org.kuali.student.common.util.spring.MethodArgsToObjectEhcacheAdvice
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodArgsToObjectEhcacheAdvice
0%
0/51
0%
0/16
2.1
 
 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  
         /**
 37  
          * 
 38  
          */
 39  
         public MethodArgsToObjectEhcacheAdvice() {
 40  0
                 super();
 41  0
         }
 42  
 
 43  
         /**
 44  
          * @param cacheName
 45  
          */
 46  
         public MethodArgsToObjectEhcacheAdvice(String cacheName) {
 47  0
                 super();
 48  0
                 this.cacheName = cacheName;
 49  0
         }
 50  
 
 51  
         public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {
 52  0
                 Object result = pjp.proceed();
 53  0
                 if(enabled){
 54  0
                         if (cacheManager == null) {
 55  0
                                 cacheManager = CacheManager.getInstance();
 56  
                                 try {
 57  0
                                         cacheManager.addCache(cacheName);
 58  0
                                 } catch (ObjectExistsException e) {
 59  
         
 60  0
                                 }
 61  
                         }
 62  0
                         LOG.info("Invalidating Cache: " + cacheName);
 63  0
                         cacheManager.getCache(cacheName).removeAll();
 64  
                 }
 65  0
                 return result;
 66  
         }
 67  
 
 68  
         public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {
 69  0
                 if(!enabled){
 70  0
                         return pjp.proceed();
 71  
                 }
 72  
                 
 73  0
                 if (cacheManager == null) {
 74  0
                         cacheManager = CacheManager.getInstance();
 75  
                         try {
 76  0
                                 cacheManager.addCache(cacheName);
 77  0
                         } catch (ObjectExistsException e) {
 78  
 
 79  0
                         }
 80  
                 }
 81  0
                 String cacheKey = generateCacheKey(pjp);
 82  
 
 83  0
                 Element cachedResult = cacheManager.getCache(cacheName).get(cacheKey);
 84  0
                 Object result = null;
 85  0
                 if (cachedResult == null) {
 86  0
                         result = pjp.proceed();
 87  0
                         LOG.info("Storing to Cache: " + cacheName);
 88  0
                         cacheManager.getCache(cacheName).put(new Element(cacheKey, result));
 89  
                 } else {
 90  0
                         LOG.info("Found in Cache: " + cacheName);
 91  0
                         result = cachedResult.getValue();
 92  
                 }
 93  
 
 94  0
                 return result;
 95  
         }
 96  
 
 97  
         /**
 98  
          * Generate cache key based on the ProceedingJonPoint. Other advices can extend and override this method to implement their own strategy for key generation
 99  
          */
 100  
         protected String generateCacheKey(ProceedingJoinPoint pjp) {
 101  0
                 final StringBuffer cacheKey = new StringBuffer(pjp.getSignature().getName());
 102  0
                 cacheKey.append("(");
 103  0
                 for (int i = 0; i < pjp.getArgs().length; i++) {
 104  
                         
 105  0
                         if(null == pjp.getArgs()[i]) {
 106  
                                 // FIXME: This will result in inconsistent behvior if the value is the literal '<null>' vs being null
 107  0
                                 cacheKey.append("<null>");
 108  
                         } else {
 109  0
                                 cacheKey.append(pjp.getArgs()[i].toString());
 110  
                         }
 111  
                         
 112  0
                         if (i + 1 != pjp.getArgs().length) {
 113  0
                                 cacheKey.append(",");
 114  
                         }
 115  
                 }
 116  
                 
 117  0
                 cacheKey.append(")");
 118  0
                 return cacheKey.toString();
 119  
         }
 120  
 
 121  
         /**
 122  
          * @return the cacheName
 123  
          */
 124  
         public String getCacheName() {
 125  0
                 return cacheName;
 126  
         }
 127  
 
 128  
         /**
 129  
          * @param cacheName
 130  
          *            the cacheName to set
 131  
          */
 132  
         public void setCacheName(String cacheName) {
 133  0
                 this.cacheName = cacheName;
 134  0
         }
 135  
 
 136  
         public void setCacheManager(CacheManager cacheManager) {
 137  0
                 this.cacheManager = cacheManager;
 138  0
         }
 139  
 
 140  
         public boolean isEnabled() {
 141  0
                 return enabled;
 142  
         }
 143  
 
 144  
         public void setEnabled(boolean enabled) {
 145  0
                 this.enabled = enabled;
 146  0
         }
 147  
 
 148  
 }