View Javadoc
1   /**
2    * This file Copyright (c) 2017-2018 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.ui.framework.ioc;
35  
36  import info.magnolia.module.ModuleRegistry;
37  import info.magnolia.module.model.ComponentsDefinition;
38  import info.magnolia.module.model.ModuleDefinition;
39  import info.magnolia.objectfactory.configuration.ComponentProviderConfiguration;
40  import info.magnolia.objectfactory.configuration.ComponentProviderConfigurationBuilder;
41  import info.magnolia.ui.api.app.AppDescriptor;
42  import info.magnolia.ui.api.app.registry.AppDescriptorRegistry;
43  
44  import java.lang.annotation.Annotation;
45  import java.util.List;
46  import java.util.regex.Pattern;
47  import java.util.stream.Collectors;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.google.inject.Key;
53  
54  /**
55   * Accumulate all the UI-related components in one {@link ComponentProviderConfiguration}.
56   */
57  public final class UiComponentConfigurationAggregator {
58  
59      private static final Logger log = LoggerFactory.getLogger(UiComponentConfigurationAggregator.class);
60  
61      private final static Pattern UI_SCOPE_ID_PATTERN = Pattern.compile("^(choosedialog|mediaeditor|admincentral|app|subapp|datasource)(?:-(.+?))*");
62  
63      private final List<String> appNames;
64      private final List<ModuleDefinition> moduleDefinitions;
65  
66      public UiComponentConfigurationAggregator(ModuleRegistry moduleRegistry, AppDescriptorRegistry appDescriptorRegistry) {
67          this(moduleRegistry.getModuleDefinitions(), appDescriptorRegistry.getAllDefinitions().stream().map(AppDescriptor::getName).collect(Collectors.toList()));
68      }
69  
70      UiComponentConfigurationAggregator(List<ModuleDefinition> moduleDefinitions, List<String> appNames) {
71          this.moduleDefinitions = moduleDefinitions;
72          this.appNames = appNames;
73      }
74  
75      public ComponentProviderConfiguration aggregate() {
76          final ComponentProviderConfigurationBuilder builder = new ComponentProviderConfigurationBuilder() {
77              @Override
78              public void addComponents(ComponentProviderConfiguration configuration, ComponentsDefinition componentsDefinition) {
79                  final String id = componentsDefinition.getId();
80                  final Annotation bindingContext = UiAnnotations.parseAnnotation(id, appNames);
81  
82                  componentsDefinition.getComponents().forEach(componentDefinition -> {
83                      componentDefinition.setBindingContext(bindingContext);
84                      try {
85                          configuration.addComponent(getComponent(componentDefinition));
86                      } catch (Exception e) {
87                          log.error("Failed to resolve UI component mapping of [{}] in context [{}]", componentDefinition.getType(), id, e);
88                      }
89                  });
90  
91                  componentsDefinition.getTypeMappings().forEach(typeMappingDefinition -> {
92                      try {
93                          Class<?> type = classForName(typeMappingDefinition.getType());
94                          Class<?> implementation = classForName(typeMappingDefinition.getImplementation());
95                          final Key typeMappingKey = bindingContext == null ? Key.get(type) : Key.get(type, bindingContext);
96                          configuration.addTypeMapping(typeMappingKey, implementation);
97                      } catch (Exception e) {
98                          log.error("Failed to resolve UI component mapping of [{}] in context [{}]", typeMappingDefinition.getType(), id, e);
99                      }
100             });
101             }
102         };
103 
104         final ComponentProviderConfiguration componentProviderConfiguration = new ComponentProviderConfiguration();
105         moduleDefinitions
106             .stream()
107             .flatMap(moduleDefinition -> moduleDefinition.getComponents().stream())
108             .filter(componentsDefinition -> UI_SCOPE_ID_PATTERN.matcher(componentsDefinition.getId()).matches())
109             .forEach(componentsDefinition -> builder.addComponents(componentProviderConfiguration, componentsDefinition));
110 
111         return componentProviderConfiguration;
112     }
113 }