View Javadoc

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.List;
20  
21  import net.sf.ehcache.CacheManager;
22  import net.sf.ehcache.Element;
23  import net.sf.ehcache.ObjectExistsException;
24  
25  import org.aopalliance.aop.Advice;
26  import org.apache.commons.collections.keyvalue.MultiKey;
27  import org.apache.log4j.Logger;
28  import org.aspectj.lang.ProceedingJoinPoint;
29  
30  public class MethodArgsToObjectEhcacheAdvice implements Advice {
31  	final Logger LOG = Logger.getLogger(getClass());
32  
33  	private CacheManager cacheManager;
34  	private String cacheName;
35  	private boolean enabled;
36  
37  	/**
38  	 * 
39  	 */
40  	public MethodArgsToObjectEhcacheAdvice() {
41  		super();
42  	}
43  
44  	/**
45  	 * @param cacheName
46  	 */
47  	public MethodArgsToObjectEhcacheAdvice(String cacheName) {
48  		super();
49  		this.cacheName = cacheName;
50  	}
51  
52  	public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {
53  		Object result = pjp.proceed();
54  		if(enabled){
55  			if (cacheManager == null) {
56  				cacheManager = CacheManager.getInstance();
57  				try {
58  					cacheManager.addCache(cacheName);
59  				} catch (ObjectExistsException e) {
60  	
61  				}
62  			}
63  			LOG.info("Invalidating Cache: " + cacheName);
64  			cacheManager.getCache(cacheName).removeAll();
65  		}
66  		return result;
67  	}
68  
69  	public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {
70  		if(!enabled){
71  			return pjp.proceed();
72  		}
73  		
74  		if (cacheManager == null) {
75  			cacheManager = CacheManager.getInstance();
76  			try {
77  				cacheManager.addCache(cacheName);
78  			} catch (ObjectExistsException e) {
79  
80  			}
81  		}
82  		MultiKey cacheKey = getCacheKey(pjp);
83  
84  		Element cachedResult = cacheManager.getCache(cacheName).get(cacheKey);
85  		Object result = null;
86  		if (cachedResult == null) {
87  			result = pjp.proceed();
88  			LOG.info("Storing to Cache: " + cacheName);
89  			cacheManager.getCache(cacheName).put(new Element(cacheKey, result));
90  		} else {
91  			LOG.info("Found in Cache: " + cacheName);
92  			result = cachedResult.getValue();
93  		}
94  
95  		return result;
96  	}
97  	
98  	private MultiKey getCacheKey(ProceedingJoinPoint pjp) {
99          List<Object> keyList = new ArrayList<Object>();
100         keyList.add(pjp.getSignature().getName());
101         for(Object arg : pjp.getArgs()){
102             if(arg==null){
103                 keyList.add("_null_");
104             }else{
105                 keyList.add(arg.toString());
106             }
107         }
108         return new MultiKey(keyList.toArray());
109     }
110 
111 	/**
112 	 * @return the cacheName
113 	 */
114 	public String getCacheName() {
115 		return cacheName;
116 	}
117 
118 	/**
119 	 * @param cacheName
120 	 *            the cacheName to set
121 	 */
122 	public void setCacheName(String cacheName) {
123 		this.cacheName = cacheName;
124 	}
125 
126 	public void setCacheManager(CacheManager cacheManager) {
127 		this.cacheManager = cacheManager;
128 	}
129 
130 	public boolean isEnabled() {
131 		return enabled;
132 	}
133 
134 	public void setEnabled(boolean enabled) {
135 		this.enabled = enabled;
136 	}
137 
138 }