01: /*
02: * Copyright 2005-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
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.util.MappingUtils;
25:
26: import org.apache.commons.logging.Log;
27: import org.apache.commons.logging.LogFactory;
28:
29: /**
30: * Internal class that manages the Dozer caches. Only intended for internal use.
31: *
32: * @author tierney.matt
33: */
34: public final class DozerCacheManager implements CacheManagerIF {
35: private static final Log log = LogFactory
36: .getLog(DozerCacheManager.class);
37: private final Map cachesMap = new HashMap();
38:
39: public Set getCaches() {
40: return new HashSet(cachesMap.values());
41: }
42:
43: public Cache getCache(String name) {
44: Cache cache = (Cache) cachesMap.get(name);
45: if (cache == null) {
46: MappingUtils
47: .throwMappingException("Unable to find cache with name: "
48: + name);
49: }
50: return cache;
51: }
52:
53: public void addCache(String name, long maxElementsInMemory) {
54: addCache(new Cache(name, maxElementsInMemory));
55: }
56:
57: public void addCache(Cache cache) {
58: synchronized (cachesMap) {
59: String name = cache.getName();
60: if (cacheExists(name)) {
61: MappingUtils
62: .throwMappingException("Cache already exists with name: "
63: + name);
64: }
65: cachesMap.put(name, cache);
66: }
67: }
68:
69: public Set getCacheNames() {
70: Set results = new HashSet();
71: Iterator iter = cachesMap.entrySet().iterator();
72: while (iter.hasNext()) {
73: Map.Entry entry = (Map.Entry) iter.next();
74: results.add(entry.getKey());
75: }
76: return results;
77: }
78:
79: /*
80: * Dont clear keys in caches map because these are only added 1 time at startup. Only clear cache entries for each cache
81: */
82: public void clearAllEntries() {
83: Iterator iter = cachesMap.values().iterator();
84: while (iter.hasNext()) {
85: Cache cache = (Cache) iter.next();
86: cache.clear();
87: }
88: }
89:
90: public boolean cacheExists(String name) {
91: return cachesMap.containsKey(name);
92: }
93:
94: public void logCaches() {
95: log.info(getCaches());
96: }
97: }
|