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
034
039 public class EhcachePortalCacheManager implements PortalCacheManager {
040
041 public void afterPropertiesSet() {
042 URL url = getClass().getResource(PropsUtil.get(_configPropertyKey));
043
044 _cacheManager = new CacheManager(url);
045
046 if (PropsValues.EHCACHE_PORTAL_CACHE_MANAGER_JMX_ENABLED) {
047 _managementService = new ManagementService(
048 _cacheManager, _mBeanServer, _registerCacheManager,
049 _registerCaches, _registerCacheConfigurations,
050 _registerCacheStatistics);
051
052 _managementService.init();
053 }
054 }
055
056 public void clearAll() {
057 _cacheManager.clearAll();
058 }
059
060 public void destroy() throws Exception {
061 try {
062 _cacheManager.shutdown();
063 }
064 finally {
065 if (_managementService != null) {
066 _managementService.dispose();
067 }
068 }
069 }
070
071 public PortalCache getCache(String name) {
072 return getCache(name, false);
073 }
074
075 public PortalCache getCache(String name, boolean blocking) {
076 Ehcache cache = _cacheManager.getEhcache(name);
077
078 if (cache == null) {
079 synchronized (_cacheManager) {
080 cache = _cacheManager.getEhcache(name);
081
082 if (cache == null) {
083 try {
084 _cacheManager.addCache(name);
085 }
086 catch (ObjectExistsException oee) {
087
088
089
090 }
091
092 cache = _cacheManager.getEhcache(name);
093
094 if (_log.isInfoEnabled()) {
095 _log.info(
096 "Cache name " + name + " is using implementation " +
097 cache.getClass().getName());
098 }
099 }
100 }
101 }
102
103 PortalCache portalCache = new EhcachePortalCache(cache);
104
105 portalCache.setDebug(_debug);
106
107 if (PropsValues.EHCACHE_BLOCKING_CACHE_ALLOWED && blocking) {
108 portalCache = new BlockingPortalCache(portalCache);
109 }
110
111 return portalCache;
112 }
113
114 public CacheManager getEhcacheManager() {
115 return _cacheManager;
116 }
117
118 public void setConfigPropertyKey(String configPropertyKey) {
119 _configPropertyKey = configPropertyKey;
120 }
121
122 public void setDebug(boolean debug) {
123 _debug = debug;
124 }
125
126 public void setMBeanServer(MBeanServer mBeanServer) {
127 _mBeanServer = mBeanServer;
128 }
129
130 public void setRegisterCacheConfigurations(
131 boolean registerCacheConfigurations) {
132
133 _registerCacheConfigurations = registerCacheConfigurations;
134 }
135
136 public void setRegisterCacheManager(boolean registerCacheManager) {
137 _registerCacheManager = registerCacheManager;
138 }
139
140 public void setRegisterCaches(boolean registerCaches) {
141 _registerCaches = registerCaches;
142 }
143
144 public void setRegisterCacheStatistics(boolean registerCacheStatistics) {
145 _registerCacheStatistics = registerCacheStatistics;
146 }
147
148 private static Log _log = LogFactoryUtil.getLog(
149 EhcachePortalCacheManager.class);
150
151 private String _configPropertyKey;
152 private CacheManager _cacheManager;
153 private boolean _debug;
154 private ManagementService _managementService;
155 private MBeanServer _mBeanServer;
156 private boolean _registerCacheManager = true;
157 private boolean _registerCaches = true;
158 private boolean _registerCacheConfigurations = true;
159 private boolean _registerCacheStatistics = true;
160
161 }