1 16 package net.sf.dozer.util.mapping.cache; 17 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import net.sf.dozer.util.mapping.MappingException; 25 import net.sf.dozer.util.mapping.exception.NotFoundException; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 33 public final class DozerCacheManager implements CacheManagerIF { 34 private static final Log log = LogFactory.getLog(DozerCacheManager.class); 35 private static DozerCacheManager singleton = createNew(); 36 private final Map cachesMap = new HashMap (); 37 38 43 public static DozerCacheManager createNew() { 44 return new DozerCacheManager(); 45 } 46 47 50 public static DozerCacheManager getInstance() { 51 return singleton; 52 } 53 54 private DozerCacheManager() { 55 } 56 57 public Set getCaches() { 58 return new HashSet (cachesMap.values()); 59 } 60 61 public Cache getCache(String name) { 62 Cache cache = (Cache)cachesMap.get(name); 63 if (cache == null) { 64 throw new NotFoundException("Unable to find cache with name: " + name); 65 } 66 return cache; 67 } 68 69 public void addCache(String name, long maxElementsInMemory) { 70 addCache(new Cache(name, maxElementsInMemory)); 71 } 72 73 public void addCache(Cache cache) { 74 synchronized(cachesMap) { 75 String name = cache.getName(); 76 if (cacheExists(name)) { 77 throw new MappingException("Cache already exists with name: " + name); 78 } 79 cachesMap.put(name, cache); 80 } 81 } 82 83 public Set getCacheNames() { 84 Set results = new HashSet (); 85 Iterator iter = cachesMap.entrySet().iterator(); 86 while (iter.hasNext()) { 87 Map.Entry entry = (Map.Entry )iter.next(); 88 results.add((String )entry.getKey()); 89 } 90 return results; 91 } 92 93 97 public void clearAllEntries() { 98 Iterator iter = cachesMap.values().iterator(); 99 while (iter.hasNext()){ 100 Cache cache = (Cache) iter.next(); 101 cache.clear(); 102 } 103 } 104 105 public boolean cacheExists(String name) { 106 return cachesMap.containsKey(name); 107 } 108 109 public void logCaches() { 110 log.info(getCaches()); 111 } 112 } 113 | Popular Tags |