KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > rmi > RMICache


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  * XMLConfigurationException.java
20  *
21  * RMICache.java
22  *
23  * This class is responsible for temporarily caching of rmi factory objects.
24  * They will be cache until they have not been touched for a given period of
25  * time.
26  */

27
28 // package path
29
package com.rift.coad.lib.deployment.rmi;
30
31 // java imports
32
import java.util.Date JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Vector JavaDoc;
37 import javax.rmi.PortableRemoteObject JavaDoc;
38
39 // logging import
40
import org.apache.log4j.Logger;
41
42 // coad imports
43
import com.rift.coad.lib.cache.Cache;
44 import com.rift.coad.lib.cache.CacheEntry;
45 import com.rift.coad.lib.common.RandomGuid;
46 import com.rift.coad.lib.configuration.ConfigurationFactory;
47 import com.rift.coad.lib.configuration.Configuration;
48 import com.rift.coad.lib.thread.ThreadStateMonitor;
49
50 /**
51  * This class is responsible for temporarily caching of rmi factory objects.
52  * They will be cache until they have not been touched for a given period of
53  * time.
54  *
55  * @author Brett Chaldecott
56  */

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

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

97     public void garbageCollect() {
98         Map JavaDoc currentEntryList = new HashMap JavaDoc();
99         synchronized (cacheEntries) {
100             currentEntryList.putAll(cacheEntries);
101         }
102         Date JavaDoc expiryDate = new Date JavaDoc();
103         for (Iterator JavaDoc iter = currentEntryList.keySet().iterator(); iter.hasNext();) {
104             Object JavaDoc key = iter.next();
105             RMICacheEntry cacheEntry = (RMICacheEntry)currentEntryList.get(key);
106             if (cacheEntry.isExpired(expiryDate)) {
107                 try {
108                     PortableRemoteObject.unexportObject(
109                             cacheEntry.getRemoteInterface());
110                     synchronized(cacheEntries) {
111                         cacheEntries.remove(key);
112                     }
113                     cacheEntry.cacheRelease();
114                 } catch (java.rmi.NoSuchObjectException JavaDoc ex) {
115                     log.warn("The object was never exported : " +
116                             ex.getMessage(),ex);
117                     // remove from cache
118
synchronized(cacheEntries) {
119                         cacheEntries.remove(key);
120                     }
121                     cacheEntry.cacheRelease();
122                 } catch (Exception JavaDoc ex) {
123                     log.error("Failed to remove a cache entry because : " +
124                             ex.getMessage(),ex);
125                 }
126             }
127         }
128     }
129     
130     
131     /**
132      * This method is called to forcibly remove everything from the cache.
133      */

134     public void clear() {
135         status.terminate(false);
136         Map JavaDoc currentEntryList = new HashMap JavaDoc();
137         synchronized (cacheEntries) {
138             currentEntryList.putAll(cacheEntries);
139             cacheEntries.clear();
140         }
141         for (Iterator JavaDoc iter = currentEntryList.keySet().iterator(); iter.hasNext();) {
142             Object JavaDoc key = iter.next();
143             RMICacheEntry cacheEntry = (RMICacheEntry)currentEntryList.get(key);
144             try {
145                 PortableRemoteObject.unexportObject(
146                         cacheEntry.getRemoteInterface());
147             } catch (java.rmi.NoSuchObjectException JavaDoc ex) {
148                 log.warn("The object was never exported : " +
149                         ex.getMessage(),ex);
150             } catch (Exception JavaDoc ex) {
151                 log.error("Failed to remove a cache entry because : " +
152                         ex.getMessage(),ex);
153             }
154             cacheEntry.cacheRelease();
155         }
156     }
157     
158     
159     /**
160      * This mehtod returns true if the cache contains the checked entry.
161      *
162      * @return TRUE if the cache contains the checked entry.
163      * @param cacheEntry The entry to perform the check for.
164      */

165     public boolean contains(Object JavaDoc cacheEntry) {
166         synchronized(cacheEntries) {
167             return cacheEntries.containsKey(cacheEntry);
168         }
169     }
170     
171     
172     /**
173      * This method is responsible for adding an entry to the cache.
174      *
175      * @param entry The entry to add to the cache.
176      */

177     public void addCacheEntry(long timeout, CacheEntry entry) throws RMIException {
178         synchronized(cacheEntries) {
179             checkStatus();
180             long cacheTimeout = timeout;
181             if (timeout == -1) {
182                 cacheTimeout = defaultCacheExpiryTime;
183             }
184             cacheEntries.put(entry,new RMICacheEntry(cacheTimeout,entry));
185         }
186     }
187     
188     /**
189      * This method will check to see if the cache has been terminated or not.
190      *
191      * @exception BeanException
192      */

193     private void checkStatus() throws RMIException {
194         if (status.isTerminated()) {
195             throw new RMIException("The RMI cache has been shut down.");
196         }
197     }
198 }
199
Popular Tags