001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: PurgingSessionManager.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.sessionmanagers;
009:
010: import com.uwyn.rife.authentication.ListSessions;
011: import com.uwyn.rife.authentication.SessionManager;
012: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
013: import com.uwyn.rife.config.RifeConfig;
014: import java.util.Random;
015:
016: public class PurgingSessionManager implements SessionManager {
017: private int mSessionPurgeFrequency = RifeConfig.Authentication
018: .getSessionPurgeFrequency();
019: private int mSessionPurgeScale = RifeConfig.Authentication
020: .getSessionPurgeScale();
021:
022: private final Random mRandom = new Random();
023:
024: private SessionManager mSessionManager = null;
025:
026: public PurgingSessionManager(SessionManager sessionManager) {
027: if (null == sessionManager)
028: throw new IllegalArgumentException(
029: "sessionManager can't be null");
030:
031: mSessionManager = sessionManager;
032: }
033:
034: public SessionManager getSessionManager() {
035: return mSessionManager;
036: }
037:
038: public int getSessionPurgeFrequency() {
039: return mSessionPurgeFrequency;
040: }
041:
042: public void setSessionPurgeFrequency(int frequency) {
043: mSessionPurgeFrequency = frequency;
044: }
045:
046: public int getSessionPurgeScale() {
047: return mSessionPurgeScale;
048: }
049:
050: public void setSessionPurgeScale(int scale) {
051: mSessionPurgeScale = scale;
052: }
053:
054: public String startSession(long userId, String hostIp,
055: boolean remembered) throws SessionManagerException {
056: int purge_decision = -1;
057: synchronized (mRandom) {
058: purge_decision = mRandom.nextInt(mSessionPurgeScale);
059: }
060: if (purge_decision <= mSessionPurgeFrequency) {
061: purgeSessions();
062: }
063:
064: return mSessionManager.startSession(userId, hostIp, remembered);
065: }
066:
067: public void setSessionDuration(final long milliseconds) {
068: mSessionManager.setSessionDuration(milliseconds);
069: }
070:
071: public long getSessionDuration() {
072: return mSessionManager.getSessionDuration();
073: }
074:
075: public boolean getRestrictHostIp() {
076: return mSessionManager.getRestrictHostIp();
077: }
078:
079: public void setRestrictHostIp(boolean flag) {
080: mSessionManager.setRestrictHostIp(flag);
081: }
082:
083: public void eraseAllSessions() throws SessionManagerException {
084: mSessionManager.eraseAllSessions();
085: }
086:
087: public boolean isSessionValid(final String authId,
088: final String hostIp) throws SessionManagerException {
089: return mSessionManager.isSessionValid(authId, hostIp);
090: }
091:
092: public boolean continueSession(final String authId)
093: throws SessionManagerException {
094: return mSessionManager.continueSession(authId);
095: }
096:
097: public long getSessionUserId(final String authId)
098: throws SessionManagerException {
099: return mSessionManager.getSessionUserId(authId);
100: }
101:
102: public void purgeSessions() throws SessionManagerException {
103: mSessionManager.purgeSessions();
104: }
105:
106: public boolean eraseSession(String authId)
107: throws SessionManagerException {
108: return mSessionManager.eraseSession(authId);
109: }
110:
111: public boolean wasRemembered(String authId)
112: throws SessionManagerException {
113: return mSessionManager.wasRemembered(authId);
114: }
115:
116: public boolean eraseUserSessions(long userId)
117: throws SessionManagerException {
118: return mSessionManager.eraseUserSessions(userId);
119: }
120:
121: public long countSessions() throws SessionManagerException {
122: return mSessionManager.countSessions();
123: }
124:
125: public boolean listSessions(ListSessions processor)
126: throws SessionManagerException {
127: return mSessionManager.listSessions(processor);
128: }
129: }
|