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: generic.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.sessionmanagers.databasedrivers;
009:
010: import com.uwyn.rife.database.queries.*;
011:
012: import com.uwyn.rife.authentication.ListSessions;
013: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
014: import com.uwyn.rife.authentication.sessionmanagers.DatabaseSessions;
015: import com.uwyn.rife.config.RifeConfig;
016: import com.uwyn.rife.database.Datasource;
017:
018: public class generic extends DatabaseSessions {
019: protected CreateTable mCreateAuthentication = null;
020: protected String mCreateAuthenticationSessStartIndex = null;
021: protected Delete mPurgeSessions = null;
022: protected Insert mStartSession = null;
023: protected Select mIsSessionValid = null;
024: protected Select mIsSessionValidRestrictHostIp = null;
025: protected Update mContinueSession = null;
026: protected Delete mEraseSession = null;
027: protected Select mWasRemembered = null;
028: protected Delete mEraseAllSessions = null;
029: protected Delete mEraseUserSessions = null;
030: protected DropTable mRemoveAuthentication = null;
031: protected String mRemoveAuthenticationSessStartIndex = null;
032: protected Select mCountSessions = null;
033: protected Select mGetSessionUserId = null;
034: protected Select mListSessions = null;
035:
036: public generic(Datasource datasource) {
037: super (datasource);
038:
039: mCreateAuthentication = new CreateTable(getDatasource())
040: .table(
041: RifeConfig.Authentication
042: .getTableAuthentication())
043: .column("authId", String.class, 32, CreateTable.NOTNULL)
044: .column("userId", long.class, CreateTable.NOTNULL)
045: .column("hostIp", String.class, 40, CreateTable.NOTNULL)
046: .column("sessStart", long.class, CreateTable.NOTNULL)
047: .column("remembered", boolean.class,
048: CreateTable.NOTNULL).defaultValue("remembered",
049: false).primaryKey(
050: RifeConfig.Authentication
051: .getTableAuthentication().toUpperCase()
052: + "_PK", "authId");
053:
054: mCreateAuthenticationSessStartIndex = "CREATE INDEX "
055: + RifeConfig.Authentication.getTableAuthentication()
056: + "_IDX ON "
057: + RifeConfig.Authentication.getTableAuthentication()
058: + " (sessStart)";
059:
060: mPurgeSessions = new Delete(getDatasource()).from(
061: mCreateAuthentication.getTable()).whereParameter(
062: "sessStart", "<=");
063:
064: mStartSession = new Insert(getDatasource()).into(
065: mCreateAuthentication.getTable()).fieldParameter(
066: "authId").fieldParameter("userId").fieldParameter(
067: "hostIp").fieldParameter("sessStart").fieldParameter(
068: "remembered");
069:
070: mIsSessionValid = new Select(getDatasource()).from(
071: mCreateAuthentication.getTable()).whereParameter(
072: "authId", "=").whereParameterAnd("sessStart", ">");
073:
074: mIsSessionValidRestrictHostIp = new Select(getDatasource())
075: .from(mCreateAuthentication.getTable()).whereParameter(
076: "authId", "=").whereParameterAnd("hostIp", "=")
077: .whereParameterAnd("sessStart", ">");
078:
079: mContinueSession = new Update(getDatasource()).table(
080: mCreateAuthentication.getTable()).fieldParameter(
081: "sessStart").whereParameter("authId", "=");
082:
083: mEraseSession = new Delete(getDatasource()).from(
084: mCreateAuthentication.getTable()).whereParameter(
085: "authId", "=");
086:
087: mWasRemembered = new Select(getDatasource()).from(
088: mCreateAuthentication.getTable()).field("remembered")
089: .whereParameter("authId", "=");
090:
091: mEraseAllSessions = new Delete(getDatasource())
092: .from(mCreateAuthentication.getTable());
093:
094: mEraseUserSessions = new Delete(getDatasource()).from(
095: mCreateAuthentication.getTable()).whereParameter(
096: "userId", "=");
097:
098: mRemoveAuthentication = new DropTable(getDatasource())
099: .table(mCreateAuthentication.getTable());
100:
101: mRemoveAuthenticationSessStartIndex = "DROP INDEX "
102: + RifeConfig.Authentication.getTableAuthentication()
103: + "_IDX";
104:
105: mCountSessions = new Select(getDatasource()).field("count(*)")
106: .from(mCreateAuthentication.getTable()).whereParameter(
107: "sessStart", ">");
108:
109: mGetSessionUserId = new Select(getDatasource()).field("userId")
110: .from(mCreateAuthentication.getTable()).whereParameter(
111: "authId", "=");
112:
113: mListSessions = new Select(getDatasource()).from(
114: mCreateAuthentication.getTable()).whereParameter(
115: "sessStart", ">");
116: }
117:
118: public boolean install() throws SessionManagerException {
119: return _install(mCreateAuthentication,
120: mCreateAuthenticationSessStartIndex);
121: }
122:
123: public boolean remove() throws SessionManagerException {
124: return _remove(mRemoveAuthentication,
125: mRemoveAuthenticationSessStartIndex);
126: }
127:
128: public void purgeSessions() throws SessionManagerException {
129: _purgeSessions(mPurgeSessions);
130: }
131:
132: public String startSession(long userId, String hostIp,
133: boolean remembered) throws SessionManagerException {
134: return _startSession(mStartSession, userId, hostIp, remembered);
135: }
136:
137: public boolean isSessionValid(String authId, String hostIp)
138: throws SessionManagerException {
139: return _isSessionValid(mIsSessionValid,
140: mIsSessionValidRestrictHostIp, authId, hostIp);
141: }
142:
143: public boolean continueSession(String authId)
144: throws SessionManagerException {
145: return _continueSession(mContinueSession, authId);
146: }
147:
148: public boolean eraseSession(String authId)
149: throws SessionManagerException {
150: return _eraseSession(mEraseSession, authId);
151: }
152:
153: public boolean wasRemembered(String authId)
154: throws SessionManagerException {
155: return _wasRemembered(mWasRemembered, authId);
156: }
157:
158: public void eraseAllSessions() throws SessionManagerException {
159: _eraseAllSessions(mEraseAllSessions);
160: }
161:
162: public boolean eraseUserSessions(long userId)
163: throws SessionManagerException {
164: return _eraseUserSessions(mEraseUserSessions, userId);
165: }
166:
167: public long countSessions() throws SessionManagerException {
168: return _countSessions(mCountSessions);
169: }
170:
171: public long getSessionUserId(String authId)
172: throws SessionManagerException {
173: return _getSessionUserId(mGetSessionUserId, authId);
174: }
175:
176: public boolean listSessions(ListSessions processor)
177: throws SessionManagerException {
178: return _listSessions(mListSessions, processor);
179: }
180: }
|