1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth.provider.token;
18
19 import org.springframework.beans.factory.DisposableBean;
20
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.TimeUnit;
26
27
28
29
30
31
32 public class InMemorySelfCleaningProviderTokenServices extends InMemoryProviderTokenServices implements DisposableBean {
33
34 private ScheduledExecutorService scheduler;
35 private Integer cleanupIntervalSeconds;
36
37 @Override
38 public void afterPropertiesSet() throws Exception {
39 super.afterPropertiesSet();
40
41 if (cleanupIntervalSeconds == null) {
42 cleanupIntervalSeconds = 60 * 60;
43 }
44
45 if (cleanupIntervalSeconds > 0) {
46 scheduler = Executors.newSingleThreadScheduledExecutor();
47 Runnable cleanupLogic = new Runnable() {
48 public void run() {
49 Iterator<Map.Entry<String, OAuthProviderTokenImpl>> entriesIt = tokenStore.entrySet().iterator();
50 while (entriesIt.hasNext()) {
51 Map.Entry<String, OAuthProviderTokenImpl> entry = entriesIt.next();
52 OAuthProviderTokenImpl tokenImpl = entry.getValue();
53 if (isExpired(tokenImpl)) {
54
55 entriesIt.remove();
56 onTokenRemoved(tokenImpl);
57 }
58 }
59 }
60 };
61 scheduler.scheduleAtFixedRate(cleanupLogic, getAccessTokenValiditySeconds(), cleanupIntervalSeconds, TimeUnit.SECONDS);
62 }
63 }
64
65 public void destroy() throws Exception {
66 if (scheduler != null) {
67 scheduler.shutdownNow();
68 }
69 }
70
71
72
73
74
75
76 public Integer getCleanupIntervalSeconds() {
77 return cleanupIntervalSeconds;
78 }
79
80
81
82
83
84
85 public void setCleanupIntervalSeconds(Integer cleanupIntervalSeconds) {
86 this.cleanupIntervalSeconds = cleanupIntervalSeconds;
87 }
88 }