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