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.authz.AuthorizationException; |
22 | |
import org.apache.shiro.session.*; |
23 | |
import org.apache.shiro.util.CollectionUtils; |
24 | |
import org.slf4j.Logger; |
25 | |
import org.slf4j.LoggerFactory; |
26 | |
|
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Collection; |
29 | |
import java.util.Collections; |
30 | |
import java.util.Date; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public abstract class AbstractNativeSessionManager extends AbstractSessionManager implements NativeSessionManager { |
40 | |
|
41 | 1 | private static final Logger log = LoggerFactory.getLogger(AbstractSessionManager.class); |
42 | |
|
43 | |
private Collection<SessionListener> listeners; |
44 | |
|
45 | 45 | public AbstractNativeSessionManager() { |
46 | 45 | this.listeners = new ArrayList<SessionListener>(); |
47 | 45 | } |
48 | |
|
49 | |
public void setSessionListeners(Collection<SessionListener> listeners) { |
50 | 2 | this.listeners = listeners != null ? listeners : new ArrayList<SessionListener>(); |
51 | 2 | } |
52 | |
|
53 | |
@SuppressWarnings({"UnusedDeclaration"}) |
54 | |
public Collection<SessionListener> getSessionListeners() { |
55 | 4 | return this.listeners; |
56 | |
} |
57 | |
|
58 | |
public Session start(SessionContext context) { |
59 | 28 | Session session = createSession(context); |
60 | 28 | applyGlobalSessionTimeout(session); |
61 | 28 | onStart(session, context); |
62 | 28 | notifyStart(session); |
63 | |
|
64 | 28 | return createExposedSession(session, context); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
protected abstract Session createSession(SessionContext context) throws AuthorizationException; |
82 | |
|
83 | |
protected void applyGlobalSessionTimeout(Session session) { |
84 | 28 | session.setTimeout(getGlobalSessionTimeout()); |
85 | 28 | onChange(session); |
86 | 28 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected void onStart(Session session, SessionContext context) { |
97 | 28 | } |
98 | |
|
99 | |
public Session getSession(SessionKey key) throws SessionException { |
100 | 0 | Session session = lookupSession(key); |
101 | 0 | return session != null ? createExposedSession(session, key) : null; |
102 | |
} |
103 | |
|
104 | |
private Session lookupSession(SessionKey key) throws SessionException { |
105 | 203 | if (key == null) { |
106 | 0 | throw new NullPointerException("SessionKey argument cannot be null."); |
107 | |
} |
108 | 203 | return doGetSession(key); |
109 | |
} |
110 | |
|
111 | |
private Session lookupRequiredSession(SessionKey key) throws SessionException { |
112 | 203 | Session session = lookupSession(key); |
113 | 199 | if (session == null) { |
114 | 0 | String msg = "Unable to locate required Session instance based on SessionKey [" + key + "]."; |
115 | 0 | throw new UnknownSessionException(msg); |
116 | |
} |
117 | 199 | return session; |
118 | |
} |
119 | |
|
120 | |
protected abstract Session doGetSession(SessionKey key) throws InvalidSessionException; |
121 | |
|
122 | |
protected Session createExposedSession(Session session, SessionContext context) { |
123 | 28 | return new DelegatingSession(this, new DefaultSessionKey(session.getId())); |
124 | |
} |
125 | |
|
126 | |
protected Session createExposedSession(Session session, SessionKey key) { |
127 | 0 | return new DelegatingSession(this, new DefaultSessionKey(session.getId())); |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
protected Session beforeInvalidNotification(Session session) { |
141 | 18 | return new ImmutableProxiedSession(session); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
protected void notifyStart(Session session) { |
153 | 28 | for (SessionListener listener : this.listeners) { |
154 | 5 | listener.onStart(session); |
155 | 5 | } |
156 | 28 | } |
157 | |
|
158 | |
protected void notifyStop(Session session) { |
159 | 12 | Session forNotification = beforeInvalidNotification(session); |
160 | 12 | for (SessionListener listener : this.listeners) { |
161 | 2 | listener.onStop(forNotification); |
162 | 2 | } |
163 | 12 | } |
164 | |
|
165 | |
protected void notifyExpiration(Session session) { |
166 | 6 | Session forNotification = beforeInvalidNotification(session); |
167 | 6 | for (SessionListener listener : this.listeners) { |
168 | 3 | listener.onExpiration(forNotification); |
169 | 2 | } |
170 | 5 | } |
171 | |
|
172 | |
public Date getStartTimestamp(SessionKey key) { |
173 | 0 | return lookupRequiredSession(key).getStartTimestamp(); |
174 | |
} |
175 | |
|
176 | |
public Date getLastAccessTime(SessionKey key) { |
177 | 0 | return lookupRequiredSession(key).getLastAccessTime(); |
178 | |
} |
179 | |
|
180 | |
public long getTimeout(SessionKey key) throws InvalidSessionException { |
181 | 5 | return lookupRequiredSession(key).getTimeout(); |
182 | |
} |
183 | |
|
184 | |
public void setTimeout(SessionKey key, long maxIdleTimeInMillis) throws InvalidSessionException { |
185 | 5 | Session s = lookupRequiredSession(key); |
186 | 4 | s.setTimeout(maxIdleTimeInMillis); |
187 | 4 | onChange(s); |
188 | 4 | } |
189 | |
|
190 | |
public void touch(SessionKey key) throws InvalidSessionException { |
191 | 1 | Session s = lookupRequiredSession(key); |
192 | 1 | s.touch(); |
193 | 1 | onChange(s); |
194 | 1 | } |
195 | |
|
196 | |
public String getHost(SessionKey key) { |
197 | 0 | return lookupRequiredSession(key).getHost(); |
198 | |
} |
199 | |
|
200 | |
public Collection<Object> getAttributeKeys(SessionKey key) { |
201 | 0 | Collection<Object> c = lookupRequiredSession(key).getAttributeKeys(); |
202 | 0 | if (!CollectionUtils.isEmpty(c)) { |
203 | 0 | return Collections.unmodifiableCollection(c); |
204 | |
} |
205 | 0 | return Collections.emptySet(); |
206 | |
} |
207 | |
|
208 | |
public Object getAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException { |
209 | 107 | return lookupRequiredSession(sessionKey).getAttribute(attributeKey); |
210 | |
} |
211 | |
|
212 | |
public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value) throws InvalidSessionException { |
213 | 44 | if (value == null) { |
214 | 0 | removeAttribute(sessionKey, attributeKey); |
215 | |
} else { |
216 | 44 | Session s = lookupRequiredSession(sessionKey); |
217 | 44 | s.setAttribute(attributeKey, value); |
218 | 44 | onChange(s); |
219 | |
} |
220 | 44 | } |
221 | |
|
222 | |
public Object removeAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException { |
223 | 28 | Session s = lookupRequiredSession(sessionKey); |
224 | 28 | Object removed = s.removeAttribute(attributeKey); |
225 | 28 | if (removed != null) { |
226 | 17 | onChange(s); |
227 | |
} |
228 | 28 | return removed; |
229 | |
} |
230 | |
|
231 | |
public boolean isValid(SessionKey key) { |
232 | |
try { |
233 | 0 | checkValid(key); |
234 | 0 | return true; |
235 | 0 | } catch (InvalidSessionException e) { |
236 | 0 | return false; |
237 | |
} |
238 | |
} |
239 | |
|
240 | |
public void stop(SessionKey key) throws InvalidSessionException { |
241 | 12 | Session session = lookupRequiredSession(key); |
242 | |
try { |
243 | 12 | if (log.isDebugEnabled()) { |
244 | 12 | log.debug("Stopping session with id [" + session.getId() + "]"); |
245 | |
} |
246 | 12 | session.stop(); |
247 | 12 | onStop(session, key); |
248 | 12 | notifyStop(session); |
249 | |
} finally { |
250 | 12 | afterStopped(session); |
251 | 12 | } |
252 | 12 | } |
253 | |
|
254 | |
protected void onStop(Session session, SessionKey key) { |
255 | 12 | onStop(session); |
256 | 12 | } |
257 | |
|
258 | |
protected void onStop(Session session) { |
259 | 0 | onChange(session); |
260 | 0 | } |
261 | |
|
262 | |
protected void afterStopped(Session session) { |
263 | 0 | } |
264 | |
|
265 | |
public void checkValid(SessionKey key) throws InvalidSessionException { |
266 | |
|
267 | 1 | lookupRequiredSession(key); |
268 | 0 | } |
269 | |
|
270 | |
protected void onChange(Session s) { |
271 | 1 | } |
272 | |
} |