1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.session.mgt;
20
21 import org.apache.shiro.cache.CacheManager;
22 import org.apache.shiro.cache.CacheManagerAware;
23 import org.apache.shiro.session.Session;
24 import org.apache.shiro.session.UnknownSessionException;
25 import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
26 import org.apache.shiro.session.mgt.eis.SessionDAO;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.Serializable;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Date;
34
35
36
37
38
39
40
41 public class DefaultSessionManager extends AbstractValidatingSessionManager implements CacheManagerAware {
42
43
44
45 private static final Logger log = LoggerFactory.getLogger(DefaultSessionManager.class);
46
47 private SessionFactory sessionFactory;
48
49 protected SessionDAO sessionDAO;
50
51 private CacheManager cacheManager;
52
53 private boolean deleteInvalidSessions;
54
55 public DefaultSessionManager() {
56 this.deleteInvalidSessions = true;
57 this.sessionFactory = new SimpleSessionFactory();
58 this.sessionDAO = new MemorySessionDAO();
59 }
60
61 public void setSessionDAO(SessionDAO sessionDAO) {
62 this.sessionDAO = sessionDAO;
63 applyCacheManagerToSessionDAO();
64 }
65
66 public SessionDAO getSessionDAO() {
67 return this.sessionDAO;
68 }
69
70
71
72
73
74
75
76
77 public SessionFactory getSessionFactory() {
78 return sessionFactory;
79 }
80
81
82
83
84
85
86
87
88 public void setSessionFactory(SessionFactory sessionFactory) {
89 this.sessionFactory = sessionFactory;
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public boolean isDeleteInvalidSessions() {
111 return deleteInvalidSessions;
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126 @SuppressWarnings({"UnusedDeclaration"})
127 public void setDeleteInvalidSessions(boolean deleteInvalidSessions) {
128 this.deleteInvalidSessions = deleteInvalidSessions;
129 }
130
131 public void setCacheManager(CacheManager cacheManager) {
132 this.cacheManager = cacheManager;
133 applyCacheManagerToSessionDAO();
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147 private void applyCacheManagerToSessionDAO() {
148 if (this.cacheManager != null && this.sessionDAO != null && this.sessionDAO instanceof CacheManagerAware) {
149 ((CacheManagerAware) this.sessionDAO).setCacheManager(this.cacheManager);
150 }
151 }
152
153 protected Session doCreateSession(SessionContext context) {
154 Session s = newSessionInstance(context);
155 if (log.isTraceEnabled()) {
156 log.trace("Creating session for host {}", s.getHost());
157 }
158 create(s);
159 return s;
160 }
161
162 protected Session newSessionInstance(SessionContext context) {
163 return getSessionFactory().createSession(context);
164 }
165
166
167
168
169
170
171
172
173 protected void create(Session session) {
174 if (log.isDebugEnabled()) {
175 log.debug("Creating new EIS record for new session instance [" + session + "]");
176 }
177 sessionDAO.create(session);
178 }
179
180 @Override
181 protected void onStop(Session session) {
182 if (session instanceof SimpleSession) {
183 SimpleSession ss = (SimpleSession) session;
184 Date stopTs = ss.getStopTimestamp();
185 ss.setLastAccessTime(stopTs);
186 }
187 onChange(session);
188 }
189
190 @Override
191 protected void afterStopped(Session session) {
192 if (isDeleteInvalidSessions()) {
193 delete(session);
194 }
195 }
196
197 protected void onExpiration(Session session) {
198 if (session instanceof SimpleSession) {
199 ((SimpleSession) session).setExpired(true);
200 }
201 onChange(session);
202 }
203
204 @Override
205 protected void afterExpired(Session session) {
206 if (isDeleteInvalidSessions()) {
207 delete(session);
208 }
209 }
210
211 protected void onChange(Session session) {
212 sessionDAO.update(session);
213 }
214
215 protected Session retrieveSession(SessionKey sessionKey) throws UnknownSessionException {
216 Serializable sessionId = getSessionId(sessionKey);
217 if (sessionId == null) {
218 log.debug("Unable to resolve session ID from SessionKey [{}]. Returning null to indicate a " +
219 "session could not be found.", sessionKey);
220 return null;
221 }
222 Session s = retrieveSessionFromDataSource(sessionId);
223 if (s == null) {
224
225 String msg = "Could not find session with ID [" + sessionId + "]";
226 throw new UnknownSessionException(msg);
227 }
228 return s;
229 }
230
231 protected Serializable getSessionId(SessionKey sessionKey) {
232 return sessionKey.getSessionId();
233 }
234
235 protected Session retrieveSessionFromDataSource(Serializable sessionId) throws UnknownSessionException {
236 return sessionDAO.readSession(sessionId);
237 }
238
239 protected void delete(Session session) {
240 sessionDAO.delete(session);
241 }
242
243 protected Collection<Session> getActiveSessions() {
244 Collection<Session> active = sessionDAO.getActiveSessions();
245 return active != null ? active : Collections.<Session>emptySet();
246 }
247
248 }