001    /**
002     * Copyright 2004-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kpme.core.cache;
017    
018    
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.apache.log4j.Logger;
023    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
024    import org.kuali.rice.core.impl.cache.DistributedCacheManagerDecorator;
025    
026    public class CacheUtils {
027        private static final Logger LOG = Logger.getLogger(CacheUtils.class);
028    
029        public static void flushCache(String cacheName) {
030            //flush cache
031            //DistributedCacheManagerDecorator distributedCacheManagerDecorator =
032            //        HrServiceLocator.getDistributedCacheManager();
033    
034            String cacheDecoratorName = extractCacheDecoratorName(cacheName);
035            if (StringUtils.isNotEmpty(cacheDecoratorName)) {
036                DistributedCacheManagerDecorator distributedCacheManagerDecorator =
037                        GlobalResourceLoader.getService(cacheDecoratorName);
038                if (distributedCacheManagerDecorator != null) {
039                    if(distributedCacheManagerDecorator.getCache(cacheName) == null) {
040                            LOG.error("Cache: " + cacheName + " not found.");
041                    } else {
042                            distributedCacheManagerDecorator.getCache(cacheName).clear();
043                    }
044                    
045                } else {
046                    LOG.error("DistributedCacheManagerDecorator: " + cacheDecoratorName + " not found.  Cache: " + cacheName + " was not flushed.");
047                }
048            }
049        }
050    
051        public static void flushCaches(List<String> cacheNames) {
052            //flush cache
053            for (String cache : cacheNames) {
054                String cacheDecoratorName = extractCacheDecoratorName(cache);
055                if (StringUtils.isNotEmpty(cacheDecoratorName)) {
056                    DistributedCacheManagerDecorator distributedCacheManagerDecorator =
057                            GlobalResourceLoader.getService(cacheDecoratorName);
058                    if (distributedCacheManagerDecorator != null) {
059                        distributedCacheManagerDecorator.getCache(cache).clear();
060                    } else {
061                        LOG.error("DistributedCacheManagerDecorator: " + cacheDecoratorName + " not found.  Cache: " + cache + " was not flushed.");
062                    }
063                }
064            }
065        }
066    
067        private static String extractCacheDecoratorName(String cacheName) {
068            String[] splitName = cacheName.split("/");
069            if (splitName.length >= 4) {
070                return "kpme" + StringUtils.capitalize(splitName[3]) + "DistributedCacheManager";
071            }
072            LOG.warn("Unable to extract cache decorator bean name from " + cacheName + ". Cache will not be flushed");
073            return StringUtils.EMPTY;
074        }
075    }