Coverage Report - no.sesat.search.view.velocity.QuickResourceCacheImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
QuickResourceCacheImpl
0%
0/18
0%
0/2
0
 
 1  
 /* Copyright (2005-2007) Schibsted Søk AS
 2  
  * This file is part of SESAT.
 3  
  *
 4  
  *   SESAT is free software: you can redistribute it and/or modify
 5  
  *   it under the terms of the GNU Affero General Public License as published by
 6  
  *   the Free Software Foundation, either version 3 of the License, or
 7  
  *   (at your option) any later version.
 8  
  *
 9  
  *   SESAT is distributed in the hope that it will be useful,
 10  
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
  *   GNU Affero General Public License for more details.
 13  
  *
 14  
  *   You should have received a copy of the GNU Affero General Public License
 15  
  *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
 16  
  *
 17  
  * Created on May 11, 2007, 8:16:37 PM
 18  
  */
 19  
 package no.sesat.search.view.velocity;
 20  
 
 21  
 import org.apache.velocity.app.Velocity;
 22  
 import org.apache.velocity.runtime.RuntimeConstants;
 23  
 import org.apache.velocity.runtime.RuntimeServices;
 24  
 import org.apache.velocity.runtime.resource.Resource;
 25  
 import org.apache.velocity.runtime.resource.ResourceCache;
 26  
 
 27  
 import java.util.HashMap;
 28  
 import java.util.Iterator;
 29  
 import java.util.Map;
 30  
 import java.util.concurrent.locks.ReadWriteLock;
 31  
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 32  
 
 33  
 /**
 34  
  * A faster implementation of the resource cache than the default. The default implementation uses a fully synchronized
 35  
  * LRUMap.
 36  
  * <p> This implementation only supports an unbounded cache size and will throw <tt>IllegalArgumentException</tt> if
 37  
  * {@link org.apache.velocity.app.Velocity} is set to anything other than zero.
 38  
  *
 39  
  * @see org.apache.velocity.runtime.resource.ResourceCacheImpl
 40  
  *
 41  
  *
 42  
  */
 43  0
 public final class QuickResourceCacheImpl implements ResourceCache {
 44  
 
 45  
     /** Property name for controlling the initial size of the cache. */
 46  
     public static final String INITIAL_SIZE_PROPERTY = "resource.manager.quickcache.size";
 47  
 
 48  
     private Map<Object, Resource> cache;
 49  
 
 50  0
     private final ReadWriteLock lock = new ReentrantReadWriteLock();
 51  
     private static final String BOUNDED_CACHE_NOT_SUPPORTED =
 52  
             "This implementatation only supports an unbounded cache. Change impl using property ";
 53  
 
 54  
     /**
 55  
      * {@inheritDoc}
 56  
      */
 57  
     public void initialize(final RuntimeServices svc) {
 58  0
         if (svc.getInt(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE, 0) != 0) {
 59  0
             throw new IllegalArgumentException(BOUNDED_CACHE_NOT_SUPPORTED + Velocity.RESOURCE_MANAGER_CLASS);
 60  
         }
 61  
 
 62  
         // Writes are rare once SESAT has inititalized a site so a HashMap should be faster than a ConcurrentHashMap.
 63  0
         cache = new HashMap<Object, Resource>(svc.getInt(INITIAL_SIZE_PROPERTY, 100));
 64  0
     }
 65  
 
 66  
     /**
 67  
      * {@inheritDoc}
 68  
      */
 69  
     public Resource get(final Object o) {
 70  
         try {
 71  0
             lock.readLock().lock();
 72  0
             return cache.get(o);
 73  
         } finally {
 74  0
             lock.readLock().unlock();
 75  
         }
 76  
     }
 77  
 
 78  
     /**
 79  
      * {@inheritDoc}
 80  
      */
 81  
     public Resource put(final Object o, final Resource resource) {
 82  
         try {
 83  0
             lock.writeLock().lock();
 84  0
             return cache.put(o, resource);
 85  
         } finally {
 86  0
             lock.writeLock().unlock();
 87  
         }
 88  
     }
 89  
 
 90  
     /**
 91  
      * {@inheritDoc}
 92  
      */
 93  
     public Resource remove(final Object o) {
 94  
         try {
 95  0
             lock.writeLock().lock();
 96  0
             return cache.remove(o);
 97  
         } finally {
 98  0
             lock.writeLock().unlock();
 99  
         }
 100  
     }
 101  
 
 102  
     /**
 103  
      * {@inheritDoc}
 104  
      */
 105  
     public Iterator enumerateKeys() {
 106  
         try {
 107  0
             lock.readLock().lock();
 108  0
             return cache.keySet().iterator();
 109  
         } finally {
 110  0
             lock.readLock().unlock();
 111  
         }
 112  
     }
 113  
 }