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.test.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.log4j.Logger;
27  import org.aspectj.lang.ProceedingJoinPoint;
28  
29  public class IdToObjectEhcacheAdvice implements Advice {
30  	final Logger LOG = Logger.getLogger(IdToObjectEhcacheAdvice.class);
31  	
32  	private CacheManager cacheManager;
33  	private String cacheName;
34  
35  	/**
36  	 * 
37  	 */
38  	public IdToObjectEhcacheAdvice() {
39  		super();
40  	}
41  
42  	/**
43  	 * @param cacheName
44  	 */
45  	public IdToObjectEhcacheAdvice(String cacheName) {
46  		super();
47  		this.cacheName = cacheName;
48  	}
49  
50  	public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {
51  		if (cacheManager == null) {
52  			cacheManager = CacheManager.getInstance();
53  			try {
54  				cacheManager.addCache(cacheName);
55  			} catch (ObjectExistsException e) {
56  
57  			}
58  		}
59  		LOG.info("Invalidating Cache");
60  		cacheManager.getCache(cacheName).remove(pjp.getArgs()[0]);
61  		return pjp.proceed();
62  	}
63  
64  	@SuppressWarnings("unchecked")
65  	public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {
66  		if (cacheManager == null) {
67  			cacheManager = CacheManager.getInstance();
68  			try {
69  				cacheManager.addCache(cacheName);
70  			} catch (ObjectExistsException e) {
71  
72  			}
73  		}
74  		// Have two caches for the one object? one by id, the other by method
75  		// call?
76  		if (pjp.getArgs().length == 1 && pjp.getArgs()[0] instanceof List) {
77  			List<Object> results = new ArrayList<Object>();
78  			List<String> uncachedIdList = new ArrayList<String>();
79  			for (String id : (List<String>) pjp.getArgs()[0]) {
80  				// Look in the cache
81  				LOG.info("Looking in Cache");
82  				Element cachedResult = cacheManager.getCache(cacheName).get(id);
83  				if (cachedResult == null) {
84  					uncachedIdList.add(id);
85  				} else {
86  					results.add(cachedResult.getValue());
87  				}
88  			}
89  			if (uncachedIdList.size() > 0) {
90  				List<Idable> uncachedResults = (List<Idable>) pjp.proceed();
91  				if (uncachedResults != null) {
92  					for (Idable uncachedResult : uncachedResults) {
93  						// Add to the cache and add to results
94  						LOG.info("Storing to Cache");
95  						results.add(uncachedResult);
96  						cacheManager.getCache(cacheName).put(
97  								new Element(uncachedResult.getId(),
98  										uncachedResult));
99  					}
100 				}
101 			}
102 			return results;
103 		}
104 		if (pjp.getArgs().length == 1 && pjp.getArgs()[0] instanceof String) {
105 			String id = (String) pjp.getArgs()[0];
106 			LOG.info("Looking in Cache");
107 			Element resultElement = cacheManager.getCache(cacheName).get(id);
108 			Object result;
109 			if (resultElement == null) {
110 				result = pjp.proceed();
111 				LOG.info("Storing to Cache");
112 				cacheManager.getCache(cacheName).put(new Element(id, result));
113 			} else {
114 				result = resultElement.getValue();
115 			}
116 			return result;
117 		}
118 
119 		return pjp.proceed();
120 	}
121 
122 	/**
123 	 * @return the cacheName
124 	 */
125 	public String getCacheName() {
126 		return cacheName;
127 	}
128 
129 	/**
130 	 * @param cacheName
131 	 *            the cacheName to set
132 	 */
133 	public void setCacheName(String cacheName) {
134 		this.cacheName = cacheName;
135 	}
136 
137 }