001
014
015 package com.liferay.portal.dao.orm.hibernate.region;
016
017 import com.liferay.portal.cache.ehcache.EhcacheConfigurationUtil;
018 import com.liferay.portal.cache.ehcache.ModifiableEhcacheWrapper;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.Validator;
022
023 import java.net.URL;
024
025 import java.util.Map;
026 import java.util.Properties;
027
028 import net.sf.ehcache.Cache;
029 import net.sf.ehcache.CacheManager;
030 import net.sf.ehcache.Ehcache;
031 import net.sf.ehcache.config.CacheConfiguration;
032 import net.sf.ehcache.config.Configuration;
033 import net.sf.ehcache.config.ConfigurationFactory;
034 import net.sf.ehcache.hibernate.EhCacheRegionFactory;
035 import net.sf.ehcache.hibernate.regions.EhcacheCollectionRegion;
036 import net.sf.ehcache.hibernate.regions.EhcacheEntityRegion;
037 import net.sf.ehcache.hibernate.regions.EhcacheQueryResultsRegion;
038 import net.sf.ehcache.hibernate.regions.EhcacheTimestampsRegion;
039
040 import org.hibernate.cache.CacheDataDescription;
041 import org.hibernate.cache.CacheException;
042 import org.hibernate.cache.CollectionRegion;
043 import org.hibernate.cache.EntityRegion;
044 import org.hibernate.cache.QueryResultsRegion;
045 import org.hibernate.cache.TimestampsRegion;
046 import org.hibernate.cfg.Settings;
047
048
051 public class LiferayEhcacheRegionFactory extends EhCacheRegionFactory {
052
053 public LiferayEhcacheRegionFactory(Properties properties) {
054 super(properties);
055 }
056
057 @Override
058 public CollectionRegion buildCollectionRegion(
059 String regionName, Properties properties,
060 CacheDataDescription cacheDataDescription)
061 throws CacheException {
062
063 configureCache(regionName);
064
065 EhcacheCollectionRegion ehcacheCollectionRegion =
066 (EhcacheCollectionRegion)super.buildCollectionRegion(
067 regionName, properties, cacheDataDescription);
068
069 return new CollectionRegionWrapper(ehcacheCollectionRegion);
070 }
071
072 @Override
073 public EntityRegion buildEntityRegion(
074 String regionName, Properties properties,
075 CacheDataDescription cacheDataDescription)
076 throws CacheException {
077
078 configureCache(regionName);
079
080 EhcacheEntityRegion ehcacheEntityRegion =
081 (EhcacheEntityRegion)super.buildEntityRegion(
082 regionName, properties, cacheDataDescription);
083
084 return new EntityRegionWrapper(ehcacheEntityRegion);
085 }
086
087 @Override
088 public QueryResultsRegion buildQueryResultsRegion(
089 String regionName, Properties properties)
090 throws CacheException {
091
092 configureCache(regionName);
093
094 EhcacheQueryResultsRegion ehcacheQueryResultsRegion =
095 (EhcacheQueryResultsRegion)super.buildQueryResultsRegion(
096 regionName, properties);
097
098 return new QueryResultsRegionWrapper(ehcacheQueryResultsRegion);
099 }
100
101 @Override
102 public TimestampsRegion buildTimestampsRegion(
103 String regionName, Properties properties)
104 throws CacheException {
105
106 configureCache(regionName);
107
108 EhcacheTimestampsRegion ehcacheTimestampsRegion =
109 (EhcacheTimestampsRegion)super.buildTimestampsRegion(
110 regionName, properties);
111
112 TimestampsRegion timestampsRegion = new TimestampsRegionWrapper(
113 ehcacheTimestampsRegion);
114
115 return timestampsRegion;
116 }
117
118 public CacheManager getCacheManager() {
119 return manager;
120 }
121
122 public void reconfigureCaches(URL cacheConfigFile) {
123 synchronized (manager) {
124 Configuration configuration = EhcacheConfigurationUtil.
125 getConfiguration(cacheConfigFile, true);
126
127 Map<String, CacheConfiguration> cacheConfigurations =
128 configuration.getCacheConfigurations();
129
130 for (CacheConfiguration cacheConfiguration :
131 cacheConfigurations.values()) {
132
133 Ehcache ehcache = new Cache(cacheConfiguration);
134
135 reconfigureCache(ehcache);
136 }
137 }
138 }
139
140 @Override
141 public void start(Settings settings, Properties properties)
142 throws CacheException {
143
144 try {
145 String configurationPath = null;
146
147 if (properties != null) {
148 configurationPath = (String)properties.get(
149 NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
150 }
151
152 if (Validator.isNull(configurationPath)) {
153 configurationPath = _DEFAULT_CLUSTERED_EHCACHE_CONFIG_FILE;
154 }
155
156 Configuration configuration = null;
157
158 if (Validator.isNull(configurationPath)) {
159 configuration = ConfigurationFactory.parseConfiguration();
160 }
161 else {
162 boolean usingDefault = configurationPath.equals(
163 _DEFAULT_CLUSTERED_EHCACHE_CONFIG_FILE);
164
165 configuration = EhcacheConfigurationUtil.getConfiguration(
166 configurationPath, true, usingDefault);
167 }
168
169
173
174 manager = new CacheManager(configuration);
175
176 mbeanRegistrationHelper.registerMBean(manager, properties);
177 }
178 catch (net.sf.ehcache.CacheException ce) {
179 throw new CacheException(ce);
180 }
181 }
182
183 protected void configureCache(String regionName) {
184 synchronized (manager) {
185 Ehcache ehcache = manager.getEhcache(regionName);
186
187 if (ehcache == null) {
188 manager.addCache(regionName);
189
190 ehcache = manager.getEhcache(regionName);
191 }
192
193 if (!(ehcache instanceof ModifiableEhcacheWrapper)) {
194 Ehcache modifiableEhcacheWrapper = new ModifiableEhcacheWrapper(
195 ehcache);
196
197 manager.replaceCacheWithDecoratedCache(
198 ehcache, modifiableEhcacheWrapper);
199 }
200 }
201 }
202
203 protected void reconfigureCache(Ehcache replacementCache) {
204 String cacheName = replacementCache.getName();
205
206 Ehcache ehcache = manager.getEhcache(cacheName);
207
208 if ((ehcache != null) &&
209 (ehcache instanceof ModifiableEhcacheWrapper)) {
210
211 if (_log.isInfoEnabled()) {
212 _log.info("Reconfiguring Hibernate cache " + cacheName);
213 }
214
215 ModifiableEhcacheWrapper modifiableEhcacheWrapper =
216 (ModifiableEhcacheWrapper)ehcache;
217
218 manager.replaceCacheWithDecoratedCache(
219 ehcache, modifiableEhcacheWrapper.getWrappedCache());
220
221 manager.removeCache(cacheName);
222
223 manager.addCache(replacementCache);
224
225 modifiableEhcacheWrapper.setWrappedCache(replacementCache);
226
227 manager.replaceCacheWithDecoratedCache(
228 replacementCache, modifiableEhcacheWrapper);
229 }
230 else {
231 if (_log.isInfoEnabled()) {
232 _log.info("Configuring Hibernate cache " + cacheName);
233 }
234
235 if (ehcache != null) {
236 manager.removeCache(cacheName);
237 }
238
239 ehcache = new ModifiableEhcacheWrapper(replacementCache);
240
241 manager.addCache(replacementCache);
242
243 manager.replaceCacheWithDecoratedCache(replacementCache, ehcache);
244 }
245 }
246
247 private static final String _DEFAULT_CLUSTERED_EHCACHE_CONFIG_FILE =
248 "/ehcache/hibernate-clustered.xml";
249
250 private static Log _log = LogFactoryUtil.getLog(
251 LiferayEhcacheRegionFactory.class);
252
253 }