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.source.yaml;
35  
36  import info.magnolia.cms.util.ExceptionUtil;
37  import info.magnolia.config.NamedDefinition;
38  import info.magnolia.config.map2bean.Map2BeanTransformer;
39  import info.magnolia.config.registry.DefinitionMetadataBuilder;
40  import info.magnolia.config.registry.DefinitionProvider;
41  import info.magnolia.config.registry.DefinitionProviderBuilder;
42  import info.magnolia.config.registry.DefinitionRawView;
43  import info.magnolia.config.registry.Registry;
44  import info.magnolia.config.source.raw.DefinitionRawViewMapWrapper;
45  import info.magnolia.resourceloader.Resource;
46  import info.magnolia.resourceloader.ResourceOrigin;
47  
48  import java.io.IOException;
49  import java.util.Map;
50  import java.util.regex.Pattern;
51  
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  /**
56   * Configuration source for yaml configuration files.
57   *
58   * @param <T> type the source will provide
59   */
60  public class YamlConfigurationSource<T> extends AbstractFileResourceConfigurationSource<T> {
61  
62      private static Logger log = LoggerFactory.getLogger(YamlConfigurationSource.class);
63  
64      private final Map2BeanTransformer map2BeanTransformer;
65  
66      private final YamlReader yamlReader;
67  
68      /**
69       * @param pathPattern a regular expression pattern used to determine whether a file should be considered by the source or not. If the pattern contains a group, it will be used to determine the name of the given object, if not explicitly configured.
70       */
71      public YamlConfigurationSource(ResourceOrigin origin, Map2BeanTransformer map2BeanTransformer, Registry<T> registry, Pattern pathPattern, YamlReader yamlReader) throws IOException {
72          super(origin, registry, pathPattern);
73          this.map2BeanTransformer = map2BeanTransformer;
74          this.yamlReader = yamlReader;
75      }
76  
77      @Override
78      public void loadAndRegister(Resource resource) {
79          if (resource == null) {
80              throw new IllegalStateException("Resource path cannot be null");
81          }
82  
83          final DefinitionProviderBuilder<T> builder = DefinitionProviderBuilder.newBuilder();
84          final DefinitionMetadataBuilder metadataBuilder = createMetadata(resource);
85          builder.metadata(metadataBuilder);
86  
87          try {
88              // 1. Get map representation from yaml file
89              final Map<String, Object> map = yamlReader.readToMap(resource);
90              final DefinitionRawView raw = new DefinitionRawViewMapWrapper(map);
91              builder.rawView(raw);
92  
93              // 2. Then transform it into the actual bean
94              final T bean = map2BeanTransformer.toBean(map, getRootType());
95              builder.definition(bean);
96  
97              // Override metadata name if needed
98              if (bean instanceof NamedDefinition) {
99                  final String definitionName = ((NamedDefinition) bean).getName();
100                 if (definitionName != null) {
101                     // Definition's name has priority
102                     metadataBuilder.name(definitionName);
103                 }
104             }
105         } catch (Throwable t) {
106             // Here we're catching any exception, from reader (io) all the way down to map2BeanTransformer
107             builder.addErrorMessage(ExceptionUtil.exceptionToWords(t));
108             log.warn("Problem while registering {} from {}: {}", getRegistry().type(), resource, ExceptionUtil.exceptionToWords(t), t);
109         }
110 
111         final DefinitionProvider<T> provider = builder.build();
112         getRegistry().register(provider);
113         log.info("Registered {} from {}", provider.getMetadata(), resource);
114     }
115 
116 }