1 package org.apache.jcs.auxiliary.disk.indexed;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Enumeration;
23 import java.util.Hashtable;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.jcs.auxiliary.AuxiliaryCache;
28 import org.apache.jcs.auxiliary.AuxiliaryCacheManager;
29 import org.apache.jcs.engine.behavior.ICache;
30
31 /***
32 * Cache manager for IndexedDiskCaches. This manages the instances of the disk
33 * cache.
34 */
35 public class IndexedDiskCacheManager
36 implements AuxiliaryCacheManager
37 {
38 private static final long serialVersionUID = -4153287154512274626L;
39
40 private final static Log log = LogFactory.getLog( IndexedDiskCacheManager.class );
41
42 private static int clients;
43
44 private static IndexedDiskCacheManager instance;
45
46 private Hashtable caches = new Hashtable();
47
48 private IndexedDiskCacheAttributes defaultCacheAttributes;
49
50 /***
51 * Constructor for the IndexedDiskCacheManager object
52 * <p>
53 * @param defaultCacheAttributes
54 * Default attributes for caches managed by the instance.
55 */
56 private IndexedDiskCacheManager( IndexedDiskCacheAttributes defaultCacheAttributes )
57 {
58 this.defaultCacheAttributes = defaultCacheAttributes;
59 }
60
61 /***
62 * Gets the singleton instance of the manager
63 * <p>
64 * @param defaultCacheAttributes
65 * If the instance has not yet been created, it will be
66 * initialized with this set of default attributes.
67 * @return The instance value
68 */
69 public static IndexedDiskCacheManager getInstance( IndexedDiskCacheAttributes defaultCacheAttributes )
70 {
71 synchronized ( IndexedDiskCacheManager.class )
72 {
73 if ( instance == null )
74 {
75 instance = new IndexedDiskCacheManager( defaultCacheAttributes );
76 }
77 }
78
79 clients++;
80
81 return instance;
82 }
83
84 /***
85 * Gets an IndexedDiskCache for the supplied name using the default
86 * attributes.
87 * <p>
88 * @param cacheName
89 * Name that will be used when creating attributes.
90 * @return A cache.
91 */
92 public AuxiliaryCache getCache( String cacheName )
93 {
94 IndexedDiskCacheAttributes cacheAttributes = (IndexedDiskCacheAttributes) defaultCacheAttributes.copy();
95
96 cacheAttributes.setCacheName( cacheName );
97
98 return getCache( cacheAttributes );
99 }
100
101 /***
102 * Get an IndexedDiskCache for the supplied attributes. Will provide an
103 * existing cache for the name attribute if one has been created, or will
104 * create a new cache.
105 * <p>
106 * @param cacheAttributes
107 * Attributes the cache should have.
108 * @return A cache, either from the existing set or newly created.
109 */
110 public AuxiliaryCache getCache( IndexedDiskCacheAttributes cacheAttributes )
111 {
112 AuxiliaryCache cache = null;
113
114 String cacheName = cacheAttributes.getCacheName();
115
116 log.debug( "Getting cache named: " + cacheName );
117
118 synchronized ( caches )
119 {
120
121
122
123 cache = (AuxiliaryCache) caches.get( cacheName );
124
125
126
127
128 if ( cache == null )
129 {
130 cache = new IndexedDiskCache( cacheAttributes );
131
132 caches.put( cacheName, cache );
133 }
134 }
135
136 return cache;
137 }
138
139 /***
140 * Disposes the cache with the given name, if found in the set of managed
141 * caches.
142 * <p>
143 * @param cacheName
144 * Name of cache to dispose.
145 */
146 public void freeCache( String cacheName )
147 {
148 ICache cache = (ICache) caches.get( cacheName );
149
150 if ( cache != null )
151 {
152 try
153 {
154 cache.dispose();
155 }
156 catch ( Exception e )
157 {
158 log.error( "Failure disposing cache: " + cacheName, e );
159 }
160 }
161 }
162
163 /***
164 * Gets the cacheType attribute of the DiskCacheManager object
165 * <p>
166 * @return The cacheType value
167 */
168 public int getCacheType()
169 {
170 return DISK_CACHE;
171 }
172
173 /***
174 * Releases the cache manager instance. When all clients have released the
175 * cache manager, all contained caches will be disposed.
176 */
177 public void release()
178 {
179 clients--;
180
181 if ( --clients != 0 )
182 {
183 return;
184 }
185
186 synchronized ( caches )
187 {
188 Enumeration allCaches = caches.elements();
189
190 while ( allCaches.hasMoreElements() )
191 {
192 ICache cache = (ICache) allCaches.nextElement();
193
194 if ( cache != null )
195 {
196 try
197 {
198 cache.dispose();
199 }
200 catch ( Exception e )
201 {
202 log.error( "Failure disposing cache: " + cache.getCacheName(), e );
203 }
204 }
205 }
206 }
207 }
208 }