KickJava   Java API By Example, From Geeks To Geeks.

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


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 bean cache entries.
39  *
40  * @author Brett Chaldecott
41  */

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

51     public BeanCacheManager() {
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 BeanCache getBeanCache(Object JavaDoc ref) throws BeanException {
64         checkStatus();
65         synchronized (beanCaches) {
66             if (!beanCaches.containsKey(ref)) {
67                 beanCaches.put(ref,new BeanCache());
68             }
69             return (BeanCache)beanCaches.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 beanCaches = new HashMap JavaDoc();
79         synchronized (this.beanCaches) {
80             beanCaches.putAll(this.beanCaches);
81         }
82         for (Iterator JavaDoc iter = beanCaches.keySet().iterator(); iter.hasNext();) {
83             BeanCache beanCache = (BeanCache)beanCaches.get(iter.next());
84             beanCache.garbageCollect();
85         }
86     }
87     
88     
89     /**
90      * This method is called to forcibly remove everything from the cache.
91      */

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

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

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