Coverage Report - org.jtheque.core.managers.cache.CacheManager
 
Classes in this File Line Coverage Branch Coverage Complexity
CacheManager
0 %
0/21
N/A
1.125
 
 1  
 package org.jtheque.core.managers.cache;
 2  
 
 3  
 import net.sf.ehcache.Cache;
 4  
 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
 5  
 import org.jtheque.core.managers.AbstractActivableManager;
 6  
 import org.jtheque.core.managers.Managers;
 7  
 import org.jtheque.core.managers.log.ILoggingManager;
 8  
 
 9  
 import java.io.File;
 10  
 import java.lang.reflect.Field;
 11  
 
 12  
 /*
 13  
  * This file is part of JTheque.
 14  
  *
 15  
  * JTheque is free software: you can redistribute it and/or modify
 16  
  * it under the terms of the GNU General Public License as published by
 17  
  * the Free Software Foundation, either version 3 of the License.
 18  
  *
 19  
  * JTheque is distributed in the hope that it will be useful,
 20  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 21  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 22  
  * GNU General Public License for more details.
 23  
  *
 24  
  * You should have received a copy of the GNU General Public License
 25  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 26  
  */
 27  
 
 28  
 /**
 29  
  * A cache manager implementation.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  0
 public final class CacheManager extends AbstractActivableManager implements ICacheManager {
 34  
     private static final int TWO_MINUTES = 120;
 35  
 
 36  
     @Override
 37  
     public void preInit() {
 38  0
         net.sf.ehcache.CacheManager manager = net.sf.ehcache.CacheManager.create();
 39  
 
 40  0
         setDiskStorePath(manager, Managers.getCore().getFolders().getCacheFolder());
 41  0
     }
 42  
 
 43  
     @Override
 44  
     public void close(){
 45  0
         getCacheManager().shutdown();
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Return the EhCache CacheManager instance.
 50  
      *
 51  
      * @return The EhCache CacheManager instance.
 52  
      */
 53  
     private static net.sf.ehcache.CacheManager getCacheManager() {
 54  0
         return net.sf.ehcache.CacheManager.getInstance();
 55  
     }
 56  
 
 57  
     @Override
 58  
     public void addCache(CacheConfiguration config) {
 59  0
         Cache cache = new Cache(
 60  
                 config.getName(),
 61  
                 config.getMaxElementsInMemory(),
 62  
                 MemoryStoreEvictionPolicy.LFU,
 63  
                 config.isOverflowToDisk(),
 64  
                 Managers.getCore().getFolders().getCacheFolder().getAbsolutePath(),
 65  
                 config.isEternal(),
 66  
                 config.getTimeToLiveSeconds(),
 67  
                 config.getTimeToIdleSeconds(),
 68  
                 config.isDiskPersistent(),
 69  
                 TWO_MINUTES,
 70  
                 null,
 71  
                 null,
 72  
                 config.getMaxElementsOnDisk(),
 73  
                 20
 74  
         );
 75  
 
 76  0
         getCacheManager().addCache(cache);
 77  0
     }
 78  
 
 79  
     @Override
 80  
     public Cache getCache(String name) {
 81  0
         return getCacheManager().getCache(name);
 82  
     }
 83  
 
 84  
     @Override
 85  
     public boolean cacheExists(String name) {
 86  0
         return getCacheManager().cacheExists(name);
 87  
     }
 88  
 
 89  
     @Override
 90  
     public void removeCache(String name) {
 91  0
         getCacheManager().removeCache(name);
 92  0
     }
 93  
 
 94  
     /**
 95  
      * Set the disk store path of the caches by reflection.
 96  
      *
 97  
      * @param manager     The manager to configure.
 98  
      * @param cacheFolder The folder of the caches.
 99  
      */
 100  
     private void setDiskStorePath(Object manager, File cacheFolder) {
 101  
         try {
 102  0
             Field m = manager.getClass().getDeclaredField("diskStorePath");
 103  
 
 104  0
             m.setAccessible(true);
 105  
 
 106  0
             m.set(manager, cacheFolder.getAbsolutePath());
 107  0
         } catch (Exception e) {
 108  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e, "Unable to set disk store path for cache");
 109  0
         }
 110  0
     }
 111  
 }