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.quartz; |
20 | |
|
21 | |
import org.quartz.JobDetail; |
22 | |
import org.quartz.Scheduler; |
23 | |
import org.quartz.SchedulerException; |
24 | |
import org.quartz.SimpleTrigger; |
25 | |
import org.quartz.impl.StdSchedulerFactory; |
26 | |
|
27 | |
import org.slf4j.Logger; |
28 | |
import org.slf4j.LoggerFactory; |
29 | |
|
30 | |
import org.apache.shiro.session.mgt.DefaultSessionManager; |
31 | |
import org.apache.shiro.session.mgt.SessionValidationScheduler; |
32 | |
import org.apache.shiro.session.mgt.ValidatingSessionManager; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class QuartzSessionValidationScheduler implements SessionValidationScheduler { |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static final long DEFAULT_SESSION_VALIDATION_INTERVAL = DefaultSessionManager.DEFAULT_SESSION_VALIDATION_INTERVAL; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private static final String JOB_NAME = "SessionValidationJob"; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | private static final Logger log = LoggerFactory.getLogger(QuartzSessionValidationScheduler.class); |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private Scheduler scheduler; |
70 | |
|
71 | 0 | private boolean schedulerImplicitlyCreated = false; |
72 | |
|
73 | 0 | private boolean enabled = false; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private ValidatingSessionManager sessionManager; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | 0 | private long sessionValidationInterval = DEFAULT_SESSION_VALIDATION_INTERVAL; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 0 | public QuartzSessionValidationScheduler() { |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | 0 | public QuartzSessionValidationScheduler(ValidatingSessionManager sessionManager) { |
101 | 0 | this.sessionManager = sessionManager; |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
protected Scheduler getScheduler() throws SchedulerException { |
109 | 0 | if (scheduler == null) { |
110 | 0 | scheduler = StdSchedulerFactory.getDefaultScheduler(); |
111 | 0 | schedulerImplicitlyCreated = true; |
112 | |
} |
113 | 0 | return scheduler; |
114 | |
} |
115 | |
|
116 | |
public void setScheduler(Scheduler scheduler) { |
117 | 0 | this.scheduler = scheduler; |
118 | 0 | } |
119 | |
|
120 | |
public void setSessionManager(ValidatingSessionManager sessionManager) { |
121 | 0 | this.sessionManager = sessionManager; |
122 | 0 | } |
123 | |
|
124 | |
public boolean isEnabled() { |
125 | 0 | return this.enabled; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void setSessionValidationInterval(long sessionValidationInterval) { |
137 | 0 | this.sessionValidationInterval = sessionValidationInterval; |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public void enableSessionValidation() { |
149 | |
|
150 | 0 | if (log.isDebugEnabled()) { |
151 | 0 | log.debug("Scheduling session validation job using Quartz with " + |
152 | |
"session validation interval of [" + sessionValidationInterval + "]ms..."); |
153 | |
} |
154 | |
|
155 | |
try { |
156 | 0 | SimpleTrigger trigger = new SimpleTrigger(getClass().getName(), |
157 | |
Scheduler.DEFAULT_GROUP, |
158 | |
SimpleTrigger.REPEAT_INDEFINITELY, |
159 | |
sessionValidationInterval); |
160 | |
|
161 | 0 | JobDetail detail = new JobDetail(JOB_NAME, Scheduler.DEFAULT_GROUP, QuartzSessionValidationJob.class); |
162 | 0 | detail.getJobDataMap().put(QuartzSessionValidationJob.SESSION_MANAGER_KEY, sessionManager); |
163 | |
|
164 | 0 | Scheduler scheduler = getScheduler(); |
165 | |
|
166 | 0 | scheduler.scheduleJob(detail, trigger); |
167 | 0 | if (schedulerImplicitlyCreated) { |
168 | 0 | scheduler.start(); |
169 | 0 | if (log.isDebugEnabled()) { |
170 | 0 | log.debug("Successfully started implicitly created Quartz Scheduler instance."); |
171 | |
} |
172 | |
} |
173 | 0 | this.enabled = true; |
174 | |
|
175 | 0 | if (log.isDebugEnabled()) { |
176 | 0 | log.debug("Session validation job successfully scheduled with Quartz."); |
177 | |
} |
178 | |
|
179 | 0 | } catch (SchedulerException e) { |
180 | 0 | if (log.isErrorEnabled()) { |
181 | 0 | log.error("Error starting the Quartz session validation job. Session validation may not occur.", e); |
182 | |
} |
183 | 0 | } |
184 | 0 | } |
185 | |
|
186 | |
public void disableSessionValidation() { |
187 | 0 | if (log.isDebugEnabled()) { |
188 | 0 | log.debug("Stopping Quartz session validation job..."); |
189 | |
} |
190 | |
|
191 | |
Scheduler scheduler; |
192 | |
try { |
193 | 0 | scheduler = getScheduler(); |
194 | 0 | if (scheduler == null) { |
195 | 0 | if (log.isWarnEnabled()) { |
196 | 0 | log.warn("getScheduler() method returned a null Quartz scheduler, which is unexpected. Please " + |
197 | |
"check your configuration and/or implementation. Returning quietly since there is no " + |
198 | |
"validation job to remove (scheduler does not exist)."); |
199 | |
} |
200 | 0 | return; |
201 | |
} |
202 | 0 | } catch (SchedulerException e) { |
203 | 0 | if (log.isWarnEnabled()) { |
204 | 0 | log.warn("Unable to acquire Quartz Scheduler. Ignoring and returning (already stopped?)", e); |
205 | |
} |
206 | 0 | return; |
207 | 0 | } |
208 | |
|
209 | |
try { |
210 | 0 | scheduler.unscheduleJob(JOB_NAME, Scheduler.DEFAULT_GROUP); |
211 | 0 | if (log.isDebugEnabled()) { |
212 | 0 | log.debug("Quartz session validation job stopped successfully."); |
213 | |
} |
214 | 0 | } catch (SchedulerException e) { |
215 | 0 | if (log.isDebugEnabled()) { |
216 | 0 | log.debug("Could not cleanly remove SessionValidationJob from Quartz scheduler. " + |
217 | |
"Ignoring and stopping.", e); |
218 | |
} |
219 | 0 | } |
220 | |
|
221 | 0 | this.enabled = false; |
222 | |
|
223 | 0 | if (schedulerImplicitlyCreated) { |
224 | |
try { |
225 | 0 | scheduler.shutdown(); |
226 | 0 | } catch (SchedulerException e) { |
227 | 0 | if (log.isWarnEnabled()) { |
228 | 0 | log.warn("Unable to cleanly shutdown implicitly created Quartz Scheduler instance.", e); |
229 | |
} |
230 | |
} finally { |
231 | 0 | setScheduler(null); |
232 | 0 | schedulerImplicitlyCreated = false; |
233 | 0 | } |
234 | |
} |
235 | |
|
236 | |
|
237 | 0 | } |
238 | |
} |