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