1 2 24 25 package com.lutris.appserver.server.sessionEnhydra; 26 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 30 import com.lutris.appserver.server.Enhydra; 31 import com.lutris.appserver.server.session.SessionException; 32 import com.lutris.logging.Logger; 33 import com.lutris.util.Config; 34 import com.lutris.util.ConfigException; 35 import com.lutris.util.FilePersistentStore; 36 import com.lutris.util.PersistentStore; 37 import com.lutris.util.PersistentStoreException; 38 39 104 public class DiskPagedSessionHome extends PagedSessionHome { 105 106 110 private Hashtable pagedCache = new Hashtable (); 111 112 115 private PersistentStore store; 116 117 120 private final String PAGE_DIR_KEY = "PageDir"; 121 122 126 private final String SAVE_ON_RESTART_KEY = "SaveOnRestart"; 127 private boolean saveSessions = false; 128 String saveSess = "false"; 129 130 131 144 public DiskPagedSessionHome(StandardSessionManager sessionMgr, 145 Config config, ClassLoader loader) 146 throws SessionException, ConfigException { 147 super(sessionMgr, config, loader); 148 String pageDir = config.getString(PAGE_DIR_KEY); 149 152 if(config.containsKey(SAVE_ON_RESTART_KEY)){ 153 saveSess = config.getString(SAVE_ON_RESTART_KEY); 154 if(saveSess.equals("true")) saveSessions = true; 155 } 156 debug(PAGE_DIR_KEY + " = " + pageDir); 157 try { 158 store = new FilePersistentStore(pageDir, loader); 159 162 if(saveSessions) 163 loadPagedSessions(); 164 } catch (Exception e) { 165 throw new SessionException(e); 166 } 167 } 168 169 175 protected PagedSession newSession(StandardSessionManager mgr, 176 String sessionKey) 177 throws SessionException { 178 return new PagedSession(mgr, sessionKey); 179 } 180 181 188 protected void deleteSession(String sessionKey) 189 throws SessionException { 190 PagedSessionHandle pagedSession = 191 (PagedSessionHandle)pagedCache.remove(sessionKey); 192 if (pagedSession != null) { 193 pagedSession.delete(); 194 } 195 } 196 197 204 protected synchronized void pageOut(PagedSession s) throws SessionException { 205 debug("page: write session to disk: " + s.getSessionKey()); 207 PagedSessionHandle page = new PagedSessionHandle(s, store); 208 page.write(); 209 pagedCache.put(s.getSessionKey(), page); 210 } 211 212 221 protected synchronized PagedSession pageIn(String sessionKey) throws SessionException { 222 PagedSessionHandle sessHandle = 223 (PagedSessionHandle)pagedCache.get(sessionKey); 224 PagedSession session = null; 225 try { 226 if (sessHandle != null) { 227 debug("page: read session from disk: " + sessHandle.getSessionKey()); 229 session = sessHandle.read(); 230 debug("page: read data: " + session); 232 sessHandle.delete(); 234 } 235 } catch (SessionException e) { 236 Enhydra.getLogChannel().write(Logger.ALERT, 237 "Session not found on disk.", e); 238 } 239 pagedCache.remove(sessionKey); 240 return session; 241 } 242 243 248 protected synchronized int getPagedSessionCount() throws SessionException { 249 return pagedCache.size(); 250 } 251 252 259 protected boolean pagedSessionKeyExists(String sessionKey) 260 throws SessionException { 261 return pagedCache.containsKey(sessionKey); 262 } 263 264 271 protected Enumeration getPagedSessionKeys() throws SessionException { 272 return pagedCache.keys(); 273 } 274 275 280 protected boolean cleanupNewPagedSession() throws SessionException { 281 long oldestTime = -1; 282 String key = null; 283 Enumeration e = pagedCache.keys(); 284 while (e.hasMoreElements()) { 285 PagedSessionHandle s = (PagedSessionHandle)pagedCache.get(e.nextElement()); 286 if (s.isNew()) { 287 if ((oldestTime < 0) 288 || (s.getTimeCreated() < oldestTime)) { 289 oldestTime = s.getTimeCreated(); 290 key = s.getSessionKey(); 291 } 292 } 293 } 294 if (key != null) { 295 removeSession(key); 296 return true; 297 } 298 return false; 299 } 300 301 305 306 public void shutdown() { 307 311 if(saveSessions){ 312 super.shutdown(); 313 }else{ 314 Enumeration enumeration = pagedCache.elements(); 315 for (int i=0; i<pagedCache.size(); i++) { 316 try { 317 ((PagedSessionHandle)enumeration.nextElement()).delete(); 318 } catch (Exception e) { 319 } 321 } 322 } 323 } 324 327 private void loadPagedSessions() 328 throws PersistentStoreException{ 329 Enumeration enumeration = store.keys(); 330 while (enumeration.hasMoreElements()){ 331 String sessionKey = (String ) enumeration.nextElement(); 332 PagedSession session = (PagedSession) store.retrieve(sessionKey); 333 Object [] obj = {sessionMgr} ; 334 session.restoreTransientData(obj); 335 PagedSessionHandle psh = new PagedSessionHandle(session,store); 336 session = null; 337 pagedCache.put( sessionKey,psh); 338 } 339 } 340 341 342 } 343 344 345 346 347 | Popular Tags |