KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > bean > TransactionBeanCacheManager


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  * BeanCacheManager.java
20  *
21  * This object is responsible for managing the bean cache entries.
22  */

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

42 public class TransactionBeanCacheManager implements Cache {
43     
44     // the classes private member variables
45
private Map JavaDoc transactionBeanCaches = new HashMap JavaDoc();
46     private ThreadStateMonitor status = new ThreadStateMonitor();
47     
48     /**
49      * Creates a new instance of BeanCacheManager
50      */

51     public TransactionBeanCacheManager() {
52         
53     }
54     
55     
56     /**
57      * This method returns the reference to the bean cache.
58      *
59      * @return The reference to the bean cache.
60      * @param ref The reference to the bean cache.
61      * @exception BeanException
62      */

63     public TransactionBeanCache getBeanCache(Object JavaDoc ref) throws BeanException {
64         checkStatus();
65         synchronized (transactionBeanCaches) {
66             if (!transactionBeanCaches.containsKey(ref)) {
67                 transactionBeanCaches.put(ref,new TransactionBeanCache());
68             }
69             return (TransactionBeanCache)transactionBeanCaches.get(ref);
70         }
71     }
72     
73     
74     /**
75      * This method is called to perform garbage collection on the cache entries.
76      */

77     public void garbageCollect() {
78         Map JavaDoc transactionBeanCaches = new HashMap JavaDoc();
79         synchronized (this.transactionBeanCaches) {
80             transactionBeanCaches.putAll(this.transactionBeanCaches);
81         }
82         for (Iterator JavaDoc iter = transactionBeanCaches.keySet().iterator();
83                 iter.hasNext();) {
84             TransactionBeanCache transactionBeanCache =
85                     (TransactionBeanCache)transactionBeanCaches.get(
86                     iter.next());
87             transactionBeanCache.garbageCollect();
88         }
89     }
90     
91     
92     /**
93      * This method is called to forcibly remove everything from the cache.
94      */

95     public void clear() {
96         status.terminate(true);
97         Map JavaDoc transactionBeanCaches = new HashMap JavaDoc();
98         synchronized (this.transactionBeanCaches) {
99             transactionBeanCaches.putAll(this.transactionBeanCaches);
100             this.transactionBeanCaches.clear();
101         }
102         for (Iterator JavaDoc iter = transactionBeanCaches.keySet().iterator();
103                 iter.hasNext();) {
104             TransactionBeanCache transactionBeanCache =
105                     (TransactionBeanCache)transactionBeanCaches.get(
106                     iter.next());
107             transactionBeanCache.clear();
108         }
109     }
110     
111     
112     /**
113      * This mehtod returns true if the cache contains the checked entry.
114      *
115      * @return TRUE if the cache contains the checked entry.
116      * @param cacheEntry The entry to perform the check for.
117      */

118     public boolean contains(Object JavaDoc cacheEntry) {
119         synchronized (transactionBeanCaches) {
120             return transactionBeanCaches.containsKey(cacheEntry);
121         }
122     }
123     
124     
125     /**
126      * This method checks the status of the bean cache manager.
127      *
128      * @exception BeanException
129      */

130     private void checkStatus() throws BeanException {
131         if (status.isTerminated()) {
132             throw new BeanException("Bean cache manager has been terminated.");
133         }
134     }
135 }
136
Popular Tags