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: * BeanCacheManager.java
020: *
021: * This object is responsible for managing the bean cache entries.
022: */
023:
024: // package path
025: package com.rift.coad.lib.bean;
026:
027: // java imports
028: import java.util.Map;
029: import java.util.HashMap;
030: import java.util.Iterator;
031:
032: // coadunation imports
033: import com.rift.coad.lib.cache.Cache;
034: import com.rift.coad.lib.cache.CacheEntry;
035: import com.rift.coad.lib.thread.ThreadStateMonitor;
036:
037: /**
038: * This object is responsible for managing the transaction bean cache entries.
039: *
040: * @author Brett Chaldecott
041: */
042: public class TransactionBeanCacheManager implements Cache {
043:
044: // the classes private member variables
045: private Map transactionBeanCaches = new HashMap();
046: private ThreadStateMonitor status = new ThreadStateMonitor();
047:
048: /**
049: * Creates a new instance of BeanCacheManager
050: */
051: public TransactionBeanCacheManager() {
052:
053: }
054:
055: /**
056: * This method returns the reference to the bean cache.
057: *
058: * @return The reference to the bean cache.
059: * @param ref The reference to the bean cache.
060: * @exception BeanException
061: */
062: public TransactionBeanCache getBeanCache(Object ref)
063: throws BeanException {
064: checkStatus();
065: synchronized (transactionBeanCaches) {
066: if (!transactionBeanCaches.containsKey(ref)) {
067: transactionBeanCaches.put(ref,
068: new TransactionBeanCache());
069: }
070: return (TransactionBeanCache) transactionBeanCaches
071: .get(ref);
072: }
073: }
074:
075: /**
076: * This method is called to perform garbage collection on the cache entries.
077: */
078: public void garbageCollect() {
079: Map transactionBeanCaches = new HashMap();
080: synchronized (this .transactionBeanCaches) {
081: transactionBeanCaches.putAll(this .transactionBeanCaches);
082: }
083: for (Iterator iter = transactionBeanCaches.keySet().iterator(); iter
084: .hasNext();) {
085: TransactionBeanCache transactionBeanCache = (TransactionBeanCache) transactionBeanCaches
086: .get(iter.next());
087: transactionBeanCache.garbageCollect();
088: }
089: }
090:
091: /**
092: * This method is called to forcibly remove everything from the cache.
093: */
094: public void clear() {
095: status.terminate(true);
096: Map transactionBeanCaches = new HashMap();
097: synchronized (this .transactionBeanCaches) {
098: transactionBeanCaches.putAll(this .transactionBeanCaches);
099: this .transactionBeanCaches.clear();
100: }
101: for (Iterator iter = transactionBeanCaches.keySet().iterator(); iter
102: .hasNext();) {
103: TransactionBeanCache transactionBeanCache = (TransactionBeanCache) transactionBeanCaches
104: .get(iter.next());
105: transactionBeanCache.clear();
106: }
107: }
108:
109: /**
110: * This mehtod returns true if the cache contains the checked entry.
111: *
112: * @return TRUE if the cache contains the checked entry.
113: * @param cacheEntry The entry to perform the check for.
114: */
115: public boolean contains(Object cacheEntry) {
116: synchronized (transactionBeanCaches) {
117: return transactionBeanCaches.containsKey(cacheEntry);
118: }
119: }
120:
121: /**
122: * This method checks the status of the bean cache manager.
123: *
124: * @exception BeanException
125: */
126: private void checkStatus() throws BeanException {
127: if (status.isTerminated()) {
128: throw new BeanException(
129: "Bean cache manager has been terminated.");
130: }
131: }
132: }
|