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      /** Interval between sweeps. */
36      private long sweepInterval;
37  
38      /** Storage service whose entries will be periodically checked. */
39      private StorageService store;
40  
41      /** Storage partitions to sweep. */
42      private Set<String> partitions;
43  
44      /**
45       * Constructor. Registers this task with the given timer.
46       * 
47       * @param taskTimer timer that will sweep the given storage service
48       * @param interval interval, in milliseconds, that the storage service will be swept
49       * @param sweptStore storage service that will be swept
50       */
51      public ExpiringObjectStorageServiceSweeper(Timer taskTimer, long interval, StorageService sweptStore) {
52          store = sweptStore;
53          sweepInterval = interval;
54          taskTimer.schedule(this, interval, interval);
55          partitions = null;
56      }
57  
58      /**
59       * Constructor. Registers this task with the given timer.
60       * 
61       * @param taskTimer timer that will sweep the given storage service
62       * @param interval interval, in milliseconds, that the storage service will be swept
63       * @param sweptStore storage service that will be swept
64       * @param sweptPartitions the partitions to sweep, if null or empty all partitions are swept
65       */
66      public ExpiringObjectStorageServiceSweeper(Timer taskTimer, long interval, StorageService sweptStore,
67              Set<String> sweptPartitions) {
68          store = sweptStore;
69          if (sweptPartitions != null || sweptPartitions.isEmpty()) {
70              partitions = sweptPartitions;
71          } else {
72              partitions = null;
73          }
74          sweepInterval = interval;
75          taskTimer.schedule(this, interval, interval);
76      }
77  
78      /** {@inheritDoc} */
79      public void run() {
80          try {
81              Iterator<String> sweepPartitions;
82              if (partitions != null && !partitions.isEmpty()) {
83                  sweepPartitions = partitions.iterator();
84              } else {
85                  sweepPartitions = store.getPartitions();
86              }
87  
88              String currentParition;
89              Iterator<?> partitionKeys;
90              Object partitionKey;
91              Object partitionValue;
92              while (sweepPartitions.hasNext()) {
93                  currentParition = sweepPartitions.next();
94                  log.trace("Sweeping storage service partition {}", currentParition);
95                  partitionKeys = store.getKeys(currentParition);
96                  if (partitionKeys == null) {
97                      continue;
98                  }
99  
100                 while (partitionKeys.hasNext()) {
101                     partitionKey = partitionKeys.next();
102                     partitionValue = store.get(currentParition, partitionKey);
103                     if (partitionValue instanceof ExpiringObject) {
104                         if (((ExpiringObject) partitionValue).isExpired()) {
105                             log.trace("Removing expired object from storage service partition {}", currentParition);
106                             partitionKeys.remove();
107                         }
108                     }
109                 }
110             }
111         } catch (Throwable t) {
112             log.error("Caught unexpected error, sweeper will execute again in " + sweepInterval + "ms", t);
113         }
114     }
115 }