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 long sweepInterval;
37
38
39 private StorageService store;
40
41
42 private Set<String> partitions;
43
44
45
46
47
48
49
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
60
61
62
63
64
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
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 }