| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.jcs.auxiliary.disk.block.BlockDiskCacheManager |
|
|
| 1 | package org.apache.jcs.auxiliary.disk.block; |
|
| 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 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 | 32 | 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 | 16 | 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 | 16 | { |
| 64 | 16 | this.defaultCacheAttributes = defaultCacheAttributes; |
| 65 | 16 | } |
| 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 | 96 | synchronized ( BlockDiskCacheManager.class ) |
| 78 | { |
|
| 79 | 96 | if ( instance == null ) |
| 80 | { |
|
| 81 | 16 | instance = new BlockDiskCacheManager( defaultCacheAttributes ); |
| 82 | } |
|
| 83 | 96 | } |
| 84 | ||
| 85 | 96 | clients++; |
| 86 | ||
| 87 | 96 | 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 | 0 | BlockDiskCacheAttributes cacheAttributes = (BlockDiskCacheAttributes) defaultCacheAttributes.copy(); |
| 101 | ||
| 102 | 0 | cacheAttributes.setCacheName( cacheName ); |
| 103 | ||
| 104 | 0 | 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 | 96 | AuxiliaryCache cache = null; |
| 119 | ||
| 120 | 96 | String cacheName = cacheAttributes.getCacheName(); |
| 121 | ||
| 122 | 96 | log.debug( "Getting cache named: " + cacheName ); |
| 123 | ||
| 124 | 96 | synchronized ( caches ) |
| 125 | { |
|
| 126 | // Try to load the cache from the set that have already been |
|
| 127 | // created. This only looks at the name attribute. |
|
| 128 | ||
| 129 | 96 | cache = (AuxiliaryCache) caches.get( cacheName ); |
| 130 | ||
| 131 | // If it was not found, create a new one using the supplied |
|
| 132 | // attributes |
|
| 133 | ||
| 134 | 96 | if ( cache == null ) |
| 135 | { |
|
| 136 | 96 | cache = new BlockDiskCache( cacheAttributes ); |
| 137 | ||
| 138 | 96 | caches.put( cacheName, cache ); |
| 139 | } |
|
| 140 | 96 | } |
| 141 | ||
| 142 | 96 | 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 | 0 | ICache cache = (ICache) caches.get( cacheName ); |
| 155 | ||
| 156 | 0 | if ( cache != null ) |
| 157 | { |
|
| 158 | try |
|
| 159 | { |
|
| 160 | 0 | cache.dispose(); |
| 161 | } |
|
| 162 | 0 | catch ( Exception e ) |
| 163 | { |
|
| 164 | 0 | log.error( "Failure disposing cache: " + cacheName, e ); |
| 165 | 0 | } |
| 166 | } |
|
| 167 | 0 | } |
| 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 | 0 | 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 | 0 | clients--; |
| 186 | ||
| 187 | 0 | if ( --clients != 0 ) |
| 188 | { |
|
| 189 | 0 | return; |
| 190 | } |
|
| 191 | ||
| 192 | 0 | synchronized ( caches ) |
| 193 | { |
|
| 194 | 0 | Enumeration allCaches = caches.elements(); |
| 195 | ||
| 196 | 0 | while ( allCaches.hasMoreElements() ) |
| 197 | { |
|
| 198 | 0 | ICache cache = (ICache) allCaches.nextElement(); |
| 199 | ||
| 200 | 0 | if ( cache != null ) |
| 201 | { |
|
| 202 | try |
|
| 203 | { |
|
| 204 | 0 | cache.dispose(); |
| 205 | } |
|
| 206 | 0 | catch ( Exception e ) |
| 207 | { |
|
| 208 | 0 | log.error( "Failure disposing cache: " + cache.getCacheName(), e ); |
| 209 | 0 | } |
| 210 | } |
|
| 211 | 0 | } |
| 212 | 0 | } |
| 213 | 0 | } |
| 214 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |