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