KickJava   Java API By Example, From Geeks To Geeks.

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


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  * KeySyncCache.java
20  *
21  * This object is responsible for returning synchronization objects based on a
22  * key passed in.
23  */

24
25 // package path
26
package com.rift.coad.lib.cache;
27
28 // java imports
29
import java.util.Date JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 // logging import
35
import org.apache.log4j.Logger;
36
37 // coadunation imports
38
import com.rift.coad.lib.configuration.ConfigurationFactory;
39 import com.rift.coad.lib.configuration.Configuration;
40 import com.rift.coad.lib.thread.ThreadStateMonitor;
41
42
43 /**
44  * This object is responsible for returning synchronization objects based on a
45  * key passed in.
46  *
47  * @author Brett Chaldecott
48  */

49 public class KeySyncCache implements Cache {
50     
51     /**
52      * This object is used to synchronize access to a specific key value.
53      */

54     public static class KeySync {
55         // private
56
private Date JavaDoc touchTime = null;
57         
58         /**
59          * The constructor of the key sync object.
60          */

61         public KeySync() {
62             touch();
63         }
64         
65         
66         /**
67          * This object returns true if this object is expiried.
68          *
69          * @return TRUE if expired FALSE if not.
70          * @param expiryDate The date the time is older than for expiry to
71          * succeed.
72          */

73         public synchronized boolean isExpired(Date JavaDoc expiryDate) {
74             return (touchTime.getTime() < expiryDate.getTime());
75         }
76         
77         
78         /**
79          * This method updates the last touch time for this object.
80          */

81         public synchronized void touch() {
82             touchTime = new Date JavaDoc();
83         }
84     }
85     
86     // class constants
87
private final static String JavaDoc CACHE_EXPIRY_TIME = "key_sync_cache_expiry";
88     private final static long CACHE_EXPIRY_TIME_DEFAULT = 30 * 60 * 1000;
89     
90     // private member variables
91
protected static Logger log =
92             Logger.getLogger(KeySyncCache.class.getName());
93             
94     // private member variable
95
private ThreadStateMonitor status = new ThreadStateMonitor();
96     private Map JavaDoc keySyncMap = new HashMap JavaDoc();
97     private Configuration config = null;
98     private long cacheExpiryTime = 0;
99     
100     
101     /**
102      * Creates a new instance of KeySyncCache
103      *
104      * @exception CacheException
105      */

106     public KeySyncCache() throws CacheException {
107         try {
108             Configuration config = ConfigurationFactory.getInstance().
109                     getConfig(KeySyncCache.class);
110             cacheExpiryTime = config.getLong(CACHE_EXPIRY_TIME,
111                     CACHE_EXPIRY_TIME_DEFAULT);
112         } catch (Exception JavaDoc ex) {
113             log.error("Failed to start the KeySyncCache object because : " +
114                     ex.getMessage(),ex);
115             throw new CacheException(
116                     "Failed to start the KeySyncCache object because : " +
117                     ex.getMessage(),ex);
118         }
119     }
120     
121     
122     /**
123      * This method returns the key sync object for the requested key.
124      *
125      * @return The reference to the key stink object to return.
126      * @param key The key to retrieve.
127      */

128     public KeySync getKeySync(Object JavaDoc key) throws CacheException {
129         checkStatus();
130         synchronized(keySyncMap) {
131             if (!keySyncMap.containsKey(key)) {
132                 keySyncMap.put(key,new KeySync());
133             }
134             KeySync keySync = (KeySync)keySyncMap.get(key);
135             keySync.touch();
136             return keySync;
137         }
138     }
139     
140     
141     /**
142      * This method is called to perform garbage collection on the cache entries.
143      */

144     public void garbageCollect() {
145         Map JavaDoc keySyncMap = new HashMap JavaDoc();
146         synchronized(this.keySyncMap) {
147             keySyncMap.putAll(this.keySyncMap);
148         }
149         Date JavaDoc expiryDate = new Date JavaDoc(new Date JavaDoc().getTime() - cacheExpiryTime);
150         for (Iterator JavaDoc iter = keySyncMap.keySet().iterator();iter.hasNext();) {
151             Object JavaDoc key = iter.next();
152             KeySync keySync = (KeySync)keySyncMap.get(key);
153             synchronized(this.keySyncMap) {
154                 if (keySync.isExpired(expiryDate)) {
155                     this.keySyncMap.remove(key);
156                 }
157             }
158         }
159     }
160     
161     
162     /**
163      * This method is called to forcibly remove everything from the cache.
164      */

165     public void clear() {
166         status.terminate(false);
167         synchronized(this.keySyncMap) {
168             this.keySyncMap.clear();
169         }
170     }
171     
172     
173     /**
174      * This mehtod returns true if the cache contains the checked entry.
175      *
176      * @return TRUE if the cache contains the checked entry.
177      * @param cacheEntry The entry to perform the check for.
178      */

179     public boolean contains(Object JavaDoc cacheEntry) {
180         synchronized (keySyncMap) {
181             return keySyncMap.containsKey(cacheEntry);
182         }
183     }
184     
185     
186     /**
187      * This method checks the status of this cache and throws if it has been
188      * terminated.
189      *
190      * @exception CacheException
191      */

192     private void checkStatus() throws CacheException {
193         if (status.isTerminated()) {
194             throw new CacheException("The cache has been terminated.");
195         }
196     }
197     
198 }
199
Popular Tags