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: DatabaseSessions.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.sessionmanagers;
009:
010: import com.uwyn.rife.authentication.sessionmanagers.exceptions.*;
011: import com.uwyn.rife.database.queries.*;
012:
013: import com.uwyn.rife.authentication.ListSessions;
014: import com.uwyn.rife.authentication.SessionManager;
015: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
016: import com.uwyn.rife.config.RifeConfig;
017: import com.uwyn.rife.database.Datasource;
018: import com.uwyn.rife.database.DbPreparedStatement;
019: import com.uwyn.rife.database.DbPreparedStatementHandler;
020: import com.uwyn.rife.database.DbQueryManager;
021: import com.uwyn.rife.database.DbRowProcessor;
022: import com.uwyn.rife.database.exceptions.DatabaseException;
023: import com.uwyn.rife.tools.UniqueID;
024: import com.uwyn.rife.tools.UniqueIDGenerator;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027:
028: public abstract class DatabaseSessions extends DbQueryManager implements
029: SessionManager {
030: private long mSessionDuration = RifeConfig.Authentication
031: .getSessionDuration();
032: private boolean mRestrictHostIp = RifeConfig.Authentication
033: .getSessionRestrictHostIp();
034:
035: protected DatabaseSessions(Datasource datasource) {
036: super (datasource);
037: }
038:
039: public long getSessionDuration() {
040: return mSessionDuration;
041: }
042:
043: public void setSessionDuration(long milliseconds) {
044: mSessionDuration = milliseconds;
045: }
046:
047: public boolean getRestrictHostIp() {
048: return mRestrictHostIp;
049: }
050:
051: public void setRestrictHostIp(boolean flag) {
052: mRestrictHostIp = flag;
053: }
054:
055: public abstract boolean install() throws SessionManagerException;
056:
057: public abstract boolean remove() throws SessionManagerException;
058:
059: public abstract long countSessions() throws SessionManagerException;
060:
061: protected boolean _install(CreateTable createAuthentication,
062: String createAuthenticationSessStartIndex) {
063: assert createAuthentication != null;
064: assert createAuthenticationSessStartIndex != null;
065:
066: executeUpdate(createAuthentication);
067: executeUpdate(createAuthenticationSessStartIndex);
068:
069: return true;
070: }
071:
072: protected boolean _remove(DropTable removeAuthentication,
073: String removeAuthenticationSessStartIndex) {
074: assert removeAuthentication != null;
075: assert removeAuthenticationSessStartIndex != null;
076:
077: executeUpdate(removeAuthenticationSessStartIndex);
078: executeUpdate(removeAuthentication);
079:
080: return true;
081: }
082:
083: protected void _purgeSessions(Delete purgeSession)
084: throws SessionManagerException {
085: try {
086: executeUpdate(purgeSession,
087: new DbPreparedStatementHandler() {
088: public void setParameters(
089: DbPreparedStatement statement) {
090: statement.setLong(1, System
091: .currentTimeMillis()
092: - getSessionDuration());
093: }
094: });
095: } catch (DatabaseException e) {
096: throw new PurgeSessionsErrorException(e);
097: }
098: }
099:
100: protected String _startSession(Insert startSession,
101: final long userId, final String hostIp,
102: final boolean remembered) throws SessionManagerException {
103: assert startSession != null;
104:
105: if (userId < 0 || null == hostIp || 0 == hostIp.length()) {
106: throw new StartSessionErrorException(userId, hostIp);
107: }
108:
109: final UniqueID auth_id = UniqueIDGenerator.generate(hostIp);
110: final String auth_id_string = auth_id.toString();
111:
112: try {
113: if (0 == executeUpdate(startSession,
114: new DbPreparedStatementHandler() {
115: public void setParameters(
116: DbPreparedStatement statement) {
117: statement.setString("authId",
118: auth_id_string).setLong("userId",
119: userId).setString("hostIp", hostIp)
120: .setLong("sessStart",
121: System.currentTimeMillis())
122: .setBoolean("remembered",
123: remembered);
124: }
125: })) {
126: throw new StartSessionErrorException(userId, hostIp);
127: }
128: } catch (DatabaseException e) {
129: throw new StartSessionErrorException(userId, hostIp, e);
130: }
131:
132: return auth_id_string;
133: }
134:
135: protected boolean _isSessionValid(Select sessionValidity,
136: Select sessionValidityRestrictHostIp, final String authId,
137: final String hostIp) throws SessionManagerException {
138: assert sessionValidity != null;
139: assert sessionValidityRestrictHostIp != null;
140:
141: if (null == authId || 0 == authId.length() || null == hostIp
142: || 0 == hostIp.length()) {
143: return false;
144: }
145:
146: boolean result = false;
147:
148: try {
149: Select query;
150: if (mRestrictHostIp) {
151: query = sessionValidityRestrictHostIp;
152: } else {
153: query = sessionValidity;
154: }
155: result = executeHasResultRows(query,
156: new DbPreparedStatementHandler() {
157: public void setParameters(
158: DbPreparedStatement statement) {
159: statement
160: .setString("authId", authId)
161: .setLong(
162: "sessStart",
163: System.currentTimeMillis()
164: - getSessionDuration());
165: if (mRestrictHostIp) {
166: statement.setString("hostIp", hostIp);
167: }
168: }
169: });
170: } catch (DatabaseException e) {
171: throw new IsSessionValidErrorException(authId, hostIp, e);
172: }
173:
174: return result;
175: }
176:
177: public boolean _continueSession(Update continueSession,
178: final String authId) throws SessionManagerException {
179: assert continueSession != null;
180:
181: if (null == authId || 0 == authId.length()) {
182: return false;
183: }
184:
185: boolean result = false;
186: try {
187: if (0 != executeUpdate(continueSession,
188: new DbPreparedStatementHandler() {
189: public void setParameters(
190: DbPreparedStatement statement) {
191: statement.setLong("sessStart",
192: System.currentTimeMillis())
193: .setString("authId", authId);
194: }
195: })) {
196: result = true;
197: }
198: } catch (DatabaseException e) {
199: throw new ContinueSessionErrorException(authId, e);
200: }
201:
202: return result;
203: }
204:
205: protected boolean _eraseSession(Delete eraseSession,
206: final String authId) throws SessionManagerException {
207: assert eraseSession != null;
208:
209: if (null == authId || 0 == authId.length()) {
210: return false;
211: }
212:
213: boolean result = false;
214: try {
215: if (0 != executeUpdate(eraseSession,
216: new DbPreparedStatementHandler() {
217: public void setParameters(
218: DbPreparedStatement statement) {
219: statement.setString("authId", authId);
220: }
221: })) {
222: result = true;
223: }
224: } catch (DatabaseException e) {
225: throw new EraseSessionErrorException(authId, e);
226: }
227:
228: return result;
229: }
230:
231: protected boolean _wasRemembered(Select wasRemembered,
232: final String authId) throws SessionManagerException {
233: assert wasRemembered != null;
234:
235: if (null == authId || 0 == authId.length()) {
236: return false;
237: }
238:
239: boolean result = false;
240:
241: try {
242: result = executeGetFirstBoolean(wasRemembered,
243: new DbPreparedStatementHandler() {
244: public void setParameters(
245: DbPreparedStatement statement) {
246: statement.setString("authId", authId);
247: }
248: });
249: } catch (DatabaseException e) {
250: throw new SessionRememberedCheckErrorException(authId, e);
251: }
252:
253: return result;
254: }
255:
256: protected boolean _eraseUserSessions(Delete eraseUserSessions,
257: final long userId) throws SessionManagerException {
258: assert eraseUserSessions != null;
259:
260: if (userId < 0) {
261: return false;
262: }
263:
264: boolean result = false;
265: try {
266: if (0 != executeUpdate(eraseUserSessions,
267: new DbPreparedStatementHandler() {
268: public void setParameters(
269: DbPreparedStatement statement) {
270: statement.setLong("userId", userId);
271: }
272: })) {
273: result = true;
274: }
275: } catch (DatabaseException e) {
276: throw new EraseUserSessionsErrorException(userId, e);
277: }
278:
279: return result;
280: }
281:
282: protected void _eraseAllSessions(Delete eraseAllSessions)
283: throws SessionManagerException {
284: assert eraseAllSessions != null;
285:
286: try {
287: executeUpdate(eraseAllSessions);
288: } catch (DatabaseException e) {
289: throw new EraseAllSessionsErrorException(e);
290: }
291: }
292:
293: protected long _countSessions(Select countSessions)
294: throws SessionManagerException {
295: assert countSessions != null;
296:
297: long result = -1;
298:
299: try {
300: result = executeGetFirstLong(countSessions,
301: new DbPreparedStatementHandler() {
302: public void setParameters(
303: DbPreparedStatement statement) {
304: statement.setLong("sessStart", System
305: .currentTimeMillis()
306: - getSessionDuration());
307: }
308: });
309:
310: } catch (DatabaseException e) {
311: throw new CountSessionsErrorException(e);
312: }
313:
314: return result;
315: }
316:
317: protected long _getSessionUserId(Select getSessionUserId,
318: final String authId) throws SessionManagerException {
319: assert getSessionUserId != null;
320:
321: if (null == authId || 0 == authId.length()) {
322: return -1;
323: }
324:
325: long result = -1;
326:
327: try {
328: result = executeGetFirstLong(getSessionUserId,
329: new DbPreparedStatementHandler() {
330: public void setParameters(
331: DbPreparedStatement statement) {
332: statement.setString("authId", authId);
333: }
334: });
335: } catch (DatabaseException e) {
336: throw new GetSessionUserIdErrorException(authId, e);
337: }
338:
339: return result;
340: }
341:
342: protected boolean _listSessions(Select listSessions,
343: final ListSessions processor)
344: throws SessionManagerException {
345: if (null == processor)
346: throw new IllegalArgumentException(
347: "processor can't be null");
348:
349: boolean result = false;
350:
351: try {
352: result = executeFetchAll(listSessions,
353: new DbRowProcessor() {
354: public boolean processRow(ResultSet resultSet)
355: throws SQLException {
356: return processor.foundSession(resultSet
357: .getInt("userId"), resultSet
358: .getString("hostIp"), resultSet
359: .getString("authId"));
360: }
361: }, new DbPreparedStatementHandler() {
362: public void setParameters(
363: DbPreparedStatement statement) {
364: statement.setLong("sessStart", System
365: .currentTimeMillis()
366: - getSessionDuration());
367: }
368: });
369:
370: } catch (DatabaseException e) {
371: throw new CountSessionsErrorException(e);
372: }
373:
374: return result;
375: }
376: }
|