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.Map;
21  import java.util.Set;
22  import java.util.concurrent.ConcurrentHashMap;
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 ConcurrentHashMap<String, Map<KeyType, ValueType>>();
38      }
39  
40      /**
41       * Constructor.
42       * 
43       * @param serviceStore the map to use as storage
44       */
45      protected MapBasedStorageService(Map<String, Map<KeyType, ValueType>> serviceStore) {
46          store = serviceStore;
47      }
48  
49      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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     /** {@inheritDoc} */
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 }