001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    
016    package org.kuali.student.common.test.spring;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import net.sf.ehcache.CacheManager;
022    import net.sf.ehcache.Element;
023    import net.sf.ehcache.ObjectExistsException;
024    
025    import org.aopalliance.aop.Advice;
026    import org.apache.log4j.Logger;
027    import org.aspectj.lang.ProceedingJoinPoint;
028    
029    @Deprecated
030    public class IdToObjectEhcacheAdvice implements Advice {
031            final Logger LOG = Logger.getLogger(IdToObjectEhcacheAdvice.class);
032            
033            private CacheManager cacheManager;
034            private String cacheName;
035    
036            /**
037             * 
038             */
039            public IdToObjectEhcacheAdvice() {
040                    super();
041            }
042    
043            /**
044             * @param cacheName
045             */
046            public IdToObjectEhcacheAdvice(String cacheName) {
047                    super();
048                    this.cacheName = cacheName;
049            }
050    
051            public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable {
052                    if (cacheManager == null) {
053                            cacheManager = CacheManager.getInstance();
054                            try {
055                                    cacheManager.addCache(cacheName);
056                            } catch (ObjectExistsException e) {
057    
058                            }
059                    }
060                    LOG.info("Invalidating Cache");
061                    cacheManager.getCache(cacheName).remove(pjp.getArgs()[0]);
062                    return pjp.proceed();
063            }
064    
065            @SuppressWarnings("unchecked")
066            public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable {
067                    if (cacheManager == null) {
068                            cacheManager = CacheManager.getInstance();
069                            try {
070                                    cacheManager.addCache(cacheName);
071                            } catch (ObjectExistsException e) {
072    
073                            }
074                    }
075                    // Have two caches for the one object? one by id, the other by method
076                    // call?
077                    if (pjp.getArgs().length == 1 && pjp.getArgs()[0] instanceof List) {
078                            List<Object> results = new ArrayList<Object>();
079                            List<String> uncachedIdList = new ArrayList<String>();
080                            for (String id : (List<String>) pjp.getArgs()[0]) {
081                                    // Look in the cache
082                                    LOG.info("Looking in Cache");
083                                    Element cachedResult = cacheManager.getCache(cacheName).get(id);
084                                    if (cachedResult == null) {
085                                            uncachedIdList.add(id);
086                                    } else {
087                                            results.add(cachedResult.getValue());
088                                    }
089                            }
090                            if (uncachedIdList.size() > 0) {
091                                    List<Idable> uncachedResults = (List<Idable>) pjp.proceed();
092                                    if (uncachedResults != null) {
093                                            for (Idable uncachedResult : uncachedResults) {
094                                                    // Add to the cache and add to results
095                                                    LOG.info("Storing to Cache");
096                                                    results.add(uncachedResult);
097                                                    cacheManager.getCache(cacheName).put(
098                                                                    new Element(uncachedResult.getId(),
099                                                                                    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    }