1 18 19 package sync4j.framework.transport.http; 20 21 import java.util.*; 22 import java.util.logging.*; 23 24 50 public class SyncHolderCache { 51 52 54 public static final long CLEANING_PERIOD = 300000; public static final long DEFAULT_TTL = 300000; public static final String LOG_NAME = "sync4j.transport.http"; 57 58 private static final Logger log = Logger.getLogger(LOG_NAME); 59 60 62 private long holderTimeToLive = DEFAULT_TTL; 63 64 private long lastCleaningTimestamp = 0; 65 66 private Map cache; 67 68 70 71 public SyncHolderCache(long timeToLive) { 72 cache = new HashMap(97); 73 74 lastCleaningTimestamp = System.currentTimeMillis(); 75 76 holderTimeToLive = timeToLive; 77 } 78 79 81 public void put(SyncHolder holder) { 82 clean(); 83 84 if (log.isLoggable(Level.FINEST)) { 85 log.finest("Caching " + holder.getSessionId() + '(' + holder + ')'); 86 } 87 88 synchronized (cache) { 89 cache.put(holder.getSessionId(), holder); 90 } 91 } 92 93 public SyncHolder get(String sessionId) { 94 return (SyncHolder)cache.get(sessionId); 95 } 96 97 public void remove(String sessionId) { 98 if (log.isLoggable(Level.FINEST)) { 99 log.finest("Removing holder for " + sessionId); 100 } 101 synchronized (cache) { 102 cache.remove(sessionId); 103 } 104 } 105 106 108 public String toString() { 109 String s = null; 110 synchronized (cache) { 111 s = String.valueOf(cache); 112 } 113 return s; 114 } 115 116 118 124 private boolean clean() { 125 long now = System.currentTimeMillis(); 126 127 if (log.isLoggable(Level.FINEST)) { 128 log.finest("Cleaning procedure..." ); 129 log.finest("now: " + now ); 130 log.finest("CLEANING_PERIOD: " + CLEANING_PERIOD ); 131 log.finest("lastCleaningTimestamp: " + lastCleaningTimestamp); 132 log.finest("holderTimeToLive: " + holderTimeToLive ); 133 } 134 135 ArrayList toBeRemoved = null; 136 SyncHolder h; 137 Object key, value; 138 Iterator i = null; 139 140 synchronized (cache) { 141 142 if ((now - lastCleaningTimestamp) <= CLEANING_PERIOD) { 143 if (log.isLoggable(Level.FINEST)) { 144 log.finest("No purging required"); 145 } 146 return false; 147 } 148 149 if (log.isLoggable(Level.FINEST)) { 150 log.finest("Performing purging..."); 151 } 152 153 toBeRemoved = new ArrayList(); 154 155 i = cache.keySet().iterator(); 156 while (i.hasNext()) { 157 key = i.next(); 158 value = cache.get(key); 159 160 if (!(value instanceof SyncHolder)) { 161 if (log.isLoggable(Level.FINEST)) { 163 log.finest("Found unexpected object: " + key + " will be removed!"); 164 } 165 toBeRemoved.add(key); 166 continue; 167 } 168 h = (SyncHolder)value; 169 if ((now - h.getCreationTimestamp()) > holderTimeToLive) { 170 171 if (log.isLoggable(Level.FINEST)) { 172 log.finest("Purging holder for session " + key); 173 } 174 175 try { 176 h.close(); 177 } catch (Exception e) { 178 log.severe(e.getMessage()); 179 log.throwing(getClass().getName(), "clean", e); 180 } 181 toBeRemoved.add(key); 182 } 183 } 184 185 i = toBeRemoved.iterator(); 189 while(i.hasNext()) { 190 cache.remove(i.next()); 191 } 192 } 193 194 lastCleaningTimestamp = System.currentTimeMillis(); 195 196 return true; 197 } 198 } 199 | Popular Tags |