1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.config;
18
19 import net.sf.ehcache.Cache;
20 import net.sf.ehcache.CacheException;
21 import net.sf.ehcache.CacheManager;
22 import net.sf.ehcache.Ehcache;
23 import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
24 import net.sf.ehcache.bootstrap.BootstrapCacheLoaderFactory;
25 import net.sf.ehcache.distribution.CacheManagerPeerListener;
26 import net.sf.ehcache.distribution.CacheManagerPeerListenerFactory;
27 import net.sf.ehcache.distribution.CacheManagerPeerProvider;
28 import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
29 import net.sf.ehcache.event.CacheEventListener;
30 import net.sf.ehcache.event.CacheEventListenerFactory;
31 import net.sf.ehcache.event.CacheManagerEventListener;
32 import net.sf.ehcache.event.CacheManagerEventListenerFactory;
33 import net.sf.ehcache.event.RegisteredEventListeners;
34 import net.sf.ehcache.util.ClassLoaderUtil;
35 import net.sf.ehcache.util.PropertyUtil;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import java.util.HashSet;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Properties;
44 import java.util.Set;
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public final class ConfigurationHelper {
59
60 private static final Log LOG = LogFactory.getLog(ConfigurationHelper.class.getName());
61
62 private Configuration configuration;
63 private CacheManager cacheManager;
64
65
66
67
68
69
70
71 public ConfigurationHelper(CacheManager cacheManager, Configuration configuration) {
72 if (cacheManager == null || configuration == null) {
73 throw new IllegalArgumentException("Cannot have null parameters");
74 }
75 this.cacheManager = cacheManager;
76 this.configuration = configuration;
77 }
78
79
80
81
82
83 protected static void registerCacheListeners(CacheConfiguration cacheConfiguration,
84 RegisteredEventListeners registeredEventListeners) {
85 List cacheEventListenerConfigurations = cacheConfiguration.cacheEventListenerConfigurations;
86 for (int i = 0; i < cacheEventListenerConfigurations.size(); i++) {
87 CacheConfiguration.CacheEventListenerFactoryConfiguration factoryConfiguration =
88 (CacheConfiguration.CacheEventListenerFactoryConfiguration) cacheEventListenerConfigurations.get(i);
89 CacheEventListener cacheEventListener = createCacheEventListener(factoryConfiguration);
90 registeredEventListeners.registerListener(cacheEventListener);
91 }
92 }
93
94
95
96
97
98
99
100 private static CacheEventListener createCacheEventListener(
101 CacheConfiguration.CacheEventListenerFactoryConfiguration factoryConfiguration) {
102 String className = null;
103 CacheEventListener cacheEventListener = null;
104 try {
105 className = factoryConfiguration.getFullyQualifiedClassPath();
106 } catch (Throwable t) {
107
108 }
109 if (className == null) {
110 LOG.debug("CacheEventListener factory not configured. Skipping...");
111 } else {
112 CacheEventListenerFactory factory = (CacheEventListenerFactory)
113 ClassLoaderUtil.createNewInstance(className);
114 Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(),
115 factoryConfiguration.getPropertySeparator());
116 cacheEventListener = factory.createCacheEventListener(properties);
117 }
118 return cacheEventListener;
119 }
120
121
122
123
124
125
126 public final BootstrapCacheLoader createBootstrapCacheLoader(
127 CacheConfiguration.BootstrapCacheLoaderFactoryConfiguration factoryConfiguration) throws CacheException {
128 String className = null;
129 BootstrapCacheLoader bootstrapCacheLoader = null;
130 try {
131 className = factoryConfiguration.fullyQualifiedClassPath;
132 } catch (Throwable t) {
133
134 }
135 if (className == null || className.length() == 0) {
136 LOG.debug("No BootstrapCacheLoaderFactory class specified. Skipping...");
137 } else {
138 BootstrapCacheLoaderFactory factory = (BootstrapCacheLoaderFactory)
139 ClassLoaderUtil.createNewInstance(className);
140 Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties(),
141 factoryConfiguration.getPropertySeparator());
142 return factory.createBootstrapCacheLoader(properties);
143 }
144 return bootstrapCacheLoader;
145 }
146
147
148
149
150
151 public final CacheManagerPeerProvider createCachePeerProvider() {
152 String className = null;
153 FactoryConfiguration cachePeerProviderFactoryConfiguration =
154 configuration.getCacheManagerPeerProviderFactoryConfiguration();
155 try {
156 className = cachePeerProviderFactoryConfiguration.fullyQualifiedClassPath;
157 } catch (Throwable t) {
158
159 }
160 if (className == null) {
161 LOG.debug("No CachePeerProviderFactoryConfiguration specified. Not configuring a CacheManagerPeerProvider.");
162 return null;
163 } else {
164 CacheManagerPeerProviderFactory cacheManagerPeerProviderFactory =
165 (CacheManagerPeerProviderFactory)
166 ClassLoaderUtil.createNewInstance(className);
167 Properties properties = PropertyUtil.parseProperties(cachePeerProviderFactoryConfiguration.getProperties(),
168 cachePeerProviderFactoryConfiguration.getPropertySeparator());
169 return cacheManagerPeerProviderFactory.createCachePeerProvider(cacheManager, properties);
170 }
171 }
172
173
174
175
176 public final CacheManagerPeerListener createCachePeerListener() {
177 String className = null;
178 FactoryConfiguration cachePeerListenerFactoryConfiguration =
179 configuration.getCacheManagerPeerListenerFactoryConfiguration();
180 try {
181 className = cachePeerListenerFactoryConfiguration.fullyQualifiedClassPath;
182 } catch (Throwable t) {
183
184 }
185 if (className == null) {
186 LOG.debug("No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener.");
187 return null;
188 } else {
189 CacheManagerPeerListenerFactory cacheManagerPeerListenerFactory = (CacheManagerPeerListenerFactory)
190 ClassLoaderUtil.createNewInstance(className);
191 Properties properties = PropertyUtil.parseProperties(cachePeerListenerFactoryConfiguration.getProperties(),
192 cachePeerListenerFactoryConfiguration.getPropertySeparator());
193 return cacheManagerPeerListenerFactory.createCachePeerListener(cacheManager, properties);
194 }
195 }
196
197
198
199
200
201
202 public final CacheManagerEventListener createCacheManagerEventListener() throws CacheException {
203 String className = null;
204 FactoryConfiguration cacheManagerEventListenerFactoryConfiguration =
205 configuration.getCacheManagerEventListenerFactoryConfiguration();
206 try {
207 className = cacheManagerEventListenerFactoryConfiguration.fullyQualifiedClassPath;
208 } catch (Throwable t) {
209
210 }
211 if (className == null || className.length() == 0) {
212 LOG.debug("No CacheManagerEventListenerFactory class specified. Skipping...");
213 return null;
214 } else {
215 CacheManagerEventListenerFactory factory = (CacheManagerEventListenerFactory)
216 ClassLoaderUtil.createNewInstance(className);
217 Properties properties = PropertyUtil.parseProperties(cacheManagerEventListenerFactoryConfiguration.properties,
218 cacheManagerEventListenerFactoryConfiguration.getPropertySeparator());
219 return factory.createCacheManagerEventListener(properties);
220 }
221 }
222
223
224
225
226
227 public final String getDiskStorePath() {
228 DiskStoreConfiguration diskStoreConfiguration = configuration.getDiskStoreConfiguration();
229 if (diskStoreConfiguration == null) {
230 return null;
231 } else {
232 return diskStoreConfiguration.getPath();
233 }
234 }
235
236
237
238
239
240 public final Ehcache createDefaultCache() throws CacheException {
241 CacheConfiguration cacheConfiguration = configuration.getDefaultCacheConfiguration();
242 if (cacheConfiguration == null) {
243 throw new CacheException("Illegal configuration. No default cache is configured.");
244 } else {
245 cacheConfiguration.name = Cache.DEFAULT_CACHE_NAME;
246 return createCache(cacheConfiguration);
247 }
248 }
249
250
251
252
253
254
255 public final Set createCaches() {
256 Set caches = new HashSet();
257 Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
258 for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
259 Map.Entry entry = (Map.Entry) iterator.next();
260 CacheConfiguration cacheConfiguration = (CacheConfiguration) entry.getValue();
261 Cache cache = createCache(cacheConfiguration);
262 caches.add(cache);
263 }
264 return caches;
265 }
266
267
268
269
270
271 public final Integer numberOfCachesThatOverflowToDisk() {
272 int count = 0;
273 Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
274 for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
275 Map.Entry entry = (Map.Entry) iterator.next();
276 CacheConfiguration cacheConfiguration = (CacheConfiguration) entry.getValue();
277 if (cacheConfiguration.overflowToDisk) {
278 count++;
279 }
280 }
281 return new Integer(count);
282 }
283
284
285
286
287
288
289 final Ehcache createCacheFromName(String name) {
290 CacheConfiguration cacheConfiguration = null;
291 Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
292 for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
293 Map.Entry entry = (Map.Entry) iterator.next();
294 CacheConfiguration cacheConfigurationCandidate = (CacheConfiguration) entry.getValue();
295 if (cacheConfigurationCandidate.name.equals(name)) {
296 cacheConfiguration = cacheConfigurationCandidate;
297 break;
298 }
299 }
300 if (cacheConfiguration == null) {
301 return null;
302 } else {
303 return createCache(cacheConfiguration);
304 }
305 }
306
307
308
309
310
311
312 final Cache createCache(CacheConfiguration cacheConfiguration) {
313 Cache cache = new Cache(cacheConfiguration.name,
314 cacheConfiguration.maxElementsInMemory,
315 cacheConfiguration.memoryStoreEvictionPolicy,
316 cacheConfiguration.overflowToDisk,
317 getDiskStorePath(),
318 cacheConfiguration.eternal,
319 cacheConfiguration.timeToLiveSeconds,
320 cacheConfiguration.timeToIdleSeconds,
321 cacheConfiguration.diskPersistent,
322 cacheConfiguration.diskExpiryThreadIntervalSeconds,
323 null,
324 null,
325 cacheConfiguration.maxElementsOnDisk,
326 cacheConfiguration.diskSpoolBufferSizeMB);
327 RegisteredEventListeners listeners = cache.getCacheEventNotificationService();
328 registerCacheListeners(cacheConfiguration, listeners);
329 BootstrapCacheLoader bootstrapCacheLoader = createBootstrapCacheLoader(
330 cacheConfiguration.bootstrapCacheLoaderFactoryConfiguration);
331 cache.setBootstrapCacheLoader(bootstrapCacheLoader);
332 return cache;
333 }
334
335
336
337
338 public final Configuration getConfigurationBean() {
339 return configuration;
340 }
341 }