Source Code Cross Referenced for DatabaseSessions.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » authentication » sessionmanagers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.authentication.sessionmanagers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.