KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > cache > KeySyncCacheManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * KeySyncCacheManager.java
20  *
21  * This class is responsible for managing the the key sync caches.
22  */

23
24 // package path
25
package com.rift.coad.lib.cache;
26
27 // java imports
28
import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 // coadunation imports
33
import com.rift.coad.lib.thread.ThreadStateMonitor;
34
35 /**
36  * This class is responsible for managing the the key sync caches.
37  *
38  * @author Brett Chaldecott
39  */

40 public class KeySyncCacheManager implements Cache {
41     
42     // private member variables
43
private ThreadStateMonitor status = new ThreadStateMonitor();
44     private Map JavaDoc keySyncCaches = new HashMap JavaDoc();
45     
46     /**
47      * Creates a new instance of KeySyncCacheManager
48      */

49     public KeySyncCacheManager() {
50     }
51     
52     
53     /**
54      * This method returns the key synch cache for the given key.
55      *
56      * @return KeySyncCache The cache object for the given identifier.
57      * @param identifier The identifier to look up.
58      * @exception CacheException
59      */

60     public KeySyncCache getKeySyncCache(Object JavaDoc identifier) throws CacheException {
61         checkStatus();
62         synchronized (keySyncCaches) {
63             if (!keySyncCaches.containsKey(identifier)) {
64                 keySyncCaches.put(identifier,new KeySyncCache());
65             }
66             return (KeySyncCache)keySyncCaches.get(identifier);
67         }
68     }
69     
70     
71     /**
72      * This method is called to perform garbage collection on the cache entries.
73      */

74     public void garbageCollect() {
75         Map JavaDoc keySyncCaches = new HashMap JavaDoc();
76         synchronized (this.keySyncCaches) {
77             keySyncCaches.putAll(this.keySyncCaches);
78         }
79         for (Iterator JavaDoc iter = keySyncCaches.keySet().iterator(); iter.hasNext();) {
80             KeySyncCache keySyncCache = (KeySyncCache)keySyncCaches.get(
81                     iter.next());
82             keySyncCache.garbageCollect();
83         }
84     }
85     
86     
87     /**
88      * This method is called to forcibly remove everything from the cache.
89      */

90     public void clear() {
91         status.terminate(false);
92         Map JavaDoc keySyncCaches = new HashMap JavaDoc();
93         synchronized (this.keySyncCaches) {
94             keySyncCaches.putAll(this.keySyncCaches);
95             this.keySyncCaches.clear();
96         }
97         for (Iterator JavaDoc iter = keySyncCaches.keySet().iterator(); iter.hasNext();) {
98             KeySyncCache keySyncCache = (KeySyncCache)keySyncCaches.get(
99                     iter.next());
100             keySyncCache.clear();
101         }
102     }
103     
104     
105     /**
106      * This mehtod returns true if the cache contains the checked entry.
107      *
108      * @return TRUE if the cache contains the checked entry.
109      * @param cacheEntry The entry to perform the check for.
110      */

111     public boolean contains(Object JavaDoc cacheEntry) {
112         synchronized (keySyncCaches) {
113             return keySyncCaches.containsKey(cacheEntry);
114         }
115     }
116     
117     
118     /**
119      * This method checks the status of the cache.
120      *
121      * @exception CacheException
122      */

123     private void checkStatus() throws CacheException {
124         if (status.isTerminated()) {
125             throw new CacheException("The cache has been terminated.");
126         }
127     }
128 }
129
Popular Tags