| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.jcs.auxiliary.disk.indexed.IndexedDiskCacheManager |
|
|
| 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.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 | 116 | private final static Log log = LogFactory.getLog( IndexedDiskCacheManager.class ); |
| 41 | ||
| 42 | private static int clients; |
|
| 43 | ||
| 44 | private static IndexedDiskCacheManager instance; |
|
| 45 | ||
| 46 | 58 | 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 | { |
| 58 | 58 | this.defaultCacheAttributes = defaultCacheAttributes; |
| 59 | 58 | } |
| 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 | 209 | synchronized ( IndexedDiskCacheManager.class ) |
| 72 | { |
|
| 73 | 209 | if ( instance == null ) |
| 74 | { |
|
| 75 | 58 | instance = new IndexedDiskCacheManager( defaultCacheAttributes ); |
| 76 | } |
|
| 77 | 209 | } |
| 78 | ||
| 79 | 209 | clients++; |
| 80 | ||
| 81 | 209 | 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 | 0 | IndexedDiskCacheAttributes cacheAttributes = (IndexedDiskCacheAttributes) defaultCacheAttributes.copy(); |
| 95 | ||
| 96 | 0 | cacheAttributes.setCacheName( cacheName ); |
| 97 | ||
| 98 | 0 | 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 | 209 | AuxiliaryCache cache = null; |
| 113 | ||
| 114 | 209 | String cacheName = cacheAttributes.getCacheName(); |
| 115 | ||
| 116 | 209 | log.debug( "Getting cache named: " + cacheName ); |
| 117 | ||
| 118 | 209 | synchronized ( caches ) |
| 119 | { |
|
| 120 | // Try to load the cache from the set that have already been |
|
| 121 | // created. This only looks at the name attribute. |
|
| 122 | ||
| 123 | 209 | cache = (AuxiliaryCache) caches.get( cacheName ); |
| 124 | ||
| 125 | // If it was not found, create a new one using the supplied |
|
| 126 | // attributes |
|
| 127 | ||
| 128 | 209 | if ( cache == null ) |
| 129 | { |
|
| 130 | 209 | cache = new IndexedDiskCache( cacheAttributes ); |
| 131 | ||
| 132 | 209 | caches.put( cacheName, cache ); |
| 133 | } |
|
| 134 | 209 | } |
| 135 | ||
| 136 | 209 | 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 | 0 | ICache cache = (ICache) caches.get( cacheName ); |
| 149 | ||
| 150 | 0 | if ( cache != null ) |
| 151 | { |
|
| 152 | try |
|
| 153 | { |
|
| 154 | 0 | cache.dispose(); |
| 155 | } |
|
| 156 | 0 | catch ( Exception e ) |
| 157 | { |
|
| 158 | 0 | log.error( "Failure disposing cache: " + cacheName, e ); |
| 159 | 0 | } |
| 160 | } |
|
| 161 | 0 | } |
| 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 | 0 | 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 | 0 | clients--; |
| 180 | ||
| 181 | 0 | if ( --clients != 0 ) |
| 182 | { |
|
| 183 | 0 | return; |
| 184 | } |
|
| 185 | ||
| 186 | 0 | synchronized ( caches ) |
| 187 | { |
|
| 188 | 0 | Enumeration allCaches = caches.elements(); |
| 189 | ||
| 190 | 0 | while ( allCaches.hasMoreElements() ) |
| 191 | { |
|
| 192 | 0 | ICache cache = (ICache) allCaches.nextElement(); |
| 193 | ||
| 194 | 0 | if ( cache != null ) |
| 195 | { |
|
| 196 | try |
|
| 197 | { |
|
| 198 | 0 | cache.dispose(); |
| 199 | } |
|
| 200 | 0 | catch ( Exception e ) |
| 201 | { |
|
| 202 | 0 | log.error( "Failure disposing cache: " + cache.getCacheName(), e ); |
| 203 | 0 | } |
| 204 | } |
|
| 205 | 0 | } |
| 206 | 0 | } |
| 207 | 0 | } |
| 208 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |