1 27 28 package com.rift.coad.lib.deployment.rmi; 30 31 import java.util.Date ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Vector ; 37 import javax.rmi.PortableRemoteObject ; 38 39 import org.apache.log4j.Logger; 41 42 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 57 public class RMICache implements Cache { 58 59 private final static String CACHE_EXPIRY_TIME = "rmi_cache_expiry"; 61 private final static long CACHE_EXPIRY_TIME_DEFAULT = 60 * 1000; 62 63 protected static Logger log = 65 Logger.getLogger(RMICache.class.getName()); 66 67 private Map cacheEntries = new HashMap (); 69 private long defaultCacheExpiryTime = 0; 70 private ThreadStateMonitor status = new ThreadStateMonitor(); 71 72 73 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 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 97 public void garbageCollect() { 98 Map currentEntryList = new HashMap (); 99 synchronized (cacheEntries) { 100 currentEntryList.putAll(cacheEntries); 101 } 102 Date expiryDate = new Date (); 103 for (Iterator iter = currentEntryList.keySet().iterator(); iter.hasNext();) { 104 Object 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 ex) { 115 log.warn("The object was never exported : " + 116 ex.getMessage(),ex); 117 synchronized(cacheEntries) { 119 cacheEntries.remove(key); 120 } 121 cacheEntry.cacheRelease(); 122 } catch (Exception ex) { 123 log.error("Failed to remove a cache entry because : " + 124 ex.getMessage(),ex); 125 } 126 } 127 } 128 } 129 130 131 134 public void clear() { 135 status.terminate(false); 136 Map currentEntryList = new HashMap (); 137 synchronized (cacheEntries) { 138 currentEntryList.putAll(cacheEntries); 139 cacheEntries.clear(); 140 } 141 for (Iterator iter = currentEntryList.keySet().iterator(); iter.hasNext();) { 142 Object key = iter.next(); 143 RMICacheEntry cacheEntry = (RMICacheEntry)currentEntryList.get(key); 144 try { 145 PortableRemoteObject.unexportObject( 146 cacheEntry.getRemoteInterface()); 147 } catch (java.rmi.NoSuchObjectException ex) { 148 log.warn("The object was never exported : " + 149 ex.getMessage(),ex); 150 } catch (Exception ex) { 151 log.error("Failed to remove a cache entry because : " + 152 ex.getMessage(),ex); 153 } 154 cacheEntry.cacheRelease(); 155 } 156 } 157 158 159 165 public boolean contains(Object cacheEntry) { 166 synchronized(cacheEntries) { 167 return cacheEntries.containsKey(cacheEntry); 168 } 169 } 170 171 172 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 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 |