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