KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > CacheManagerFactory


1 package org.apache.slide.util;
2
3 import java.net.URL JavaDoc;
4
5 import net.sf.ehcache.CacheException;
6 import net.sf.ehcache.CacheManager;
7
8 /**
9  * A factory for obtaining ehcache CacheManagers. This simplifies the process
10  * of obtaining a CacheManager.
11  *
12  */

13 public class CacheManagerFactory {
14     
15     private static CacheManager defaultCacheManager;
16     private static HashMap cacheManagers = new HashMap();
17
18     private CacheManagerFactory() {}
19     
20     /**
21      * Retrieves the default CacheManager using the default configuration file.
22      *
23      * @return the default CacheManager.
24      * @throws CacheException
25      */

26     public static CacheManager getDefaultCacheManager() throws CacheException {
27         if ( defaultCacheManager == null ) {
28             defaultCacheManager = CacheManager.create();
29         }
30         return defaultCacheManager;
31     }
32     
33     /**
34      * Creates a CacheManager using the configuration file at configFilePath, then adds
35      * the CacheManager to an internal Map. Subsequent requests for a CacheManager with
36      * the same configFilePath returns the CacheManager from the Map.
37      *
38      * @param configFilePath path to the configuration file to use.
39      * @return a CacheManager
40      * @throws CacheException
41      */

42     public static CacheManager getCacheManager( String JavaDoc configFilePath ) throws CacheException {
43         CacheManager cm = (CacheManager)cacheManagers.get( configFilePath );
44         if ( cm == null ) {
45             cm = CacheManager.create( configFilePath );
46         }
47         return cm;
48     }
49     
50     /**
51      * Creates a CacheManager using the configuration file at configFileURL, then adds
52      * the CacheManager to an internal Map. Subsequent requests for a CacheManager with
53      * the same configFileURL returns the CacheManager from the Map.
54      *
55      * @param configFileURL URL to the configuration file to use.
56      * @return a CacheManager
57      * @throws CacheException
58      */

59     public static CacheManager getCacheManager( URL JavaDoc configFileURL ) throws CacheException {
60         CacheManager cm = (CacheManager)cacheManagers.get( configFileURL );
61         if ( cm == null ) {
62             cm = CacheManager.create( configFileURL );
63         }
64         return cm;
65     }
66
67 }
68
Popular Tags