1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
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
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 }