1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.utils; 22 23 import java.io.Serializable; 24 import java.net.URL; 25 26 import net.sf.ehcache.Cache; 27 import net.sf.ehcache.CacheException; 28 import net.sf.ehcache.CacheManager; 29 import net.sf.ehcache.Element; 30 import net.sourceforge.atunes.Constants; 31 import net.sourceforge.atunes.model.IOSManager; 32 33 /** 34 * A cache using ehcache 35 * 36 * @author alex 37 * 38 */ 39 public abstract class AbstractCache { 40 41 private IOSManager osManager; 42 43 private String configFile; 44 45 private String cacheName; 46 47 private CacheManager cacheManager; 48 49 private Cache cache; 50 51 /** 52 * @param cacheName 53 */ setCacheName(final String cacheName)54 public void setCacheName(final String cacheName) { 55 this.cacheName = cacheName; 56 } 57 58 /** 59 * @param osManager 60 */ setOsManager(final IOSManager osManager)61 public void setOsManager(final IOSManager osManager) { 62 this.osManager = osManager; 63 } 64 65 /** 66 * @param configFile 67 */ setConfigFile(final String configFile)68 public void setConfigFile(final String configFile) { 69 this.configFile = configFile; 70 } 71 init(final IOSManager osManager, final URL settings)72 private void init(final IOSManager osManager, final URL settings) { 73 System.setProperty("ehcache.disk.store.dir", StringUtils.getString( 74 osManager.getUserConfigFolder(), osManager.getFileSeparator(), 75 Constants.CACHE_DIR)); 76 this.cacheManager = new CacheManager(settings); 77 } 78 getCache()79 private synchronized Cache getCache() { 80 if (this.cache == null) { 81 return getCacheManager().getCache(this.cacheName); 82 } 83 return this.cache; 84 } 85 getCacheManager()86 private synchronized CacheManager getCacheManager() { 87 if (this.cacheManager == null) { 88 Logger.debug("Initializing cache from file: ", this.configFile); 89 init(this.osManager, 90 AbstractCache.class.getResource(this.configFile)); 91 } 92 return this.cacheManager; 93 } 94 dispose()95 protected void dispose() { 96 getCache().dispose(); 97 } 98 flush()99 protected void flush() { 100 getCache().flush(); 101 } 102 put(final Element element)103 protected void put(final Element element) { 104 getCache().put(element); 105 } 106 get(final Serializable key)107 protected Element get(final Serializable key) { 108 return getCache().get(key); 109 } 110 removeAll()111 protected boolean removeAll() { 112 boolean exception = false; 113 try { 114 getCache().removeAll(); 115 } catch (IllegalStateException e) { 116 Logger.info("Could not delete all files from ", this.cacheName, 117 " cache"); 118 exception = true; 119 } catch (CacheException e) { 120 Logger.info("Could not delete all files from ", this.cacheName, 121 " cache"); 122 exception = true; 123 } 124 return exception; 125 } 126 } 127