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 if (_log.isInfoEnabled()) {
100 _log.info(
101 "Cache name " + name + " is using implementation " +
102 cache.getClass().getName());
103 }
104 }
105 }
106 }
107
108 PortalCache portalCache = new EhcachePortalCache(cache);
109
110 portalCache.setDebug(_debug);
111
112 if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
113 portalCache = new BlockingPortalCache(portalCache);
114 }
115
116 return portalCache;
117 }
118
119 public CacheManager getEhcacheManager() {
120 return _cacheManager;
121 }
122
123 public void setConfigPropertyKey(String configPropertyKey) {
124 _configPropertyKey = configPropertyKey;
125 }
126
127 public void setDebug(boolean debug) {
128 _debug = debug;
129 }
130
131 public void setMBeanServer(MBeanServer mBeanServer) {
132 _mBeanServer = mBeanServer;
133 }
134
135 public void setRegisterCacheConfigurations(
136 boolean registerCacheConfigurations) {
137
138 _registerCacheConfigurations = registerCacheConfigurations;
139 }
140
141 public void setRegisterCacheManager(boolean registerCacheManager) {
142 _registerCacheManager = registerCacheManager;
143 }
144
145 public void setRegisterCaches(boolean registerCaches) {
146 _registerCaches = registerCaches;
147 }
148
149 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
150 _registerCacheStatistics = registerCacheStatistics;
151 }
152
153 private static Log _log = LogFactoryUtil.getLog(
154 EhcachePortalCacheManager.class);
155
156 private String _configPropertyKey;
157 private CacheManager _cacheManager;
158 private boolean _debug;
159 private ManagementService _managementService;
160 private MBeanServer _mBeanServer;
161 private boolean _registerCacheManager = true;
162 private boolean _registerCaches = true;
163 private boolean _registerCacheConfigurations = true;
164 private boolean _registerCacheStatistics = true;
165
166 }