View Javadoc
1   /**
2    * This file Copyright (c) 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 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          throw new IllegalStateException("not implemented yet");
84      }
85  
86      @Override
87      public DefinitionQuery query() {
88          return new AggregatingQuery();
89      }
90  
91      private class AggregatingQuery extends DefinitionQuery {
92          @Override
93          public Collection<DefinitionProvider<?>> findMultiple() {
94              final Collection<Registry> allRegistries = all();
95              // collect results from each registry
96              final Collection<Collection<? extends DefinitionProvider<?>>> allRes = Collections2.transform(allRegistries,
97                      new RegistryCollectionFunction(this));
98              // concat them in a single list
99              return Lists.newLinkedList(Iterables.concat(allRes));
100         }
101 
102 
103         private class RegistryCollectionFunction implements Function<Registry, Collection<? extends DefinitionProvider<?>>> {
104             private final AggregatingQuery query;
105 
106             public RegistryCollectionFunction(AggregatingQuery query) {
107                 this.query = query;
108             }
109 
110             @Override
111             public Collection<? extends DefinitionProvider<?>> apply(final Registry registry) {
112                 return ((DefinitionQuery<?>) copyFor(query, registry)).findMultiple();
113             }
114         }
115 
116     }
117 
118     // not public, only needed for RegFacadeImpl. Move or make public ? TODO how we do ensure we copied it right ?
119     // --> unit test ? (if we add props to queries... need to make sure we can still copy it)
120     private static DefinitionQuery<?> copyFor(AggregatingQuery src, Registry<?> registry) {
121         return registry.query()
122                 .from(src.getModule())
123                 .at(src.getLocationPattern())
124                 .ofType(src.getType())
125                 .named(src.getName());
126     }
127 
128 
129     private static class RegistryByType implements Predicate<Registry> {
130         private final DefinitionType type;
131 
132         public RegistryByType(DefinitionType type) {
133             this.type = type;
134         }
135 
136         @Override
137         public boolean apply(Registry registry) {
138             return registry.type().equals(type);
139         }
140     }
141 
142 }