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  /**
25   * A simple task that periodically sweeps over a {@link StorageService} and removes expired entries.
26   */
27  public class ExpiringObjectStorageServiceSweeper extends TimerTask {
28  
29      /** Storage service whose entries will be periodically checked. */
30      private StorageService store;
31  
32      /** Storage partitions to sweep. */
33      private Set<String> partitions;
34  
35      /**
36       * Constructor. Registers this task with the given timer.
37       * 
38       * @param taskTimer timer that will sweep the given storage service
39       * @param sweepInterval interval, in milliseconds, that the storage service will be swept
40       * @param sweptStore storage service that will be swept
41       */
42      public ExpiringObjectStorageServiceSweeper(Timer taskTimer, long sweepInterval, StorageService sweptStore) {
43          store = sweptStore;
44          taskTimer.schedule(this, sweepInterval);
45      }
46  
47      /**
48       * Constructor. Registers this task with the given timer.
49       * 
50       * @param taskTimer timer that will sweep the given storage service
51       * @param sweepInterval interval, in milliseconds, that the storage service will be swept
52       * @param sweptStore storage service that will be swept
53       * @param sweptParitions the partitions to sweep, if null or empty all partitions are swept
54       */
55      public ExpiringObjectStorageServiceSweeper(Timer taskTimer, long sweepInterval, StorageService sweptStore,
56              Set<String> sweptParitions) {
57          store = sweptStore;
58          if (sweptParitions != null || sweptParitions.isEmpty()) {
59              partitions = sweptParitions;
60          }
61          taskTimer.schedule(this, sweepInterval);
62      }
63  
64      /** {@inheritDoc} */
65      public void run() {
66          Iterator<String> sweepPartitions;
67          if (partitions != null) {
68              sweepPartitions = partitions.iterator();
69          } else {
70              sweepPartitions = store.getPartitions();
71          }
72  
73          String currentParition;
74          Iterator<?> partitionKeys;
75          Object partitionKey;
76          Object partitionValue;
77          while (sweepPartitions.hasNext()) {
78              currentParition = sweepPartitions.next();
79              partitionKeys = store.getKeys(currentParition);
80              if (partitionKeys == null) {
81                  continue;
82              }
83  
84              while (partitionKeys.hasNext()) {
85                  partitionKey = partitionKeys.next();
86                  partitionValue = store.get(currentParition, partitionKey);
87                  if (partitionValue instanceof ExpiringObject) {
88                      if (((ExpiringObject) partitionValue).isExpired()) {
89                          partitionKeys.remove();
90                      }
91                  }
92              }
93          }
94      }
95  }