1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30 public class ExpiringObjectStorageServiceSweeper extends TimerTask {
31
32
33 private final Logger log = LoggerFactory.getLogger(ExpiringObjectStorageServiceSweeper.class);
34
35
36 private StorageService store;
37
38
39 private Set<String> partitions;
40
41
42
43
44
45
46
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
56
57
58
59
60
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
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 }