View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
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    * http://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.opensaml.util.storage;
18  
19  import java.util.Iterator;
20  import java.util.Set;
21  import java.util.Timer;
22  import java.util.TimerTask;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * A simple task that periodically sweeps over a {@link StorageService} and removes expired entries.
29   */
30  public class ExpiringObjectStorageServiceSweeper extends TimerTask {
31      
32      /** Class logger. */
33      private final Logger log = LoggerFactory.getLogger(ExpiringObjectStorageServiceSweeper.class);
34  
35      /** Storage service whose entries will be periodically checked. */
36      private StorageService store;
37  
38      /** Storage partitions to sweep. */
39      private Set<String> partitions;
40  
41      /**
42       * Constructor. Registers this task with the given timer.
43       * 
44       * @param taskTimer timer that will sweep the given storage service
45       * @param sweepInterval interval, in milliseconds, that the storage service will be swept
46       * @param sweptStore storage service that will be swept
47       */
48      public ExpiringObjectStorageServiceSweeper(Timer taskTimer, long sweepInterval, StorageService sweptStore) {
49          store = sweptStore;
50          taskTimer.schedule(this, sweepInterval, sweepInterval);
51          partitions = null;
52      }
53  
54      /**
55       * Constructor. Registers this task with the given timer.
56       * 
57       * @param taskTimer timer that will sweep the given storage service
58       * @param sweepInterval interval, in milliseconds, that the storage service will be swept
59       * @param sweptStore storage service that will be swept
60       * @param sweptPartitions the partitions to sweep, if null or empty all partitions are swept
61       */
62      public ExpiringObjectStorageServiceSweeper(Timer taskTimer, long sweepInterval, StorageService sweptStore,
63              Set<String> sweptPartitions) {
64          store = sweptStore;
65          if (sweptPartitions != null || sweptPartitions.isEmpty()) {
66              partitions = sweptPartitions;
67          }else{
68              partitions = null;
69          }
70          taskTimer.schedule(this, sweepInterval, sweepInterval);
71      }
72  
73      /** {@inheritDoc} */
74      public void run() {
75          Iterator<String> sweepPartitions;
76          if (partitions != null && !partitions.isEmpty()) {
77              sweepPartitions = partitions.iterator();
78          } else {
79              sweepPartitions = store.getPartitions();
80          }
81  
82          String currentParition;
83          Iterator<?> partitionKeys;
84          Object partitionKey;
85          Object partitionValue;
86          while (sweepPartitions.hasNext()) {
87              currentParition = sweepPartitions.next();
88              log.trace("Sweeping storage service partition {}", currentParition);
89              partitionKeys = store.getKeys(currentParition);
90              if (partitionKeys == null) {
91                  continue;
92              }
93  
94              while (partitionKeys.hasNext()) {
95                  partitionKey = partitionKeys.next();
96                  partitionValue = store.get(currentParition, partitionKey);
97                  if (partitionValue instanceof ExpiringObject) {
98                      if (((ExpiringObject) partitionValue).isExpired()) {
99                          log.trace("Removing expired object from storage service partition {}", currentParition);
100                         partitionKeys.remove();
101                     }
102                 }
103             }
104         }
105     }
106 }