2
* Copyright 2003-2010 Terracotta, Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package net.sf.ehcache.config;
19
import java.util.ArrayList;
20
import java.util.Collections;
21
import java.util.HashMap;
22
import java.util.HashSet;
23
import java.util.Iterator;
24
import java.util.List;
26
import java.util.Properties;
29
import net.sf.ehcache.Cache;
30
import net.sf.ehcache.CacheException;
31
import net.sf.ehcache.CacheManager;
32
import net.sf.ehcache.Ehcache;
33
import net.sf.ehcache.config.CacheConfiguration.CacheDecoratorFactoryConfiguration;
34
import net.sf.ehcache.constructs.CacheDecoratorFactory;
35
import net.sf.ehcache.distribution.CacheManagerPeerListener;
36
import net.sf.ehcache.distribution.CacheManagerPeerListenerFactory;
37
import net.sf.ehcache.distribution.CacheManagerPeerProvider;
38
import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
39
import net.sf.ehcache.event.CacheManagerEventListener;
40
import net.sf.ehcache.event.CacheManagerEventListenerFactory;
41
import net.sf.ehcache.exceptionhandler.CacheExceptionHandler;
42
import net.sf.ehcache.exceptionhandler.CacheExceptionHandlerFactory;
43
import net.sf.ehcache.exceptionhandler.ExceptionHandlingDynamicCacheProxy;
44
import net.sf.ehcache.util.ClassLoaderUtil;
45
import net.sf.ehcache.util.PropertyUtil;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
50
* The configuration for ehcache.
52
* This class can be populated through:
54
* <li>introspection by {@link ConfigurationFactory} or
55
* <li>programmatically
59
* @version $Id: ConfigurationHelper.java 2401 2010-05-05 18:11:38Z lorban $
61
public final class ConfigurationHelper {
63
private static final Logger LOG = LoggerFactory.getLogger(ConfigurationHelper.class.getName());
65
private Configuration configuration;
66
private CacheManager cacheManager;
72
* @param configuration
74
public ConfigurationHelper(CacheManager cacheManager, Configuration configuration) {
75
if (cacheManager == null || configuration == null) {
76
throw new IllegalArgumentException("Cannot have null parameters");
78
this.cacheManager = cacheManager;
79
this.configuration = configuration;
83
* Tries to create a CacheLoader from the configuration using the factory
86
* @return The CacheExceptionHandler, or null if it could not be found.
88
public static CacheExceptionHandler createCacheExceptionHandler(
89
CacheConfiguration.CacheExceptionHandlerFactoryConfiguration factoryConfiguration) throws CacheException {
90
String className = null;
91
CacheExceptionHandler cacheExceptionHandler = null;
92
if (factoryConfiguration != null) {
93
className = factoryConfiguration.getFullyQualifiedClassPath();
95
if (className == null || className.length() == 0) {
96
LOG.debug("No CacheExceptionHandlerFactory class specified. Skipping...");
98
CacheExceptionHandlerFactory factory = (CacheExceptionHandlerFactory)
99
ClassLoaderUtil.createNewInstance(className);
100
Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(),
101
factoryConfiguration.getPropertySeparator());
102
return factory.createExceptionHandler(properties);
104
return cacheExceptionHandler;
109
* Tries to load the class specified otherwise defaults to null
111
* @return a map of CacheManagerPeerProviders
113
public Map<String, CacheManagerPeerProvider> createCachePeerProviders() {
114
String className = null;
115
Map<String, CacheManagerPeerProvider> cacheManagerPeerProviders = new HashMap<String, CacheManagerPeerProvider>();
116
List<FactoryConfiguration> cachePeerProviderFactoryConfiguration =
117
configuration.getCacheManagerPeerProviderFactoryConfiguration();
118
for (FactoryConfiguration factoryConfiguration : cachePeerProviderFactoryConfiguration) {
120
if (factoryConfiguration != null) {
121
className = factoryConfiguration.getFullyQualifiedClassPath();
123
if (className == null) {
124
LOG.debug("No CachePeerProviderFactoryConfiguration specified. Not configuring a CacheManagerPeerProvider.");
127
CacheManagerPeerProviderFactory cacheManagerPeerProviderFactory =
128
(CacheManagerPeerProviderFactory)
129
ClassLoaderUtil.createNewInstance(className);
130
Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(),
131
factoryConfiguration.getPropertySeparator());
132
CacheManagerPeerProvider cacheManagerPeerProvider =
133
cacheManagerPeerProviderFactory.createCachePeerProvider(cacheManager, properties);
134
cacheManagerPeerProviders.put(cacheManagerPeerProvider.getScheme(), cacheManagerPeerProvider);
138
return cacheManagerPeerProviders;
142
* Tries to load the class specified otherwise defaults to null
144
public Map<String, CacheManagerPeerListener> createCachePeerListeners() {
145
String className = null;
146
Map<String, CacheManagerPeerListener> cacheManagerPeerListeners = new HashMap<String, CacheManagerPeerListener>();
147
List<FactoryConfiguration> cacheManagerPeerListenerFactoryConfigurations =
148
configuration.getCacheManagerPeerListenerFactoryConfigurations();
149
boolean first = true;
150
for (FactoryConfiguration factoryConfiguration : cacheManagerPeerListenerFactoryConfigurations) {
152
if (factoryConfiguration != null) {
153
className = factoryConfiguration.getFullyQualifiedClassPath();
155
if (className == null) {
156
LOG.debug("No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener.");
159
CacheManagerPeerListenerFactory cacheManagerPeerListenerFactory = (CacheManagerPeerListenerFactory)
160
ClassLoaderUtil.createNewInstance(className);
161
Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(),
162
factoryConfiguration.getPropertySeparator());
163
CacheManagerPeerListener cacheManagerPeerListener =
164
cacheManagerPeerListenerFactory.createCachePeerListener(cacheManager, properties);
165
cacheManagerPeerListeners.put(cacheManagerPeerListener.getScheme(), cacheManagerPeerListener);
168
return cacheManagerPeerListeners;
172
* Tries to load the class specified.
174
* @return If there is none returns null.
176
public final CacheManagerEventListener createCacheManagerEventListener() throws CacheException {
177
String className = null;
178
FactoryConfiguration cacheManagerEventListenerFactoryConfiguration =
179
configuration.getCacheManagerEventListenerFactoryConfiguration();
180
if (cacheManagerEventListenerFactoryConfiguration != null) {
181
className = cacheManagerEventListenerFactoryConfiguration.getFullyQualifiedClassPath();
183
if (className == null || className.length() == 0) {
184
LOG.debug("No CacheManagerEventListenerFactory class specified. Skipping...");
187
CacheManagerEventListenerFactory factory = (CacheManagerEventListenerFactory)
188
ClassLoaderUtil.createNewInstance(className);
189
Properties properties = PropertyUtil.parseProperties(cacheManagerEventListenerFactoryConfiguration.properties,
190
cacheManagerEventListenerFactoryConfiguration.getPropertySeparator());
191
return factory.createCacheManagerEventListener(properties);
197
* @return the disk store path, or null if not set.
199
public final String getDiskStorePath() {
200
DiskStoreConfiguration diskStoreConfiguration = configuration.getDiskStoreConfiguration();
201
if (diskStoreConfiguration == null) {
204
return diskStoreConfiguration.getPath();
209
* @return the Default Cache
210
* @throws net.sf.ehcache.CacheException if there is no default cache
212
public final Ehcache createDefaultCache() throws CacheException {
213
CacheConfiguration cacheConfiguration = configuration.getDefaultCacheConfiguration();
214
if (cacheConfiguration == null) {
215
throw new CacheException("Illegal configuration. No default cache is configured.");
217
cacheConfiguration.name = Cache.DEFAULT_CACHE_NAME;
218
return createCache(cacheConfiguration);
223
* Creates unitialised caches for each cache configuration found
225
* @return an empty set if there are none,
227
public final Set createCaches() {
228
Set caches = new HashSet();
229
Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
230
for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
231
Map.Entry entry = (Map.Entry) iterator.next();
232
CacheConfiguration cacheConfiguration = (CacheConfiguration) entry.getValue();
233
cacheConfiguration.setDefaultTransactionManager(configuration.getDefaultTransactionManager());
234
Ehcache cache = createCache(cacheConfiguration);
242
* Calculates the number of caches in the configuration that overflow to disk
244
public final Integer numberOfCachesThatOverflowToDisk() {
246
Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
247
for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
248
Map.Entry entry = (Map.Entry) iterator.next();
249
CacheConfiguration cacheConfiguration = (CacheConfiguration) entry.getValue();
250
if (cacheConfiguration.overflowToDisk) {
254
return Integer.valueOf(count);
259
* Calculates the number of caches in the configuration that are diskPersistent
261
public final Integer numberOfCachesThatAreDiskPersistent() {
263
Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
264
for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
265
Map.Entry entry = (Map.Entry) iterator.next();
266
CacheConfiguration cacheConfiguration = (CacheConfiguration) entry.getValue();
267
if (cacheConfiguration.isDiskPersistent()) {
271
return Integer.valueOf(count);
275
* Creates a cache from configuration where the configuration cache name matches the given name
277
* @return the cache, or null if there is no match
279
final Ehcache createCacheFromName(String name) {
280
CacheConfiguration cacheConfiguration = null;
281
Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
282
for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
283
Map.Entry entry = (Map.Entry) iterator.next();
284
CacheConfiguration cacheConfigurationCandidate = (CacheConfiguration) entry.getValue();
285
if (cacheConfigurationCandidate.name.equals(name)) {
286
cacheConfiguration = cacheConfigurationCandidate;
290
if (cacheConfiguration == null) {
293
return createCache(cacheConfiguration);
298
* Create a cache given a cache configuration
300
* @param cacheConfiguration
302
final Ehcache createCache(CacheConfiguration cacheConfiguration) {
303
Ehcache cache = new Cache(cacheConfiguration.clone(), null, null);
304
cache = applyCacheExceptionHandler(cacheConfiguration, cache);
308
private Ehcache applyCacheExceptionHandler(CacheConfiguration cacheConfiguration, Ehcache cache) {
309
CacheExceptionHandler cacheExceptionHandler =
310
createCacheExceptionHandler(cacheConfiguration.getCacheExceptionHandlerFactoryConfiguration());
311
cache.setCacheExceptionHandler(cacheExceptionHandler);
313
if (cache.getCacheExceptionHandler() != null) {
314
return ExceptionHandlingDynamicCacheProxy.createProxy(cache);
320
* Creates decorated ehcaches for the cache, if any configured in ehcache.xml
322
* @param cache the cache
323
* @return List of the decorated ehcaches, if any configured in ehcache.xml otherwise returns empty list
325
public List<Ehcache> createCacheDecorators(Ehcache cache) {
326
CacheConfiguration cacheConfiguration = cache.getCacheConfiguration();
327
if (cacheConfiguration == null) {
328
return Collections.emptyList();
330
List<CacheDecoratorFactoryConfiguration> cacheDecoratorConfigurations = cacheConfiguration.getCacheDecoratorConfigurations();
331
if (cacheDecoratorConfigurations == null || cacheDecoratorConfigurations.size() == 0) {
332
LOG.debug("CacheDecoratorFactory not configured. Skipping...");
333
return Collections.emptyList();
335
List<Ehcache> result = new ArrayList<Ehcache>();
336
for (CacheDecoratorFactoryConfiguration factoryConfiguration : cacheDecoratorConfigurations) {
337
Ehcache decoratedCache = createDecoratedCache(cache, factoryConfiguration);
338
if (decoratedCache != null) {
339
result.add(decoratedCache);
346
* Creates the decorated cache from the decorator config specified. Returns null if the name of the factory class is not specified
348
private static Ehcache createDecoratedCache(Ehcache cache,
349
CacheConfiguration.CacheDecoratorFactoryConfiguration factoryConfiguration) {
350
if (factoryConfiguration == null) {
353
String className = factoryConfiguration.getFullyQualifiedClassPath();
354
if (className == null) {
355
LOG.debug("CacheDecoratorFactory was specified without the name of the factory. Skipping...");
358
CacheDecoratorFactory factory = (CacheDecoratorFactory) ClassLoaderUtil.createNewInstance(className);
359
Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(),
360
factoryConfiguration.getPropertySeparator());
361
return factory.createDecoratedEhcache(cache, properties);
366
* @return the Configuration used
368
public final Configuration getConfigurationBean() {
369
return configuration;