KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ProxyCache.java
20  *
21  * This object is responsible for caching the proxy entries. They will be
22  * released after x number milli seconds of in activity. Forcing a reconnect
23  * and the release of a resource.
24  */

25
26 // the package path
27
package com.rift.coad.lib.bean;
28
29 // java imports
30
import java.util.Date JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Vector JavaDoc;
35
36
37 // logging import
38
import org.apache.log4j.Logger;
39
40 // coadunation imports
41
import com.rift.coad.lib.cache.Cache;
42 import com.rift.coad.lib.cache.CacheEntry;
43 import com.rift.coad.lib.common.RandomGuid;
44 import com.rift.coad.lib.configuration.ConfigurationFactory;
45 import com.rift.coad.lib.configuration.Configuration;
46 import com.rift.coad.lib.thread.ThreadStateMonitor;
47
48 /**
49  * This object is responsible for caching the proxy entries. They will be
50  * released after x number milli seconds of in activity. Forcing a reconnect
51  * and the release of a resource.
52  *
53  * @author Brett Chaldecott
54  */

55 public class ProxyCache implements Cache {
56     
57     // class constants
58
private final static String JavaDoc CACHE_EXPIRY_TIME = "proxy_cache_expiry";
59     private final static long CACHE_EXPIRY_TIME_DEFAULT = 60 * 1000;
60     
61     // private member variables
62
protected static Logger log =
63             Logger.getLogger(ProxyCache.class.getName());
64     
65     // private member variables
66
private Map JavaDoc cacheEntries = new HashMap JavaDoc();
67     private long defaultCacheExpiryTime = 0;
68     private ThreadStateMonitor status = new ThreadStateMonitor();
69     
70     
71     /**
72      * Creates a new instance of ProxyCache
73      *
74      * @exception BeanException
75      */

76     public ProxyCache() throws BeanException {
77         try {
78             Configuration config = ConfigurationFactory.getInstance().
79                     getConfig(ProxyCache.class);
80             defaultCacheExpiryTime = config.getLong(CACHE_EXPIRY_TIME,
81                     CACHE_EXPIRY_TIME_DEFAULT);
82         } catch (Exception JavaDoc ex) {
83             log.error("Failed to start the ProxyCache object because : " +
84                     ex.getMessage(),ex);
85             throw new BeanException(
86                     "Failed to start the ProxyCache object because : " +
87                     ex.getMessage(),ex);
88         }
89     }
90     
91     
92     /**
93      * This method is called to perform garbage collection on the cache entries.
94      */

95     public void garbageCollect() {
96         Map JavaDoc cacheEntries = new HashMap JavaDoc();
97         synchronized(this.cacheEntries) {
98             cacheEntries.putAll(this.cacheEntries);
99         }
100         
101         // the start time
102
Date JavaDoc currentTime = new Date JavaDoc();
103         for (Iterator JavaDoc iter = cacheEntries.keySet().iterator(); iter.hasNext();) {
104             Object JavaDoc key = iter.next();
105             ProxyCacheEntry cacheEntry =
106                     (ProxyCacheEntry)cacheEntries.get(key);
107             if (cacheEntry.isExpired(currentTime)) {
108                 synchronized(this.cacheEntries) {
109                     this.cacheEntries.remove(key);
110                 }
111                 cacheEntry.cacheRelease();
112             }
113         }
114     }
115     
116     
117     /**
118      * This method is called to forcibly remove everything from the cache.
119      */

120     public void clear() {
121         Map JavaDoc cacheEntries = new HashMap JavaDoc();
122         synchronized(this.cacheEntries) {
123             status.terminate(false);
124             cacheEntries.putAll(this.cacheEntries);
125             this.cacheEntries.clear();
126         }
127         
128         for (Iterator JavaDoc iter = cacheEntries.keySet().iterator(); iter.hasNext();) {
129             ProxyCacheEntry cacheEntry =
130                     (ProxyCacheEntry)cacheEntries.get(iter.next());
131             cacheEntry.cacheRelease();
132         }
133     }
134     
135     
136     /**
137      * This mehtod returns true if the cache contains the checked entry.
138      *
139      * @return TRUE if the cache contains the checked entry.
140      * @param cacheEntry The entry to perform the check for.
141      */

142     public boolean contains(Object JavaDoc cacheEntry) {
143         // this method assumes the cache entry is the handler
144
synchronized(cacheEntries) {
145             return cacheEntries.containsKey(cacheEntry);
146         }
147     }
148     
149     
150     /**
151      * This method adds a new entry to the proxy cache.
152      *
153      * @param timeout The timeout for this object.
154      * @param proxy The proxy to add to the cache.
155      * @param handler The handler to perform the check for.
156      */

157     public void addCacheEntry(long timeout, Object JavaDoc proxy, CacheEntry handler)
158             throws BeanException {
159         synchronized(cacheEntries) {
160             checkStatus();
161             long cacheTimeout = timeout;
162             if (timeout == -1) {
163                 cacheTimeout = defaultCacheExpiryTime;
164             }
165             cacheEntries.put(handler,new ProxyCacheEntry(cacheTimeout, proxy,
166                     handler));
167         }
168     }
169     
170     
171     /**
172      * This method will check to see if the cache has been terminated or not.
173      *
174      * @exception BeanException
175      */

176     private void checkStatus() throws BeanException {
177         if (status.isTerminated()) {
178             throw new BeanException("The proxy cache has been shut down.");
179         }
180     }
181 }
182
Popular Tags