| 1 |  |  | 
            
  
    | 2 |  |  | 
            
  
    | 3 |  |  | 
            
  
    | 4 |  |  | 
            
  
    | 5 |  |  | 
            
  
    | 6 |  |  | 
            
  
    | 7 |  |  | 
            
  
    | 8 |  |  | 
            
  
    | 9 |  |  | 
            
  
    | 10 |  |  | 
            
  
    | 11 |  |  | 
            
  
    | 12 |  |  | 
            
  
    | 13 |  |  | 
            
  
    | 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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 64 (64) | Complexity: 19 | Complexity Density: 0.47 |  | 
  
  
    | 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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 36 | 0 |  public MethodArgsToObjectEhcacheAdvice() {... | 
            
  
    | 37 | 0 | super(); | 
            
  
    | 38 |  | } | 
            
  
    | 39 |  |  | 
            
  
    | 40 |  |  | 
            
  
    | 41 |  | @param | 
            
  
    | 42 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 2 (2) | Complexity: 1 | Complexity Density: 0.5 |  | 
  
  
    | 43 | 0 |  public MethodArgsToObjectEhcacheAdvice(String cacheName) {... | 
            
  
    | 44 | 0 | super(); | 
            
  
    | 45 | 0 | this.cacheName = cacheName; | 
            
  
    | 46 |  | } | 
            
  
    | 47 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 13 (13) | Complexity: 4 | Complexity Density: 0.44 |  | 
  
  
    | 48 | 0 |  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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 22 (22) | Complexity: 5 | Complexity Density: 0.31 |  | 
  
  
    | 65 | 0 |  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 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 11 (11) | Complexity: 3 | Complexity Density: 0.43 |  | 
  
  
    | 94 | 0 |  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 | 
            
  
    | 108 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 109 | 0 |  public String getCacheName() {... | 
            
  
    | 110 | 0 | return cacheName; | 
            
  
    | 111 |  | } | 
            
  
    | 112 |  |  | 
            
  
    | 113 |  |  | 
            
  
    | 114 |  | @param | 
            
  
    | 115 |  |  | 
            
  
    | 116 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 117 | 0 |  public void setCacheName(String cacheName) {... | 
            
  
    | 118 | 0 | this.cacheName = cacheName; | 
            
  
    | 119 |  | } | 
            
  
    | 120 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 121 | 0 |  public void setCacheManager(CacheManager cacheManager) {... | 
            
  
    | 122 | 0 | this.cacheManager = cacheManager; | 
            
  
    | 123 |  | } | 
            
  
    | 124 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 125 | 0 |  public boolean isEnabled() {... | 
            
  
    | 126 | 0 | return enabled; | 
            
  
    | 127 |  | } | 
            
  
    | 128 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 129 | 0 |  public void setEnabled(boolean enabled) {... | 
            
  
    | 130 | 0 | this.enabled = enabled; | 
            
  
    | 131 |  | } | 
            
  
    | 132 |  |  | 
            
  
    | 133 |  | } |