View Javadoc
1   /*
2    * Copyright 2008 Web Cohesion
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Implementation of TokenServices that stores tokens in memory. The token services will schedule a thread to do cleaning up of expired tokens.
29   *
30   * @author Ryan Heaton
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                //there's a race condition here, but we'll live with it for now.
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     * The interval at which to schedule cleanup. (&lt;= 0 for never).
73     *
74     * @return The interval at which to schedule cleanup.
75     */
76    public Integer getCleanupIntervalSeconds() {
77      return cleanupIntervalSeconds;
78    }
79  
80    /**
81     * The interval at which to schedule cleanup.
82     *
83     * @param cleanupIntervalSeconds The interval at which to schedule cleanup.
84     */
85    public void setCleanupIntervalSeconds(Integer cleanupIntervalSeconds) {
86      this.cleanupIntervalSeconds = cleanupIntervalSeconds;
87    }
88  }