View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.config.registry;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.HashSet;
40  import java.util.Map;
41  import java.util.Set;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  /**
46   * Thread safe map intended to be used for registries. Provides an atomic operation <code>removeAndPutAll</code> that is
47   * used to remove a set of previously added values before adding a collection of new ones. Read operations are blocked
48   * until it completes guaranteeing proper visibility.
49   * <p/>
50   * It is common for entities in registries to also hold their identifier. The method <code>keyFormValue</code> can be
51   * overridden to get the identifier from the value. This removes the need to package a set of entities that should be
52   * added in a Map before calling {@link #removeAndPutAll(java.util.Collection, java.util.Collection)}.
53   *
54   * This is a port and simplified version of {@link info.magnolia.registry.RegistryMap}, which was written prior to the
55   * introduction of the {@link Registry} interface. It should not be used directly.
56   *
57   * @param <T> the type of the contained objects
58   */
59  public class RegistryMap<T> {
60  
61      private final Map<DefinitionMetadata, DefinitionProvider<T>> map = new HashMap<>();
62  
63      private final Map<String, DefinitionMetadata> stringKeysMap = new HashMap<>();
64  
65      public synchronized DefinitionProvider<T> get(DefinitionMetadata key) {
66          return map.get(key);
67      }
68  
69      public synchronized DefinitionProvider<T> getByStringKey(String stringKey) {
70          final DefinitionMetadata k = stringKeysMap.get(stringKey);
71          return get(k);
72      }
73  
74      public synchronized DefinitionProvider<T> getRequired(DefinitionMetadata key) throws RegistrationException {
75          DefinitionProvider<T> value = map.get(key);
76          if (value == null) {
77              throw new RegistrationException("Entry for [" + key + "] not found in registry, available entries are: " +
78                      (map.isEmpty() ? "<none>" : StringUtils.join(map.keySet(), ", ")));
79          }
80          return value;
81      }
82  
83      public synchronized DefinitionProvider<T> getRequiredByStringKey(String stringKey) throws RegistrationException {
84          final DefinitionMetadata k = stringKeysMap.get(stringKey);
85          return getRequired(k);
86      }
87  
88  
89      public synchronized DefinitionMetadata put(DefinitionProvider<T> value) {
90          final DefinitionMetadata key = keyFromValue(value);
91          map.put(key, value);
92          stringKeysMap.put(asStringKey(value), key);
93          return key;
94      }
95  
96      public synchronized void remove(DefinitionMetadata key) {
97          final DefinitionProvider<T> provider = map.remove(key);
98          stringKeysMap.remove(asStringKey(provider));
99      }
100 
101     public synchronized Set<DefinitionMetadata> removeAndPutAll(Collection<DefinitionMetadata> toRemove, Collection<DefinitionProvider<T>> toPut) {
102         // Fail early (and don't remove existing defs) if keyFromValue can't work for any of toPut items
103         if (!toPut.isEmpty()) {
104             keyFromValue(toPut.iterator().next());
105         }
106         for (DefinitionMetadata key : toRemove) {
107             if (!map.containsKey(key)) {
108                 throw new IllegalArgumentException(String.format("%s can't be removed from map, it is not an existing key", key));
109             }
110 
111             final DefinitionProvider<T> provider = map.remove(key);
112             stringKeysMap.remove(asStringKey(provider));
113         }
114         final Set<DefinitionMetadata> keys = new HashSet<>();
115         for (DefinitionProvider<T> value : toPut) {
116             final DefinitionMetadata key = put(value);
117             keys.add(key);
118         }
119         return keys;
120     }
121 
122     public synchronized Collection<DefinitionMetadata> keySet() {
123         return new ArrayList<>(map.keySet());
124     }
125 
126     public synchronized Collection<DefinitionProvider<T>> values() {
127         return new ArrayList<>(map.values());
128     }
129 
130     protected DefinitionMetadata keyFromValue(DefinitionProvider<T> provider) {
131         return provider.getMetadata();
132     }
133 
134     protected String asStringKey(DefinitionProvider<T> provider) {
135         return provider.getMetadata().getReferenceId();
136     }
137 }