1
14
15 package com.liferay.portal.cache.ehcache;
16
17 import com.liferay.portal.cache.cluster.EhcachePortalCacheClusterReplicatorFactory;
18 import com.liferay.portal.kernel.cache.BlockingPortalCache;
19 import com.liferay.portal.kernel.cache.PortalCache;
20 import com.liferay.portal.kernel.cache.PortalCacheManager;
21 import com.liferay.portal.kernel.log.Log;
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
23 import com.liferay.portal.kernel.util.ReflectionUtil;
24 import com.liferay.portal.util.PropsUtil;
25 import com.liferay.portal.util.PropsValues;
26
27 import java.lang.reflect.Field;
28
29 import java.net.URL;
30
31 import javax.management.MBeanServer;
32
33 import net.sf.ehcache.CacheManager;
34 import net.sf.ehcache.Ehcache;
35 import net.sf.ehcache.ObjectExistsException;
36 import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration;
37 import net.sf.ehcache.config.CacheConfiguration;
38 import net.sf.ehcache.config.Configuration;
39 import net.sf.ehcache.config.ConfigurationFactory;
40 import net.sf.ehcache.management.ManagementService;
41 import net.sf.ehcache.util.FailSafeTimer;
42
43
51 public class EhcachePortalCacheManager implements PortalCacheManager {
52
53 public void afterPropertiesSet() {
54 Configuration configuration = getConfiguration();
55
56 _cacheManager = new CacheManager(configuration);
57
58 FailSafeTimer failSafeTimer = _cacheManager.getTimer();
59
60 failSafeTimer.cancel();
61
62 try {
63 Field cacheManagerTimerField = ReflectionUtil.getDeclaredField(
64 CacheManager.class, "cacheManagerTimer");
65
66 cacheManagerTimerField.set(_cacheManager, null);
67 }
68 catch (Exception e) {
69 throw new RuntimeException(e);
70 }
71
72 if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
73 _managementService = new ManagementService(
74 _cacheManager, _mBeanServer, _registerCacheManager,
75 _registerCaches, _registerCacheConfigurations,
76 _registerCacheStatistics);
77
78 _managementService.init();
79 }
80 }
81
82 public void clearAll() {
83 _cacheManager.clearAll();
84 }
85
86 public void destroy() throws Exception {
87 try {
88 _cacheManager.shutdown();
89 }
90 finally {
91 if (_managementService != null) {
92 _managementService.dispose();
93 }
94 }
95 }
96
97 public PortalCache getCache(String name) {
98 return getCache(name, false);
99 }
100
101 public PortalCache getCache(String name, boolean blocking) {
102 Ehcache cache = _cacheManager.getEhcache(name);
103
104 if (cache == null) {
105 synchronized (_cacheManager) {
106 cache = _cacheManager.getEhcache(name);
107
108 if (cache == null) {
109 try {
110 _cacheManager.addCache(name);
111 }
112 catch (ObjectExistsException oee) {
113
114
116 }
117
118 cache = _cacheManager.getEhcache(name);
119
120 cache.setStatisticsEnabled(
121 PropsValues.EHCACHE_STATISTICS_ENABLED);
122
123 if (_log.isInfoEnabled()) {
124 _log.info(
125 "Cache name " + name + " is using implementation " +
126 cache.getClass().getName());
127 }
128 }
129 }
130 }
131
132 PortalCache portalCache = new EhcachePortalCache(cache);
133
134 portalCache.setDebug(_debug);
135
136 if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
137 portalCache = new BlockingPortalCache(portalCache);
138 }
139
140 return portalCache;
141 }
142
143 public CacheManager getEhcacheManager() {
144 return _cacheManager;
145 }
146
147 public void removeCache(String name) {
148 _cacheManager.removeCache(name);
149 }
150
151 public void setConfigPropertyKey(String configPropertyKey) {
152 _configPropertyKey = configPropertyKey;
153 }
154
155 public void setDebug(boolean debug) {
156 _debug = debug;
157 }
158
159 public void setMBeanServer(MBeanServer mBeanServer) {
160 _mBeanServer = mBeanServer;
161 }
162
163 public void setRegisterCacheConfigurations(
164 boolean registerCacheConfigurations) {
165
166 _registerCacheConfigurations = registerCacheConfigurations;
167 }
168
169 public void setRegisterCacheManager(boolean registerCacheManager) {
170 _registerCacheManager = registerCacheManager;
171 }
172
173 public void setRegisterCaches(boolean registerCaches) {
174 _registerCaches = registerCaches;
175 }
176
177 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
178 _registerCacheStatistics = registerCacheStatistics;
179 }
180
181 protected Configuration getConfiguration() {
182 URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
183
184 Configuration configuration = ConfigurationFactory.parseConfiguration(
185 url);
186
187 if (PropsValues.EHCACHE_CLUSTER_LINK_REPLICATION_ENABLED) {
188 configuration.getCacheManagerPeerProviderFactoryConfiguration().
189 clear();
190 configuration.getCacheManagerPeerListenerFactoryConfigurations().
191 clear();
192
193 CacheConfiguration defaultCacheConfiguration =
194 configuration.getDefaultCacheConfiguration();
195
196 processCacheConfiguration(defaultCacheConfiguration);
197
198 for (CacheConfiguration cacheConfiguration :
199 configuration.getCacheConfigurations().values()) {
200
201 processCacheConfiguration(cacheConfiguration);
202 }
203 }
204
205 return configuration;
206 }
207
208 protected void processCacheConfiguration(
209 CacheConfiguration cacheConfiguration) {
210
211 cacheConfiguration.addBootstrapCacheLoaderFactory(null);
212
213 cacheConfiguration.getCacheEventListenerConfigurations().clear();
214
215 CacheEventListenerFactoryConfiguration configuration =
216 new CacheEventListenerFactoryConfiguration();
217
218 configuration.setClass(
219 EhcachePortalCacheClusterReplicatorFactory.class.getName());
220
221 cacheConfiguration.addCacheEventListenerFactory(configuration);
222 }
223
224 private static Log _log = LogFactoryUtil.getLog(
225 EhcachePortalCacheManager.class);
226
227 private String _configPropertyKey;
228 private CacheManager _cacheManager;
229 private boolean _debug;
230 private ManagementService _managementService;
231 private MBeanServer _mBeanServer;
232 private boolean _registerCacheManager = true;
233 private boolean _registerCaches = true;
234 private boolean _registerCacheConfigurations = true;
235 private boolean _registerCacheStatistics = true;
236
237 }