001    package org.apache.jcs.auxiliary.disk.block;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.Serializable;
023    import java.util.Hashtable;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.jcs.auxiliary.disk.AbstractDiskCacheManager;
028    import org.apache.jcs.engine.behavior.IElementSerializer;
029    import org.apache.jcs.engine.logging.behavior.ICacheEventLogger;
030    
031    /**
032     * Cache manager for BlockDiskCaches. This manages the instances of the disk cache.
033     */
034    public class BlockDiskCacheManager
035        extends AbstractDiskCacheManager
036    {
037        /** Don't change */
038        private static final long serialVersionUID = -4153287154512274626L;
039    
040        /** The logger */
041        private final static Log log = LogFactory.getLog( BlockDiskCacheManager.class );
042    
043        /** The singleton instance */
044        private static BlockDiskCacheManager instance;
045    
046        /** block disks for a region. */
047        private final Hashtable<String, BlockDiskCache<? extends Serializable, ? extends Serializable>> caches =
048            new Hashtable<String, BlockDiskCache<? extends Serializable, ? extends Serializable>>();
049    
050        /** Attributes. */
051        private final BlockDiskCacheAttributes defaultCacheAttributes;
052    
053        /**
054         * Constructor for the BlockDiskCacheManager object
055         * <p>
056         * @param defaultCacheAttributes Default attributes for caches managed by the instance.
057         * @param cacheEventLogger
058         * @param elementSerializer
059         */
060        private BlockDiskCacheManager( BlockDiskCacheAttributes defaultCacheAttributes, ICacheEventLogger cacheEventLogger,
061                                       IElementSerializer elementSerializer )
062        {
063            this.defaultCacheAttributes = defaultCacheAttributes;
064            setElementSerializer( elementSerializer );
065            setCacheEventLogger( cacheEventLogger );
066        }
067    
068        /**
069         * Gets the singleton instance of the manager
070         * <p>
071         * @param defaultCacheAttributes If the instance has not yet been created, it will be
072         *            initialized with this set of default attributes.
073         * @param cacheEventLogger
074         * @param elementSerializer
075         * @return The instance value
076         */
077        public static BlockDiskCacheManager getInstance( BlockDiskCacheAttributes defaultCacheAttributes,
078                                                         ICacheEventLogger cacheEventLogger,
079                                                         IElementSerializer elementSerializer )
080        {
081            synchronized ( BlockDiskCacheManager.class )
082            {
083                if ( instance == null )
084                {
085                    instance = new BlockDiskCacheManager( defaultCacheAttributes, cacheEventLogger, elementSerializer );
086                }
087            }
088            return instance;
089        }
090    
091        /**
092         * Gets an BlockDiskCache for the supplied name using the default attributes.
093         * <p>
094         * @param cacheName Name that will be used when creating attributes.
095         * @return A cache.
096         */
097        public <K extends Serializable, V extends Serializable> BlockDiskCache<K, V> getCache( String cacheName )
098        {
099            BlockDiskCacheAttributes cacheAttributes = (BlockDiskCacheAttributes) defaultCacheAttributes.copy();
100    
101            cacheAttributes.setCacheName( cacheName );
102    
103            return getCache( cacheAttributes );
104        }
105    
106        /**
107         * Get an BlockDiskCache 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> BlockDiskCache<K, V> getCache( BlockDiskCacheAttributes cacheAttributes )
114        {
115            BlockDiskCache<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                BlockDiskCache<K, V> blockDiskCache = (BlockDiskCache<K, V>) caches.get( cacheName );
128                cache = blockDiskCache;
129    
130                // If it was not found, create a new one using the supplied
131                // attributes
132                if ( cache == null )
133                {
134                    cache = new BlockDiskCache<K, V>( cacheAttributes );
135                    cache.setCacheEventLogger( getCacheEventLogger() );
136                    cache.setElementSerializer( getElementSerializer() );
137                    caches.put( cacheName, cache );
138                }
139            }
140    
141            return cache;
142        }
143    }