View Javadoc
1   /**
2    * This file Copyright (c) 2015-2016 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 info.magnolia.config.source.ConfigurationSourceType;
37  
38  import java.util.Collection;
39  import java.util.Set;
40  
41  import javax.inject.Inject;
42  
43  import com.google.common.base.Function;
44  import com.google.common.base.Predicate;
45  import com.google.common.collect.Collections2;
46  import com.google.common.collect.Iterables;
47  import com.google.common.collect.Lists;
48  
49  /**
50   * Implementation of the {@link info.magnolia.config.registry.RegistryFacade}.
51   */
52  public class RegistryFacadeImpl implements RegistryFacade {
53      private final Collection<Registry> registries;
54  
55      @Inject
56      public RegistryFacadeImpl(Set<Registry> registries) {
57          this.registries = registries;
58          //TODO validate each registry is "unique" (registry.type())
59      }
60  
61      @Override
62      public Registry registryFor(final DefinitionType type) {
63          return Iterables.find(registries, new RegistryByType(type));
64      }
65  
66      @Override
67      public Collection<Registry> all() {
68          return registries;
69      }
70  
71      @Override
72      public Collection<DefinitionProvider> byModule(String moduleName) {
73          return query().from(moduleName).findMultiple();
74      }
75  
76      @Override
77      public Collection<DefinitionProvider> byType(DefinitionType type) {
78          return query().ofType(type).findMultiple();
79      }
80  
81      @Override
82      public Collection<DefinitionProvider> bySource(ConfigurationSourceType sourceType) {
83          return query().ofConfigurationSourceType(sourceType).findMultiple();
84      }
85  
86      @Override
87      public DefinitionQuery query() {
88          return new AggregatingQuery();
89      }
90  
91      @Override
92      public DefinitionQuery query(DefinitionQuery base) {
93          return new AggregatingQuery(base);
94      }
95  
96      private class AggregatingQuery extends DefinitionQuery {
97  
98          AggregatingQuery() {
99              super();
100         }
101 
102         AggregatingQuery(DefinitionQuery base) {
103             super(base);
104         }
105 
106         @Override
107         public Collection<DefinitionProvider<?>> findMultiple() {
108             final Collection<Registry> allRegistries = all();
109             // collect results from each registry
110             final Collection<Collection<? extends DefinitionProvider<?>>> allRes = Collections2.transform(allRegistries,
111                     new RegistryCollectionFunction(this));
112             // concat them in a single list
113             return Lists.newLinkedList(Iterables.concat(allRes));
114         }
115 
116 
117         private class RegistryCollectionFunction implements Function<Registry, Collection<? extends DefinitionProvider<?>>> {
118             private final AggregatingQuery query;
119 
120             public RegistryCollectionFunction(AggregatingQuery query) {
121                 this.query = query;
122             }
123 
124             @Override
125             public Collection<? extends DefinitionProvider<?>> apply(final Registry registry) {
126                 return DefinitionQuery.build(registry, query).findMultiple();
127             }
128         }
129 
130     }
131 
132     private static class RegistryByType implements Predicate<Registry> {
133         private final DefinitionType type;
134 
135         public RegistryByType(DefinitionType type) {
136             this.type = type;
137         }
138 
139         @Override
140         public boolean apply(Registry registry) {
141             return registry.type().equals(type);
142         }
143     }
144 
145 }