View Javadoc

1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.core.cache;
17  
18  
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24  import org.kuali.rice.core.impl.cache.DistributedCacheManagerDecorator;
25  
26  public class CacheUtils {
27      private static final Logger LOG = Logger.getLogger(CacheUtils.class);
28  
29      public static void flushCache(String cacheName) {
30          //flush cache
31          //DistributedCacheManagerDecorator distributedCacheManagerDecorator =
32          //        HrServiceLocator.getDistributedCacheManager();
33  
34          String cacheDecoratorName = extractCacheDecoratorName(cacheName);
35          if (StringUtils.isNotEmpty(cacheDecoratorName)) {
36              DistributedCacheManagerDecorator distributedCacheManagerDecorator =
37                      GlobalResourceLoader.getService(cacheDecoratorName);
38              if (distributedCacheManagerDecorator != null) {
39              	if(distributedCacheManagerDecorator.getCache(cacheName) == null) {
40              		LOG.error("Cache: " + cacheName + " not found.");
41              	} else {
42              		distributedCacheManagerDecorator.getCache(cacheName).clear();
43              	}
44                  
45              } else {
46                  LOG.error("DistributedCacheManagerDecorator: " + cacheDecoratorName + " not found.  Cache: " + cacheName + " was not flushed.");
47              }
48          }
49      }
50  
51      public static void flushCaches(List<String> cacheNames) {
52          //flush cache
53          for (String cache : cacheNames) {
54              String cacheDecoratorName = extractCacheDecoratorName(cache);
55              if (StringUtils.isNotEmpty(cacheDecoratorName)) {
56                  DistributedCacheManagerDecorator distributedCacheManagerDecorator =
57                          GlobalResourceLoader.getService(cacheDecoratorName);
58                  if (distributedCacheManagerDecorator != null) {
59                      distributedCacheManagerDecorator.getCache(cache).clear();
60                  } else {
61                      LOG.error("DistributedCacheManagerDecorator: " + cacheDecoratorName + " not found.  Cache: " + cache + " was not flushed.");
62                  }
63              }
64          }
65      }
66  
67      private static String extractCacheDecoratorName(String cacheName) {
68          String[] splitName = cacheName.split("/");
69          if (splitName.length >= 4) {
70              return "kpme" + StringUtils.capitalize(splitName[3]) + "DistributedCacheManager";
71          }
72          LOG.warn("Unable to extract cache decorator bean name from " + cacheName + ". Cache will not be flushed");
73          return StringUtils.EMPTY;
74      }
75  }