1
22
23 package com.liferay.portal.cache;
24
25 import com.liferay.portal.kernel.cache.PortalCache;
26 import com.liferay.portal.kernel.cache.PortalCacheManager;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.util.PropsUtil;
30 import com.liferay.portal.util.PropsValues;
31
32 import java.net.URL;
33
34 import javax.management.MBeanServer;
35
36 import net.sf.ehcache.CacheManager;
37 import net.sf.ehcache.Ehcache;
38 import net.sf.ehcache.ObjectExistsException;
39 import net.sf.ehcache.constructs.blocking.BlockingCache;
40 import net.sf.ehcache.management.ManagementService;
41
42
49 public class EhcachePortalCacheManager implements PortalCacheManager {
50
51 public void afterPropertiesSet() {
52 URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
53
54 _cacheManager = new CacheManager(url);
55
56 ManagementService.registerMBeans(
57 _cacheManager, _mbeanServer, _registerCacheManager, _registerCaches,
58 _registerCacheConfigurations, _registerCacheStatistics);
59 }
60
61 public void clearAll() {
62 _cacheManager.clearAll();
63 }
64
65 public void destroy() throws Exception {
66 _cacheManager.shutdown();
67 }
68
69 public PortalCache getCache(String name) {
70 return getCache(name, false);
71 }
72
73 public PortalCache getCache(String name, boolean blocking) {
74 Ehcache cache = _cacheManager.getEhcache(name);
75
76 if (cache == null) {
77 try {
78 _cacheManager.addCache(name);
79 }
80 catch (ObjectExistsException oee) {
81
82
84 }
85
86 cache = _cacheManager.getEhcache(name);
87
88 if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
89 cache = replaceCacheWithDecoratedCache(cache);
90 }
91
92 if (_log.isInfoEnabled()) {
93 _log.info(
94 "Cache name " + name + " is using implementation " +
95 cache.getClass().getName());
96 }
97 }
98
99 return new EhcachePortalCache(cache);
100 }
101
102 public void setConfigPropertyKey(String configPropertyKey) {
103 _configPropertyKey = configPropertyKey;
104 }
105
106 public void setMBeanServer(MBeanServer server) {
107 _mbeanServer = server;
108 }
109
110 public void setRegisterCacheConfigurations(
111 boolean registerCacheConfigurations) {
112
113 _registerCacheConfigurations = registerCacheConfigurations;
114 }
115
116 public void setRegisterCacheManager(boolean registerCacheManager) {
117 _registerCacheManager = registerCacheManager;
118 }
119
120 public void setRegisterCaches(boolean registerCaches) {
121 _registerCaches = registerCaches;
122 }
123
124 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
125 _registerCacheStatistics = registerCacheStatistics;
126 }
127
128 protected Ehcache replaceCacheWithDecoratedCache(Ehcache cache) {
129 if (cache instanceof BlockingCache) {
130 return cache;
131 }
132
133 Ehcache decoratedCache = new BlockingCache(cache);
134
135 _cacheManager.replaceCacheWithDecoratedCache(
136 cache, decoratedCache);
137
138 return decoratedCache;
139 }
140
141 private static Log _log =
142 LogFactoryUtil.getLog(EhcachePortalCacheManager.class);
143
144 private String _configPropertyKey;
145 private CacheManager _cacheManager;
146 private MBeanServer _mbeanServer;
147 private boolean _registerCacheManager = true;
148 private boolean _registerCaches = true;
149 private boolean _registerCacheConfigurations = true;
150 private boolean _registerCacheStatistics = true;
151
152 }