1 18 19 package sync4j.framework.server.session; 20 21 import java.io.Serializable ; 22 23 import java.util.Collection ; 24 import java.util.logging.Logger ; 25 import java.util.logging.Level ; 26 27 import sync4j.framework.logging.Sync4jLogger; 28 import sync4j.framework.server.session.SessionHandler; 29 import sync4j.framework.server.session.SessionExpiredException; 30 31 38 public abstract class AbstractSessionManager implements Serializable { 39 40 42 45 private int lifeTime = 5; 46 47 50 public int getLifeTime() { 51 return lifeTime; 52 } 53 54 57 public void setLifeTime(int lifeTime) { 58 this.lifeTime = lifeTime; 59 } 60 61 62 64 67 private static final Logger log = Sync4jLogger.getLogger(); 68 69 71 public SessionHandler getSessionHandler(String sessionId) 72 throws SessionExpiredException { 73 if (log.isLoggable(Level.FINEST)) { 74 log.finest("Looking for session " + sessionId); 75 } 76 77 SessionHandler handler = getSessionFromBag(sessionId); 78 79 if (handler == null) { 80 handler = createNewSession(sessionId); 81 putSessionInBag(sessionId, handler); 82 } else { 83 if (handler.isNew()) { 84 handler.setNew(false); 85 putSessionInBag(sessionId, handler); 86 } 87 } 88 89 return handler; 90 } 91 92 public void removeSession(String sessionId) { 93 if (log.isLoggable(Level.FINEST)) { 94 log.finest("Removing session " + sessionId); 95 } 96 97 try { 98 SessionHandler oldSession = getSessionFromBag(sessionId); 99 oldSession.expire(); 100 removeSessionFromBag(sessionId); 101 } catch (SessionExpiredException e) { 102 if (log.isLoggable(Level.FINEST)) { 103 log.finest("Session already expired"); 104 } 105 } 106 107 if (log.isLoggable(Level.FINEST)) { 108 log.finest("Remaining sessions: " + getSessionBag()); 109 } 110 } 111 112 public void removeSession(SessionHandler handler) { 113 if (log.isLoggable(Level.FINEST)) { 114 log.finest("Removing session " + ((handler!=null) ? handler.getSessionId() : "null")); 115 } 116 117 handler.expire(); 118 removeSessionFromBag(handler.getSessionId()); 119 120 if (log.isLoggable(Level.FINEST)) { 121 log.finest("Remaining sessions: " + getSessionBag()); 122 } 123 } 124 125 public void storeSessionHandler(SessionHandler handler) { 126 log.fine("Storing session " + handler); 127 128 129 130 putSessionInBag(handler.getSessionId(), handler); 131 } 132 133 134 136 abstract protected SessionHandler createNewSession(String sessionId); 137 abstract protected SessionHandler getSessionFromBag(String sessionId) throws SessionExpiredException; 138 abstract protected void putSessionInBag(String sessionId, SessionHandler handler); 139 abstract protected void removeSessionFromBag(String sessionId); 140 abstract protected Collection getSessionBag(); 141 } 142 143 | Popular Tags |