| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.cache; |
| 17 | |
|
| 18 | |
import org.dozer.util.MappingUtils; |
| 19 | |
import org.slf4j.LoggerFactory; |
| 20 | |
import org.slf4j.Logger; |
| 21 | |
|
| 22 | |
import java.util.Collection; |
| 23 | |
import java.util.HashMap; |
| 24 | |
import java.util.HashSet; |
| 25 | |
import java.util.Map; |
| 26 | |
import java.util.Map.Entry; |
| 27 | |
import java.util.Set; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 1144 | public final class DozerCacheManager implements CacheManager { |
| 35 | |
|
| 36 | 1144 | private final Logger log = LoggerFactory.getLogger(DozerCacheManager.class); |
| 37 | |
|
| 38 | 1144 | private final Map<String, Cache> cachesMap = new HashMap<String, Cache>(); |
| 39 | |
|
| 40 | |
public Collection<Cache> getCaches() { |
| 41 | 1 | return new HashSet<Cache>(cachesMap.values()); |
| 42 | |
} |
| 43 | |
|
| 44 | |
public Cache getCache(String name) { |
| 45 | 264124 | Cache cache = cachesMap.get(name); |
| 46 | 264124 | if (cache == null) { |
| 47 | 1 | MappingUtils.throwMappingException("Unable to find cache with name: " + name); |
| 48 | |
} |
| 49 | 264123 | return cache; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public void addCache(String name, int maxElementsInMemory) { |
| 53 | 2275 | addCache(new DozerCache(name, maxElementsInMemory)); |
| 54 | 2274 | } |
| 55 | |
|
| 56 | |
public void addCache(Cache cache) { |
| 57 | 2278 | synchronized (cachesMap) { |
| 58 | 2278 | String name = cache.getName(); |
| 59 | 2278 | if (cacheExists(name)) { |
| 60 | 1 | MappingUtils.throwMappingException("Cache already exists with name: " + name); |
| 61 | |
} |
| 62 | 2277 | cachesMap.put(name, cache); |
| 63 | 2277 | } |
| 64 | 2277 | } |
| 65 | |
|
| 66 | |
public Collection<String> getCacheNames() { |
| 67 | 1 | Set<String> results = new HashSet<String>(); |
| 68 | 1 | for (Entry<String, Cache> entry : cachesMap.entrySet()) { |
| 69 | 2 | results.add(entry.getKey()); |
| 70 | 2 | } |
| 71 | 1 | return results; |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public void clearAllEntries() { |
| 78 | 1 | for (Cache cache : cachesMap.values()) { |
| 79 | 1 | cache.clear(); |
| 80 | 1 | } |
| 81 | 1 | } |
| 82 | |
|
| 83 | |
public boolean cacheExists(String name) { |
| 84 | 2282 | return cachesMap.containsKey(name); |
| 85 | |
} |
| 86 | |
|
| 87 | |
public void logCaches() { |
| 88 | 0 | log.info(getCaches().toString()); |
| 89 | 0 | } |
| 90 | |
|
| 91 | |
} |