Source Code Cross Referenced for DatabaseRemember.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » authentication » remembermanagers » 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.remembermanagers 
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: DatabaseRemember.java 3634 2007-01-08 21:42:24Z gbevin $
007:         */
008:        package com.uwyn.rife.authentication.remembermanagers;
009:
010:        import com.uwyn.rife.authentication.remembermanagers.exceptions.*;
011:
012:        import com.uwyn.rife.authentication.RememberManager;
013:        import com.uwyn.rife.authentication.exceptions.RememberManagerException;
014:        import com.uwyn.rife.config.RifeConfig;
015:        import com.uwyn.rife.database.Datasource;
016:        import com.uwyn.rife.database.DbPreparedStatement;
017:        import com.uwyn.rife.database.DbPreparedStatementHandler;
018:        import com.uwyn.rife.database.DbQueryManager;
019:        import com.uwyn.rife.database.exceptions.DatabaseException;
020:        import com.uwyn.rife.database.queries.CreateTable;
021:        import com.uwyn.rife.database.queries.Delete;
022:        import com.uwyn.rife.database.queries.DropTable;
023:        import com.uwyn.rife.database.queries.Insert;
024:        import com.uwyn.rife.database.queries.Select;
025:        import com.uwyn.rife.tools.StringEncryptor;
026:        import com.uwyn.rife.tools.UniqueID;
027:        import com.uwyn.rife.tools.UniqueIDGenerator;
028:        import java.security.NoSuchAlgorithmException;
029:
030:        public abstract class DatabaseRemember extends DbQueryManager implements 
031:                RememberManager {
032:            private long mRememberDuration = RifeConfig.Authentication
033:                    .getRememberDuration();
034:
035:            protected DatabaseRemember(Datasource datasource) {
036:                super (datasource);
037:            }
038:
039:            public long getRememberDuration() {
040:                return mRememberDuration;
041:            }
042:
043:            public void setRememberDuration(long milliseconds) {
044:                mRememberDuration = milliseconds;
045:            }
046:
047:            public abstract boolean install() throws RememberManagerException;
048:
049:            public abstract boolean remove() throws RememberManagerException;
050:
051:            protected boolean _install(CreateTable createRemember,
052:                    String createRememberMomentIndex) {
053:                executeUpdate(createRemember);
054:                executeUpdate(createRememberMomentIndex);
055:
056:                return true;
057:            }
058:
059:            protected boolean _remove(DropTable removeRemember,
060:                    String removeRememberMomentIndex) {
061:                executeUpdate(removeRememberMomentIndex);
062:                executeUpdate(removeRemember);
063:
064:                return true;
065:            }
066:
067:            protected String _createRememberId(Insert createRememberId,
068:                    final long userId, String hostIp)
069:                    throws RememberManagerException {
070:                assert createRememberId != null;
071:
072:                if (userId < 0) {
073:                    throw new CreateRememberIdErrorException(userId);
074:                }
075:
076:                final UniqueID remember_id = UniqueIDGenerator.generate(hostIp);
077:                final String remember_id_string = remember_id.toString();
078:
079:                try {
080:                    if (0 == executeUpdate(createRememberId,
081:                            new DbPreparedStatementHandler() {
082:                                public void setParameters(
083:                                        DbPreparedStatement statement) {
084:                                    statement.setString("rememberId",
085:                                            remember_id_string).setLong(
086:                                            "userId", userId).setLong("moment",
087:                                            System.currentTimeMillis());
088:                                }
089:                            })) {
090:                        throw new CreateRememberIdErrorException(userId);
091:                    }
092:                } catch (DatabaseException e) {
093:                    throw new CreateRememberIdErrorException(userId, e);
094:                }
095:
096:                try {
097:                    return StringEncryptor.SHA.encrypt(String.valueOf(userId))
098:                            + "|" + remember_id_string;
099:                } catch (NoSuchAlgorithmException e) {
100:                    throw new CreateRememberIdErrorException(userId, e);
101:                }
102:            }
103:
104:            protected boolean _eraseRememberId(Delete eraseRememberId,
105:                    final String rememberId) throws RememberManagerException {
106:                assert eraseRememberId != null;
107:
108:                if (null == rememberId || 0 == rememberId.length()) {
109:                    return false;
110:                }
111:
112:                final int rememberid_slash = rememberId.indexOf("|");
113:                if (-1 == rememberid_slash) {
114:                    return false;
115:                }
116:
117:                boolean result = false;
118:                try {
119:                    if (0 != executeUpdate(eraseRememberId,
120:                            new DbPreparedStatementHandler() {
121:                                public void setParameters(
122:                                        DbPreparedStatement statement) {
123:                                    statement
124:                                            .setString(
125:                                                    "rememberId",
126:                                                    rememberId
127:                                                            .substring(rememberid_slash + 1));
128:                                }
129:                            })) {
130:                        result = true;
131:                    }
132:                } catch (DatabaseException e) {
133:                    throw new EraseRememberIdErrorException(rememberId, e);
134:                }
135:
136:                return result;
137:            }
138:
139:            protected boolean _eraseUserRememberIds(
140:                    Delete eraseUserRememberIds, final long userId)
141:                    throws RememberManagerException {
142:                assert eraseUserRememberIds != null;
143:
144:                if (userId < 0) {
145:                    return false;
146:                }
147:
148:                boolean result = false;
149:                try {
150:                    if (0 != executeUpdate(eraseUserRememberIds,
151:                            new DbPreparedStatementHandler() {
152:                                public void setParameters(
153:                                        DbPreparedStatement statement) {
154:                                    statement.setLong("userId", userId);
155:                                }
156:                            })) {
157:                        result = true;
158:                    }
159:                } catch (DatabaseException e) {
160:                    throw new EraseUserRememberIdsErrorException(userId, e);
161:                }
162:
163:                return result;
164:            }
165:
166:            protected void _eraseAllRememberIds(Delete eraseAllRememberIds)
167:                    throws RememberManagerException {
168:                assert eraseAllRememberIds != null;
169:
170:                try {
171:                    executeUpdate(eraseAllRememberIds);
172:                } catch (DatabaseException e) {
173:                    throw new EraseAllRememberIdsErrorException(e);
174:                }
175:            }
176:
177:            protected long _getRememberedUserId(Select getRememberedUserId,
178:                    final String rememberId) throws RememberManagerException {
179:                assert getRememberedUserId != null;
180:
181:                if (null == rememberId || 0 == rememberId.length()) {
182:                    return -1;
183:                }
184:
185:                final int rememberid_slash = rememberId.indexOf("|");
186:                if (-1 == rememberid_slash) {
187:                    return -1;
188:                }
189:
190:                final String encrypted_userid = rememberId.substring(0,
191:                        rememberid_slash);
192:                final String real_rememberid = rememberId
193:                        .substring(rememberid_slash + 1);
194:
195:                long result = -1;
196:
197:                try {
198:                    result = executeGetFirstLong(getRememberedUserId,
199:                            new DbPreparedStatementHandler() {
200:                                public void setParameters(
201:                                        DbPreparedStatement statement) {
202:                                    statement.setString("rememberId",
203:                                            real_rememberid);
204:                                }
205:                            });
206:                } catch (DatabaseException e) {
207:                    throw new GetRememberedUserIdErrorException(rememberId, e);
208:                }
209:
210:                try {
211:                    if (!encrypted_userid.equals(StringEncryptor.SHA
212:                            .encrypt(String.valueOf(result)))) {
213:                        return -1;
214:                    }
215:                } catch (NoSuchAlgorithmException e) {
216:                    throw new GetRememberedUserIdErrorException(rememberId, e);
217:                }
218:
219:                return result;
220:            }
221:
222:            protected void _purgeRememberIds(Delete purgeRememberIds)
223:                    throws RememberManagerException {
224:                try {
225:                    executeUpdate(purgeRememberIds,
226:                            new DbPreparedStatementHandler() {
227:                                public void setParameters(
228:                                        DbPreparedStatement statement) {
229:                                    statement.setLong(1, System
230:                                            .currentTimeMillis()
231:                                            - getRememberDuration());
232:                                }
233:                            });
234:                } catch (DatabaseException e) {
235:                    throw new PurgeRememberIdsErrorException(e);
236:                }
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.