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