KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > cache > DozerCacheManager


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
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 /**
31  * @author tierney.matt
32 */

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 JavaDoc cachesMap = new HashMap JavaDoc();
37
38   /*
39    * The createNew() factory method does not act as a singleton. Callers must maintain their own reference to it.
40    * NOTE: that if the getInstance() method is called, the singleton instance will be created/returned,
41    * separate from any instances created in this method
42    */

43   public static DozerCacheManager createNew() {
44     return new DozerCacheManager();
45   }
46   
47   /*
48    * Use singleton cache manager for cache(s) that can be shared by all processes within a VM
49    */

50   public static DozerCacheManager getInstance() {
51     return singleton;
52   }
53   
54   private DozerCacheManager() {
55   }
56
57   public Set JavaDoc getCaches() {
58     return new HashSet JavaDoc(cachesMap.values());
59   }
60   
61   public Cache getCache(String JavaDoc 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 JavaDoc name, long maxElementsInMemory) {
70     addCache(new Cache(name, maxElementsInMemory));
71   }
72   
73   public void addCache(Cache cache) {
74     synchronized(cachesMap) {
75       String JavaDoc 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 JavaDoc getCacheNames() {
84     Set JavaDoc results = new HashSet JavaDoc();
85     Iterator JavaDoc iter = cachesMap.entrySet().iterator();
86     while (iter.hasNext()) {
87       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
88       results.add((String JavaDoc)entry.getKey());
89     }
90     return results;
91   }
92   
93   /*
94    * Dont clear keys in caches map because these are only added 1 time at startup.
95    * Only clear cache entries for each cache
96    */

97   public void clearAllEntries() {
98     Iterator JavaDoc 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 JavaDoc name) {
106     return cachesMap.containsKey(name);
107   }
108   
109   public void logCaches() {
110     log.info(getCaches());
111   }
112 }
113
Popular Tags