001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * KeySyncCacheManager.java
020: *
021: * This class is responsible for managing the the key sync caches.
022: */
023:
024: // package path
025: package com.rift.coad.lib.cache;
026:
027: // java imports
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.HashMap;
031:
032: // coadunation imports
033: import com.rift.coad.lib.thread.ThreadStateMonitor;
034:
035: /**
036: * This class is responsible for managing the the key sync caches.
037: *
038: * @author Brett Chaldecott
039: */
040: public class KeySyncCacheManager implements Cache {
041:
042: // private member variables
043: private ThreadStateMonitor status = new ThreadStateMonitor();
044: private Map keySyncCaches = new HashMap();
045:
046: /**
047: * Creates a new instance of KeySyncCacheManager
048: */
049: public KeySyncCacheManager() {
050: }
051:
052: /**
053: * This method returns the key synch cache for the given key.
054: *
055: * @return KeySyncCache The cache object for the given identifier.
056: * @param identifier The identifier to look up.
057: * @exception CacheException
058: */
059: public KeySyncCache getKeySyncCache(Object identifier)
060: throws CacheException {
061: checkStatus();
062: synchronized (keySyncCaches) {
063: if (!keySyncCaches.containsKey(identifier)) {
064: keySyncCaches.put(identifier, new KeySyncCache());
065: }
066: return (KeySyncCache) keySyncCaches.get(identifier);
067: }
068: }
069:
070: /**
071: * This method is called to perform garbage collection on the cache entries.
072: */
073: public void garbageCollect() {
074: Map keySyncCaches = new HashMap();
075: synchronized (this .keySyncCaches) {
076: keySyncCaches.putAll(this .keySyncCaches);
077: }
078: for (Iterator iter = keySyncCaches.keySet().iterator(); iter
079: .hasNext();) {
080: KeySyncCache keySyncCache = (KeySyncCache) keySyncCaches
081: .get(iter.next());
082: keySyncCache.garbageCollect();
083: }
084: }
085:
086: /**
087: * This method is called to forcibly remove everything from the cache.
088: */
089: public void clear() {
090: status.terminate(false);
091: Map keySyncCaches = new HashMap();
092: synchronized (this .keySyncCaches) {
093: keySyncCaches.putAll(this .keySyncCaches);
094: this .keySyncCaches.clear();
095: }
096: for (Iterator iter = keySyncCaches.keySet().iterator(); iter
097: .hasNext();) {
098: KeySyncCache keySyncCache = (KeySyncCache) keySyncCaches
099: .get(iter.next());
100: keySyncCache.clear();
101: }
102: }
103:
104: /**
105: * This mehtod returns true if the cache contains the checked entry.
106: *
107: * @return TRUE if the cache contains the checked entry.
108: * @param cacheEntry The entry to perform the check for.
109: */
110: public boolean contains(Object cacheEntry) {
111: synchronized (keySyncCaches) {
112: return keySyncCaches.containsKey(cacheEntry);
113: }
114: }
115:
116: /**
117: * This method checks the status of the cache.
118: *
119: * @exception CacheException
120: */
121: private void checkStatus() throws CacheException {
122: if (status.isTerminated()) {
123: throw new CacheException("The cache has been terminated.");
124: }
125: }
126: }
|