001
014
015 package com.liferay.portal.cache.ehcache;
016
017 import com.liferay.portal.kernel.cache.BlockingPortalCache;
018 import com.liferay.portal.kernel.cache.PortalCache;
019 import com.liferay.portal.kernel.cache.PortalCacheManager;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.util.PropsUtil;
023 import com.liferay.portal.util.PropsValues;
024
025 import java.net.URL;
026
027 import javax.management.MBeanServer;
028
029 import net.sf.ehcache.CacheManager;
030 import net.sf.ehcache.Ehcache;
031 import net.sf.ehcache.ObjectExistsException;
032 import net.sf.ehcache.management.ManagementService;
033 import net.sf.ehcache.util.FailSafeTimer;
034
035
040 public class EhcachePortalCacheManager implements PortalCacheManager {
041
042 public void afterPropertiesSet() {
043 URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
044
045 _cacheManager = new CacheManager(url);
046
047 FailSafeTimer failSafeTimer = _cacheManager.getTimer();
048
049 failSafeTimer.cancel();
050
051 if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
052 _managementService = new ManagementService(
053 _cacheManager, _mBeanServer, _registerCacheManager,
054 _registerCaches, _registerCacheConfigurations,
055 _registerCacheStatistics);
056
057 _managementService.init();
058 }
059 }
060
061 public void clearAll() {
062 _cacheManager.clearAll();
063 }
064
065 public void destroy() throws Exception {
066 try {
067 _cacheManager.shutdown();
068 }
069 finally {
070 if (_managementService != null) {
071 _managementService.dispose();
072 }
073 }
074 }
075
076 public PortalCache getCache(String name) {
077 return getCache(name, false);
078 }
079
080 public PortalCache getCache(String name, boolean blocking) {
081 Ehcache cache = _cacheManager.getEhcache(name);
082
083 if (cache == null) {
084 synchronized (_cacheManager) {
085 cache = _cacheManager.getEhcache(name);
086
087 if (cache == null) {
088 try {
089 _cacheManager.addCache(name);
090 }
091 catch (ObjectExistsException oee) {
092
093
094
095 }
096
097 cache = _cacheManager.getEhcache(name);
098
099 cache.setStatisticsEnabled(
100 PropsValues.EHCACHE_STATISTICS_ENABLED);
101
102 if (_log.isInfoEnabled()) {
103 _log.info(
104 "Cache name " + name + " is using implementation " +
105 cache.getClass().getName());
106 }
107 }
108 }
109 }
110
111 PortalCache portalCache = new EhcachePortalCache(cache);
112
113 portalCache.setDebug(_debug);
114
115 if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
116 portalCache = new BlockingPortalCache(portalCache);
117 }
118
119 return portalCache;
120 }
121
122 public CacheManager getEhcacheManager() {
123 return _cacheManager;
124 }
125
126 public void setConfigPropertyKey(String configPropertyKey) {
127 _configPropertyKey = configPropertyKey;
128 }
129
130 public void setDebug(boolean debug) {
131 _debug = debug;
132 }
133
134 public void setMBeanServer(MBeanServer mBeanServer) {
135 _mBeanServer = mBeanServer;
136 }
137
138 public void setRegisterCacheConfigurations(
139 boolean registerCacheConfigurations) {
140
141 _registerCacheConfigurations = registerCacheConfigurations;
142 }
143
144 public void setRegisterCacheManager(boolean registerCacheManager) {
145 _registerCacheManager = registerCacheManager;
146 }
147
148 public void setRegisterCaches(boolean registerCaches) {
149 _registerCaches = registerCaches;
150 }
151
152 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
153 _registerCacheStatistics = registerCacheStatistics;
154 }
155
156 private static Log _log = LogFactoryUtil.getLog(
157 EhcachePortalCacheManager.class);
158
159 private String _configPropertyKey;
160 private CacheManager _cacheManager;
161 private boolean _debug;
162 private ManagementService _managementService;
163 private MBeanServer _mBeanServer;
164 private boolean _registerCacheManager = true;
165 private boolean _registerCaches = true;
166 private boolean _registerCacheConfigurations = true;
167 private boolean _registerCacheStatistics = true;
168
169 }