1 18 package sync4j.transport.http.server; 19 20 import java.util.*; 21 import java.util.logging.*; 22 23 import javax.servlet.*; 24 25 import org.apache.commons.lang.*; 26 import org.jgroups.*; 27 import org.jgroups.blocks.*; 28 import org.jgroups.log.*; 29 30 import sync4j.framework.transport.http.*; 31 32 58 public class RemoteHolderCache 59 implements Constants { 60 61 63 public static final long CLEANING_PERIOD = 300000; 65 private static final Logger log = Logger.getLogger(LOG_NAME); 66 67 69 private long holderTimeToLive = DEFAULT_TTL; 70 71 private long lastCleaningTimestamp = 0; 72 73 private String channelProps; 74 75 private DistributedHashtable cache; 76 77 79 public RemoteHolderCache(ServletConfig config) { 80 try { 81 String group = config.getInitParameter(PARAM_GROUP); 82 String props = config.getInitParameter(PARAM_CHANNEL_PROPERTIES); 83 84 if (StringUtils.isEmpty(group)) { 85 group = DEFAULT_GROUP; 86 } 87 88 if (log.isLoggable(Level.FINE)) { 93 Trace.init(); 94 } 95 96 if (log.isLoggable(Level.FINE)) { 97 log.fine("Multicast group: " + group); 98 log.fine("Multicast properties: " + props); 99 } 100 101 JChannelFactory cf = new JChannelFactory(); 102 cache = new DistributedHashtable(group, cf, props, DEFAULT_TIMEOUT); 103 104 } catch (Exception e) { 105 log.severe(e.getMessage()); 106 log.throwing(getClass().getName(), "configure", e); 107 } 108 109 lastCleaningTimestamp = System.currentTimeMillis(); 110 111 holderTimeToLive = 112 Long.parseLong(config.getInitParameter(PARAM_SESSION_TTL)); 113 114 } 115 116 118 public void put(SyncHolder holder) { 119 clean(); 120 if (log.isLoggable(Level.FINE)) { 121 log.fine("Caching " + holder.getSessionId() + '(' + holder + ')'); 122 } 123 synchronized (cache) { 124 cache.put(holder.getSessionId(), holder); 125 } 126 } 127 128 public SyncHolder get(String sessionId) { 129 return (SyncHolder)cache.get(sessionId); 130 } 131 132 public void remove(String sessionId) { 133 if (log.isLoggable(Level.FINE)) { 134 log.fine("Removing holder for " + sessionId); 135 } 136 synchronized (cache) { 137 cache.remove(sessionId); 138 } 139 } 140 141 143 public String toString() { 144 String s = null; 145 synchronized (cache) { 146 s = String.valueOf(cache); 147 } 148 return s; 149 } 150 151 153 159 private boolean clean() { 160 long now = System.currentTimeMillis(); 161 162 if (log.isLoggable(Level.FINE)) { 163 log.fine("Cleaning procedure..." ); 164 log.fine("now: " + now ); 165 log.fine("CLEANING_PERIOD: " + CLEANING_PERIOD ); 166 log.fine("lastCleaningTimestamp: " + lastCleaningTimestamp); 167 log.fine("holderTimeToLive: " + holderTimeToLive ); 168 } 169 170 ArrayList toBeRemoved = null; 171 172 RemoteEJBSyncHolder h; 173 Object key, value; 174 Iterator i = null; 175 176 synchronized (cache) { 177 178 if ((now - lastCleaningTimestamp) <= CLEANING_PERIOD) { 179 if (log.isLoggable(Level.FINEST)) { 180 log.finest("No purging required"); 181 } 182 return false; 183 } 184 185 if (log.isLoggable(Level.FINEST)) { 186 log.finest("Performing purging..."); 187 } 188 189 toBeRemoved = new ArrayList(); 190 191 i = cache.keySet().iterator(); 192 while (i.hasNext()) { 193 key = i.next(); 194 value = cache.get(key); 195 196 if (!(value instanceof RemoteEJBSyncHolder)) { 197 if (log.isLoggable(Level.FINEST)) { 199 log.finest("Found unexpected object:" + key + " will be removed!"); 200 } 201 toBeRemoved.add(key); 202 continue; 203 } 204 h = (RemoteEJBSyncHolder)value; 205 if ((now - h.getCreationTimestamp()) > holderTimeToLive) { 206 if (log.isLoggable(Level.FINEST)) { 207 log.finest("Purging holder for session " + key); 208 } 209 try { 210 h.close(); 211 } catch (Exception e) { 212 log.severe(e.getMessage()); 213 log.throwing(getClass().getName(), "clean", e); 214 } 215 toBeRemoved.add(key); 216 } 217 } 218 219 i = toBeRemoved.iterator(); 223 while(i.hasNext()) { 224 cache.remove(i.next()); 225 } 226 } 227 228 lastCleaningTimestamp = System.currentTimeMillis(); 229 230 return true; 231 } 232 } 233 | Popular Tags |