1 package org.apache.jcs.auxiliary.disk.indexed;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.Serializable;
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.disk.AbstractDiskCacheManager;
28 import org.apache.jcs.engine.behavior.IElementSerializer;
29 import org.apache.jcs.engine.logging.behavior.ICacheEventLogger;
30
31 /**
32 * Cache manager for IndexedDiskCaches. This manages the instances of the disk cache.
33 */
34 public class IndexedDiskCacheManager
35 extends AbstractDiskCacheManager
36 {
37 /** Don't change */
38 private static final long serialVersionUID = -4153287154512274626L;
39
40 /** The logger */
41 private final static Log log = LogFactory.getLog( IndexedDiskCacheManager.class );
42
43 /** Singleton instance. */
44 private static IndexedDiskCacheManager instance;
45
46 /** Each region has an entry here. */
47 private final Hashtable<String, IndexedDiskCache<? extends Serializable, ? extends Serializable>> caches =
48 new Hashtable<String, IndexedDiskCache<? extends Serializable, ? extends Serializable>>();
49
50 /** User configurable attributes */
51 private final IndexedDiskCacheAttributes defaultCacheAttributes;
52
53 /**
54 * Constructor for the IndexedDiskCacheManager object
55 * <p>
56 * @param defaultCacheAttributes Default attributes for caches managed by the instance.
57 * @param cacheEventLogger
58 * @param elementSerializer
59 */
60 private IndexedDiskCacheManager( IndexedDiskCacheAttributes defaultCacheAttributes,
61 ICacheEventLogger cacheEventLogger, IElementSerializer elementSerializer )
62 {
63 this.defaultCacheAttributes = defaultCacheAttributes;
64 setElementSerializer( elementSerializer );
65 setCacheEventLogger( cacheEventLogger );
66 }
67
68 /**
69 * Gets the singleton instance of the manager
70 * <p>
71 * @param defaultCacheAttributes If the instance has not yet been created, it will be
72 * initialized with this set of default attributes.
73 * @param cacheEventLogger
74 * @param elementSerializer
75 * @return The instance value
76 */
77 public static IndexedDiskCacheManager getInstance( IndexedDiskCacheAttributes defaultCacheAttributes,
78 ICacheEventLogger cacheEventLogger,
79 IElementSerializer elementSerializer )
80 {
81 synchronized ( IndexedDiskCacheManager.class )
82 {
83 if ( instance == null )
84 {
85 instance = new IndexedDiskCacheManager( defaultCacheAttributes, cacheEventLogger, elementSerializer );
86 }
87 }
88 return instance;
89 }
90
91 /**
92 * Gets an IndexedDiskCache for the supplied name using the default attributes.
93 * <p>
94 * @param cacheName Name that will be used when creating attributes.
95 * @return A cache.
96 */
97 public <K extends Serializable, V extends Serializable> IndexedDiskCache<K, V> getCache( String cacheName )
98 {
99 IndexedDiskCacheAttributes cacheAttributes = (IndexedDiskCacheAttributes) defaultCacheAttributes.copy();
100
101 cacheAttributes.setCacheName( cacheName );
102
103 return getCache( cacheAttributes );
104 }
105
106 /**
107 * Get an IndexedDiskCache for the supplied attributes. Will provide an existing cache for the
108 * name attribute if one has been created, or will create a new cache.
109 * <p>
110 * @param cacheAttributes Attributes the cache should have.
111 * @return A cache, either from the existing set or newly created.
112 */
113 public <K extends Serializable, V extends Serializable> IndexedDiskCache<K, V> getCache( IndexedDiskCacheAttributes cacheAttributes )
114 {
115 IndexedDiskCache<K, V> cache = null;
116
117 String cacheName = cacheAttributes.getCacheName();
118
119 log.debug( "Getting cache named: " + cacheName );
120
121 synchronized ( caches )
122 {
123 // Try to load the cache from the set that have already been
124 // created. This only looks at the name attribute.
125
126 @SuppressWarnings("unchecked") // Need to cast because of common map for all caches
127 IndexedDiskCache<K, V> indexedDiskCache = (IndexedDiskCache<K, V>) caches.get( cacheName );
128 cache = indexedDiskCache;
129
130 // If it was not found, create a new one using the supplied
131 // attributes
132
133 if ( cache == null )
134 {
135 cache = new IndexedDiskCache<K, V>( cacheAttributes, getElementSerializer() );
136 cache.setCacheEventLogger( getCacheEventLogger() );
137 caches.put( cacheName, cache );
138 }
139 }
140
141 return cache;
142 }
143 }