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