1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.ksb.cache; |
18 | |
|
19 | |
import com.opensymphony.oscache.base.AbstractCacheAdministrator; |
20 | |
import com.opensymphony.oscache.base.CacheEntry; |
21 | |
import com.opensymphony.oscache.base.NeedsRefreshException; |
22 | |
import com.opensymphony.oscache.general.GeneralCacheAdministrator; |
23 | |
import org.apache.commons.lang.StringUtils; |
24 | |
import org.apache.log4j.Logger; |
25 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
26 | |
import org.kuali.rice.ksb.api.bus.ServiceBus; |
27 | |
import org.kuali.rice.ksb.api.cache.RiceCacheAdministrator; |
28 | |
|
29 | |
import java.util.Properties; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class RiceCacheAdministratorImpl implements RiceCacheAdministrator { |
37 | |
|
38 | |
private static final String DEFAULT_SERVICE_NAME = "OSCacheNotificationService"; |
39 | |
|
40 | |
private static final String CACHE_PREFIX = "cache."; |
41 | |
private GeneralCacheAdministrator cacheAdministrator; |
42 | |
private boolean started; |
43 | |
private boolean forceRegistryRefresh; |
44 | |
private String serviceName; |
45 | |
protected ServiceBus serviceBus; |
46 | 0 | private static final Logger LOG = Logger.getLogger(RiceCacheAdministratorImpl.class); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public ServiceBus getServiceBus() { |
52 | 0 | return this.serviceBus; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public void setServiceBus(ServiceBus serviceBus) { |
59 | 0 | this.serviceBus = serviceBus; |
60 | 0 | } |
61 | |
|
62 | |
public boolean isStarted() { |
63 | 0 | return this.started; |
64 | |
} |
65 | |
|
66 | |
public Object getFromCache(String key) { |
67 | 0 | return getFromCache(key, CacheEntry.INDEFINITE_EXPIRY); |
68 | |
} |
69 | |
|
70 | |
public Object getFromCache(String key, int refreshPeriod) { |
71 | |
try { |
72 | 0 | return getCacheAdministrator().getFromCache(key, refreshPeriod); |
73 | 0 | } catch (NeedsRefreshException e) { |
74 | 0 | getCacheAdministrator().cancelUpdate(key); |
75 | 0 | return null; |
76 | |
} |
77 | |
} |
78 | |
|
79 | |
public void putInCache(String key, Object content, String[] groups) { |
80 | |
try { |
81 | 0 | getCacheAdministrator().putInCache(key, content, groups); |
82 | 0 | } catch (IllegalStateException e) { |
83 | 0 | LOG.warn("Failed to insert object into cache with key: " + key); |
84 | 0 | } |
85 | 0 | } |
86 | |
|
87 | |
public void putInCache(String key, Object content) { |
88 | |
try { |
89 | 0 | getCacheAdministrator().putInCache(key, content); |
90 | 0 | } catch (IllegalStateException e) { |
91 | 0 | LOG.warn("Failed to insert object into cache with key: " + key); |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
public void flushEntry(String key) { |
97 | 0 | getCacheAdministrator().flushEntry(key); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
public void flushGroup(String group) { |
102 | 0 | getCacheAdministrator().flushGroup(group); |
103 | 0 | } |
104 | |
|
105 | |
public void flushAll() { |
106 | 0 | getCacheAdministrator().flushAll(); |
107 | 0 | } |
108 | |
|
109 | |
public void setCacheCapacity(int capacity) { |
110 | 0 | getCacheAdministrator().setCacheCapacity(capacity); |
111 | 0 | } |
112 | |
|
113 | |
public int getCacheCapacity() { |
114 | 0 | return getCacheAdministrator().getCache().getCapacity(); |
115 | |
} |
116 | |
|
117 | |
public int getSize() { |
118 | 0 | return getCacheAdministrator().getCache().getSize(); |
119 | |
} |
120 | |
|
121 | |
public void start() throws Exception { |
122 | 0 | Properties props = loadCacheSettings(); |
123 | 0 | this.cacheAdministrator = new GeneralCacheAdministrator(props); |
124 | 0 | } |
125 | |
|
126 | |
protected Properties loadCacheSettings() { |
127 | 0 | Properties properties = new Properties(); |
128 | 0 | Properties configProperties = ConfigContext.getCurrentContextConfig().getProperties(); |
129 | 0 | for (Object keyObject : configProperties.keySet()) { |
130 | 0 | String key = (String)keyObject; |
131 | 0 | if (key.startsWith(CACHE_PREFIX)) { |
132 | 0 | properties.put(key, configProperties.getProperty(key)); |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | 0 | if (!properties.containsKey(AbstractCacheAdministrator.CACHE_MEMORY_KEY)) { |
137 | 0 | properties.put(AbstractCacheAdministrator.CACHE_MEMORY_KEY, "true"); |
138 | |
} |
139 | 0 | if (!properties.containsKey(AbstractCacheAdministrator.CACHE_ENTRY_EVENT_LISTENERS_KEY)) { |
140 | 0 | properties.put(AbstractCacheAdministrator.CACHE_ENTRY_EVENT_LISTENERS_KEY, RiceDistributedCacheListener.class.getName()); |
141 | |
} |
142 | 0 | if (!properties.containsKey(AbstractCacheAdministrator.CACHE_BLOCKING_KEY)) { |
143 | 0 | properties.put(AbstractCacheAdministrator.CACHE_BLOCKING_KEY, "false"); |
144 | |
} |
145 | 0 | properties.put(CacheProperties.FORCE_REGISTRY_REFRESH_KEY, new Boolean(this.forceRegistryRefresh)); |
146 | 0 | properties.put(CacheProperties.SERVICE_BUS, serviceBus); |
147 | 0 | if (StringUtils.isBlank(this.serviceName)) { |
148 | 0 | this.serviceName = DEFAULT_SERVICE_NAME; |
149 | |
} |
150 | 0 | properties.put(CacheProperties.SERVICE_NAME_KEY, this.serviceName); |
151 | 0 | return properties; |
152 | |
} |
153 | |
|
154 | |
public void stop() throws Exception { |
155 | 0 | getCacheAdministrator().destroy(); |
156 | 0 | this.started = false; |
157 | 0 | } |
158 | |
|
159 | |
public void putInCache(String key, Object content, String group) { |
160 | 0 | putInCache(key, content, new String[] {group}); |
161 | 0 | } |
162 | |
|
163 | |
protected GeneralCacheAdministrator getCacheAdministrator() { |
164 | 0 | return this.cacheAdministrator; |
165 | |
} |
166 | |
|
167 | |
public void setForceRegistryRefresh(boolean forceRegistryRefresh) { |
168 | 0 | this.forceRegistryRefresh = forceRegistryRefresh; |
169 | 0 | } |
170 | |
|
171 | |
public String getServiceName() { |
172 | 0 | return this.serviceName; |
173 | |
} |
174 | |
|
175 | |
public void setServiceName(String serviceName) { |
176 | 0 | this.serviceName = serviceName; |
177 | 0 | } |
178 | |
|
179 | |
} |