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: MemorySessions.java 3732 2007-05-02 20:45:59Z 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.authentication.sessionmanagers.exceptions.StartSessionErrorException;
014: import com.uwyn.rife.config.RifeConfig;
015: import com.uwyn.rife.tools.UniqueID;
016: import com.uwyn.rife.tools.UniqueIDGenerator;
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: public class MemorySessions implements SessionManager {
022: private long mSessionDuration = RifeConfig.Authentication
023: .getSessionDuration();
024: private boolean mRestrictHostIp = RifeConfig.Authentication
025: .getSessionRestrictHostIp();
026:
027: private final Map<String, MemorySession> mSessions = new HashMap<String, MemorySession>();
028:
029: MemorySessions() {
030: }
031:
032: public long getSessionDuration() {
033: return mSessionDuration;
034: }
035:
036: public void setSessionDuration(long milliseconds) {
037: mSessionDuration = milliseconds;
038: }
039:
040: public boolean getRestrictHostIp() {
041: return mRestrictHostIp;
042: }
043:
044: public void setRestrictHostIp(boolean flags) {
045: mRestrictHostIp = flags;
046: }
047:
048: public void purgeSessions() {
049: new PurgeSessions().start();
050: }
051:
052: private class PurgeSessions extends Thread {
053: public void run() {
054: synchronized (mSessions) {
055: ArrayList<String> stale_sessions = new ArrayList<String>();
056: long expiration = System.currentTimeMillis()
057: - getSessionDuration();
058: for (MemorySession session : mSessions.values()) {
059: if (session.getStart() <= expiration) {
060: stale_sessions.add(session.getAuthId());
061: }
062: }
063:
064: if (stale_sessions != null) {
065: for (String authid : stale_sessions) {
066: mSessions.remove(authid);
067: }
068: }
069: }
070: }
071: }
072:
073: public String startSession(long userId, String hostIp,
074: boolean remembered) throws SessionManagerException {
075: if (userId < 0 || null == hostIp || 0 == hostIp.length()) {
076: throw new StartSessionErrorException(userId, hostIp);
077: }
078:
079: UniqueID auth_id = UniqueIDGenerator.generate(hostIp);
080: String auth_id_string = auth_id.toString();
081:
082: MemorySession session = new MemorySession(auth_id_string,
083: userId, hostIp, remembered);
084:
085: synchronized (mSessions) {
086: mSessions.put(auth_id_string, session);
087: }
088:
089: return auth_id_string;
090: }
091:
092: public boolean isSessionValid(String authId, String hostIp)
093: throws SessionManagerException {
094: if (null == authId || 0 == authId.length() || null == hostIp
095: || 0 == hostIp.length()) {
096: return false;
097: }
098:
099: MemorySession session = getSession(authId);
100:
101: if (session != null) {
102: if (session.getStart() > System.currentTimeMillis()
103: - getSessionDuration()
104: && (!mRestrictHostIp || session.getHostIp().equals(
105: hostIp))) {
106: return true;
107: }
108: }
109:
110: return false;
111: }
112:
113: public long getSessionUserId(String authId)
114: throws SessionManagerException {
115: MemorySession session = mSessions.get(authId);
116:
117: if (null == session) {
118: return -1;
119: }
120:
121: return session.getUserId();
122: }
123:
124: public boolean continueSession(String authId)
125: throws SessionManagerException {
126: if (null == authId || 0 == authId.length()) {
127: return false;
128: }
129:
130: synchronized (mSessions) {
131: if (mSessions.containsKey(authId)) {
132: MemorySession session = mSessions.get(authId);
133: session.setStart(System.currentTimeMillis());
134: mSessions.put(authId, session);
135:
136: return true;
137: }
138: }
139:
140: return false;
141: }
142:
143: public boolean eraseSession(String authId)
144: throws SessionManagerException {
145: if (null == authId || 0 == authId.length()) {
146: return false;
147: }
148:
149: synchronized (mSessions) {
150: if (mSessions.containsKey(authId)) {
151: mSessions.remove(authId);
152:
153: return true;
154: }
155: }
156:
157: return false;
158: }
159:
160: public boolean wasRemembered(String authId)
161: throws SessionManagerException {
162: if (null == authId || 0 == authId.length()) {
163: return false;
164: }
165:
166: synchronized (mSessions) {
167: MemorySession session = mSessions.get(authId);
168: if (null == session) {
169: return false;
170: }
171:
172: return session.getRemembered();
173: }
174: }
175:
176: public boolean eraseUserSessions(long userId)
177: throws SessionManagerException {
178: if (userId < 0) {
179: return false;
180: }
181:
182: boolean result = false;
183:
184: synchronized (mSessions) {
185: ArrayList<String> sessions_to_erase = new ArrayList<String>();
186:
187: // collect the sessions that have to be erased
188: for (Map.Entry<String, MemorySession> sessions_entry : mSessions
189: .entrySet()) {
190: if (userId == sessions_entry.getValue().getUserId()) {
191: sessions_to_erase.add(sessions_entry.getKey());
192: }
193: }
194:
195: // erased the collected sessions
196: for (String authid : sessions_to_erase) {
197: mSessions.remove(authid);
198: }
199:
200: if (sessions_to_erase.size() > 0) {
201: result = true;
202: }
203: }
204:
205: return result;
206: }
207:
208: public void eraseAllSessions() throws SessionManagerException {
209: mSessions.clear();
210: }
211:
212: public MemorySession getSession(String authId) {
213: return mSessions.get(authId);
214: }
215:
216: public long countSessions() {
217: long valid_session_count = 0;
218: synchronized (mSessions) {
219: long expiration = System.currentTimeMillis()
220: - getSessionDuration();
221: for (MemorySession session : mSessions.values()) {
222: if (session.getStart() > expiration) {
223: valid_session_count++;
224: }
225: }
226: }
227: return valid_session_count;
228: }
229:
230: public boolean listSessions(ListSessions processor) {
231: if (null == processor)
232: throw new IllegalArgumentException(
233: "processor can't be null");
234:
235: boolean result = false;
236:
237: synchronized (mSessions) {
238: long expiration = System.currentTimeMillis()
239: - getSessionDuration();
240: for (MemorySession session : mSessions.values()) {
241: if (session.getStart() > expiration) {
242: result = true;
243: if (!processor.foundSession(session.getUserId(),
244: session.getHostIp(), session.getAuthId())) {
245: break;
246: }
247: }
248: }
249: }
250:
251: return result;
252: }
253: }
|