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.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23
24
25
26
27
28
29
30 public class MapBasedStorageService<KeyType, ValueType> implements StorageService<KeyType, ValueType> {
31
32
33 private Map<String, Map<KeyType, ValueType>> store;
34
35
36 public MapBasedStorageService() {
37 store = new ConcurrentHashMap<String, Map<KeyType, ValueType>>();
38 }
39
40
41
42
43
44
45 protected MapBasedStorageService(Map<String, Map<KeyType, ValueType>> serviceStore) {
46 store = serviceStore;
47 }
48
49
50 public Iterator<String> getPartitions() {
51 Set<String> keys = store.keySet();
52 if (keys != null) {
53 return keys.iterator();
54 }
55
56 return null;
57 }
58
59
60 public Iterator<KeyType> getKeys(String partition) {
61 if (store.containsKey(partition)) {
62 Set<KeyType> keys = store.get(partition).keySet();
63 if (keys != null) {
64 return keys.iterator();
65 }
66 }
67
68 return null;
69 }
70
71
72 public boolean contains(String partition, KeyType key) {
73 if (key == null) {
74 return false;
75 }
76
77 if (store.containsKey(partition)) {
78 return store.get(partition).containsKey(key);
79 }
80
81 return false;
82 }
83
84
85 public ValueType get(String partition, KeyType key) {
86 if (key == null) {
87 return null;
88 }
89
90 if (store.containsKey(partition)) {
91 return store.get(partition).get(key);
92 }
93
94 return null;
95 }
96
97
98 public ValueType put(String partition, KeyType key, ValueType value) {
99 if (key == null) {
100 return null;
101 }
102
103 Map<KeyType, ValueType> partitionMap;
104 synchronized (store) {
105 partitionMap = store.get(partition);
106 if (partitionMap == null) {
107 partitionMap = new ConcurrentHashMap<KeyType, ValueType>();
108 }
109 store.put(partition, partitionMap);
110 }
111
112 return partitionMap.put(key, value);
113 }
114
115
116 public ValueType remove(String partition, KeyType key) {
117 if (key == null) {
118 return null;
119 }
120
121 if (store.containsKey(partition)) {
122 return store.get(partition).remove(key);
123 }
124
125 return null;
126 }
127 }