View Javadoc

1   /**
2    *  Copyright 2003-2006 Greg Luck
3    *
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
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  
17  package net.sf.ehcache.config;
18  
19  import net.sf.ehcache.Cache;
20  import net.sf.ehcache.CacheManager;
21  import net.sf.ehcache.CacheException;
22  import net.sf.ehcache.distribution.CacheManagerPeerProviderFactory;
23  import net.sf.ehcache.distribution.CacheManagerPeerProvider;
24  import net.sf.ehcache.distribution.CacheManagerPeerListener;
25  import net.sf.ehcache.distribution.CacheManagerPeerListenerFactory;
26  import net.sf.ehcache.event.CacheEventListener;
27  import net.sf.ehcache.event.CacheEventListenerFactory;
28  import net.sf.ehcache.event.RegisteredEventListeners;
29  import net.sf.ehcache.event.CacheManagerEventListener;
30  import net.sf.ehcache.event.CacheManagerEventListenerFactory;
31  import net.sf.ehcache.util.ClassLoaderUtil;
32  import net.sf.ehcache.util.PropertyUtil;
33  
34  import java.util.Properties;
35  import java.util.Map;
36  import java.util.List;
37  import java.util.Set;
38  import java.util.Iterator;
39  import java.util.HashSet;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * The configuration for ehcache.
46   * <p/>
47   * This class can be populated through:
48   * <ul>
49   * <li>introspection by {@link ConfigurationFactory} or
50   * <li>programmatically
51   * </ul>
52   *
53   * @author Greg Luck
54   * @version $Id: ConfigurationHelper.java 52 2006-04-24 14:50:03Z gregluck $
55   */
56  public final class ConfigurationHelper {
57  
58      private static final Log LOG = LogFactory.getLog(ConfigurationHelper.class.getName());
59  
60      private Configuration configuration;
61      private CacheManager cacheManager;
62  
63      /**
64       * Only Constructor
65       *
66       * @param cacheManager
67       * @param configuration
68       */
69      public ConfigurationHelper(CacheManager cacheManager, Configuration configuration) {
70          if (cacheManager == null || configuration == null) {
71              throw new IllegalArgumentException("Cannot have null parameters");
72          }
73          this.cacheManager = cacheManager;
74          this.configuration = configuration;
75      }
76  
77  
78      /**
79       * A factory method to create a RegisteredEventListeners
80       */
81      protected static void registerCacheListeners(CacheConfiguration cacheConfiguration,
82                                                  RegisteredEventListeners registeredEventListeners) {
83          List cacheEventListenerConfigurations = cacheConfiguration.cacheEventListenerConfigurations;
84          for (int i = 0; i < cacheEventListenerConfigurations.size(); i++) {
85              CacheConfiguration.CacheEventListenerFactoryConfiguration factoryConfiguration =
86                      (CacheConfiguration.CacheEventListenerFactoryConfiguration) cacheEventListenerConfigurations.get(i);
87              CacheEventListener cacheEventListener = createCacheEventListener(factoryConfiguration);
88              registeredEventListeners.registerListener(cacheEventListener);
89          }
90      }
91  
92  
93      /**
94       * Tries to load the class specified otherwise defaults to null
95       *
96       * @param factoryConfiguration
97       */
98      private static CacheEventListener createCacheEventListener(
99              CacheConfiguration.CacheEventListenerFactoryConfiguration factoryConfiguration) {
100         String className = null;
101         CacheEventListener cacheEventListener = null;
102         try {
103             className = factoryConfiguration.getFullyQualifiedClassPath();
104         } catch (Throwable t) {
105             //
106         }
107         if (className == null) {
108             LOG.error("CacheEventListener factory not configured. Skipping listener configuration");
109         } else {
110             CacheEventListenerFactory factory = (CacheEventListenerFactory)
111                     ClassLoaderUtil.createNewInstance(className);
112             Properties properties = PropertyUtil.parseProperties(factoryConfiguration.getProperties());
113             cacheEventListener = factory.createCacheEventListener(properties);
114         }
115         return cacheEventListener;
116     }
117 
118     /**
119      * Tries to load the class specified otherwise defaults to null
120      */
121     public final CacheManagerPeerProvider createCachePeerProvider() {
122         String className = null;
123         FactoryConfiguration cachePeerProviderFactoryConfiguration =
124                 configuration.getCacheManagerPeerProviderFactoryConfiguration();
125         try {
126             className = cachePeerProviderFactoryConfiguration.fullyQualifiedClassPath;
127         } catch (Throwable t) {
128             //
129         }
130         if (className == null) {
131             LOG.debug("No CachePeerProviderFactoryConfiguration specified. Not configuring a CacheManagerPeerProvider.");
132             return null;
133         } else {
134             CacheManagerPeerProviderFactory cacheManagerPeerProviderFactory =
135                     (CacheManagerPeerProviderFactory)
136                             ClassLoaderUtil.createNewInstance(className);
137             Properties properties = PropertyUtil.parseProperties(cachePeerProviderFactoryConfiguration.properties);
138             return cacheManagerPeerProviderFactory.createCachePeerProvider(cacheManager, properties);
139         }
140     }
141 
142     /**
143      * Tries to load the class specified otherwise defaults to null
144      */
145     public final CacheManagerPeerListener createCachePeerListener() {
146         String className = null;
147         FactoryConfiguration cachePeerListenerFactoryConfiguration =
148                 configuration.getCacheManagerPeerListenerFactoryConfiguration();
149         try {
150             className = cachePeerListenerFactoryConfiguration.fullyQualifiedClassPath;
151         } catch (Throwable t) {
152             //
153         }
154         if (className == null) {
155             LOG.debug("No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener.");
156             return null;
157         } else {
158             CacheManagerPeerListenerFactory cacheManagerPeerListenerFactory = (CacheManagerPeerListenerFactory)
159                     ClassLoaderUtil.createNewInstance(className);
160             Properties properties = PropertyUtil.parseProperties(cachePeerListenerFactoryConfiguration.properties);
161             return cacheManagerPeerListenerFactory.createCachePeerListener(cacheManager, properties);
162         }
163     }
164 
165     /**
166      * Tries to load the class specified.
167      *
168      * @return If there is none returns null.
169      */
170     public final CacheManagerEventListener createCacheManagerEventListener() throws CacheException {
171         String className = null;
172         FactoryConfiguration cacheManagerEventListenerFactoryConfiguration =
173                 configuration.getCacheManagerEventListenerFactoryConfiguration();
174         try {
175             className = cacheManagerEventListenerFactoryConfiguration.fullyQualifiedClassPath;
176         } catch (Throwable t) {
177             //No class created because the config was missing
178         }
179         if (className == null || className.length() == 0) {
180             LOG.debug("No CacheManagerEventListenerFactory class specified. Skipping...");
181             return null;
182         } else {
183             CacheManagerEventListenerFactory factory = (CacheManagerEventListenerFactory)
184                     ClassLoaderUtil.createNewInstance(className);
185             Properties properties = PropertyUtil.parseProperties(cacheManagerEventListenerFactoryConfiguration.properties);
186             return factory.createCacheManagerEventListener(properties);
187         }
188     }
189 
190     /**
191      * @return the disk store path, or null if not set.
192      */
193     public final String getDiskStorePath() {
194         DiskStoreConfiguration diskStoreConfiguration = configuration.getDiskStoreConfiguration();
195         if (diskStoreConfiguration == null) {
196             return null;
197         } else {
198             return diskStoreConfiguration.getPath();
199         }
200     }
201 
202     /**
203      * @return the Default Cache
204      * @throws net.sf.ehcache.CacheException if there is no default cache
205      */
206     public final Cache createDefaultCache() throws CacheException {
207         CacheConfiguration cacheConfiguration = configuration.getDefaultCacheConfiguration();
208         if (cacheConfiguration == null) {
209             throw new CacheException("Illegal configuration. No default cache is configured.");
210         } else {
211             cacheConfiguration.name = Cache.DEFAULT_CACHE_NAME;
212             return createCache(cacheConfiguration);
213         }
214     }
215 
216     /**
217      * Creates unitialised caches for each cache configuration found
218      *
219      * @return an empty set if there are none,
220      */
221     public final Set createCaches() {
222         Set caches = new HashSet();
223         Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
224         for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
225             Map.Entry entry = (Map.Entry) iterator.next();
226             CacheConfiguration cacheConfiguration = (CacheConfiguration) entry.getValue();
227             Cache cache = createCache(cacheConfiguration);
228             caches.add(cache);
229         }
230         return caches;
231     }
232 
233     /**
234      * Creates a cache from configuration where the configuration cache name matches the given name
235      *
236      * @return the cache, or null if there is no match
237      */
238     final Cache createCacheFromName(String name) {
239         CacheConfiguration cacheConfiguration = null;
240         Set cacheConfigurations = configuration.getCacheConfigurations().entrySet();
241         for (Iterator iterator = cacheConfigurations.iterator(); iterator.hasNext();) {
242             Map.Entry entry = (Map.Entry) iterator.next();
243             CacheConfiguration cacheConfigurationCandidate = (CacheConfiguration) entry.getValue();
244             if (cacheConfigurationCandidate.name.equals(name)) {
245                 cacheConfiguration = cacheConfigurationCandidate;
246                 break;
247             }
248         }
249         if (cacheConfiguration == null) {
250             return null;
251         } else {
252             return createCache(cacheConfiguration);
253         }
254     }
255 
256     /**
257      * Create a cache given a cache configuration
258      *
259      * @param cacheConfiguration
260      */
261     final Cache createCache(CacheConfiguration cacheConfiguration) {
262         Cache cache = new Cache(cacheConfiguration.name,
263                 cacheConfiguration.maxElementsInMemory,
264                 cacheConfiguration.memoryStoreEvictionPolicy,
265                 cacheConfiguration.overflowToDisk,
266                 getDiskStorePath(),
267                 cacheConfiguration.eternal,
268                 cacheConfiguration.timeToLiveSeconds,
269                 cacheConfiguration.timeToIdleSeconds,
270                 cacheConfiguration.diskPersistent,
271                 cacheConfiguration.diskExpiryThreadIntervalSeconds,
272                 null);
273         RegisteredEventListeners listeners = cache.getCacheEventNotificationService();
274         registerCacheListeners(cacheConfiguration, listeners);
275         return cache;
276     }
277 
278     /**
279      * @return the Configuration used
280      */
281     public final Configuration getConfigurationBean() {
282         return configuration;
283     }
284 }