View Javadoc
1   /**
2    * This file Copyright (c) 2014-2017 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.config.registry.Registry;
37  import info.magnolia.config.source.AbstractConfigurationSourceBuilder;
38  import info.magnolia.init.MagnoliaConfigurationProperties;
39  import info.magnolia.map2bean.Map2BeanTransformer;
40  import info.magnolia.module.ModuleRegistry;
41  import info.magnolia.objectfactory.Components;
42  import info.magnolia.resourceloader.ResourceOrigin;
43  
44  import java.util.regex.Pattern;
45  import java.util.regex.PatternSyntaxException;
46  
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  /**
51   * Builder for {@link info.magnolia.config.source.yaml.YamlConfigurationSource} instances.
52   */
53  public class YamlConfigurationSourceBuilder extends AbstractConfigurationSourceBuilder {
54  
55      private static final Logger log = LoggerFactory.getLogger(YamlConfigurationSourceBuilder.class);
56  
57      /**
58       * Regex format for config files according to Magnolia module conventions.
59       * Mind the %s part of the regex, which should be replaced with the expected definition type.
60       * @see #moduleConventionsFormatFor(Registry)
61       */
62      private static final String MODULE_CONVENTIONS_FORMAT = "^/(?<module>[a-zA-Z0-9-_]+)/(?<deftype>%s)/(?<relPath>.*/)?(?<name>[a-zA-Z0-9-_\\.]+)\\.yaml$";
63  
64      private final ResourceOrigin origin;
65      private final YamlReader yamlReader;
66      private final MagnoliaConfigurationProperties magnoliaConfigurationProperties;
67      private final Map2BeanTransformer map2BeanTransformer;
68      private final ModuleRegistry moduleRegistry;
69      private String pathPattern;
70  
71      /**
72       * @deprecated since 5.5 - use {@link #YamlConfigurationSourceBuilder(ResourceOrigin, Map2BeanTransformer, YamlReader, MagnoliaConfigurationProperties, ModuleRegistry)} instead.
73       */
74      @Deprecated
75      public YamlConfigurationSourceBuilder(ResourceOrigin origin, info.magnolia.config.map2bean.Map2BeanTransformer map2BeanTransformer, YamlReader yamlReader) {
76          this(origin, map2BeanTransformer, yamlReader, Components.getComponent(MagnoliaConfigurationProperties.class), Components.getComponent(ModuleRegistry.class));
77      }
78  
79      /**
80       * @deprecated since 5.5.6 - use {@link #YamlConfigurationSourceBuilder(ResourceOrigin, Map2BeanTransformer, YamlReader, MagnoliaConfigurationProperties, ModuleRegistry)} instead.
81       */
82      @Deprecated
83      public YamlConfigurationSourceBuilder(ResourceOrigin origin, Map2BeanTransformer map2BeanTransformer, YamlReader yamlReader, MagnoliaConfigurationProperties magnoliaConfigurationProperties) {
84          this(origin, map2BeanTransformer, yamlReader, magnoliaConfigurationProperties, Components.getComponent(ModuleRegistry.class));
85      }
86  
87      public YamlConfigurationSourceBuilder(ResourceOrigin origin, Map2BeanTransformer map2BeanTransformer, YamlReader yamlReader, MagnoliaConfigurationProperties magnoliaConfigurationProperties, ModuleRegistry moduleRegistry) {
88          this.origin = origin;
89          this.map2BeanTransformer = map2BeanTransformer;
90          this.yamlReader = yamlReader;
91          this.magnoliaConfigurationProperties = magnoliaConfigurationProperties;
92          this.moduleRegistry = moduleRegistry;
93      }
94  
95      public YamlConfigurationSourceBuilder withPattern(String pathPattern) {
96          this.pathPattern = pathPattern;
97          return this;
98      }
99  
100     @Override
101     public void bindTo(Registry<?> registry) {
102         final Pattern pathPattern = validatePathPattern(this.pathPattern);
103         if (registry == null) {
104             throw new NullPointerException("Must pass a registry instance");
105         }
106 
107         new YamlConfigurationSource<>(origin, map2BeanTransformer, registry, pathPattern, yamlReader, magnoliaConfigurationProperties, moduleRegistry).start();
108     }
109 
110     public void bindWithDefaults(Registry registry) {
111         if (pathPattern != null) {
112             throw new IllegalStateException(this + " is already partially configured, don't use bindWithDefaults().");
113         }
114         withPattern(moduleConventionsFormatFor(registry)).bindTo(registry);
115     }
116 
117     protected Pattern validatePathPattern(String regex) {
118         if (pathPattern == null) {
119             throw new IllegalStateException(this + " is incomplete.");
120         }
121         try {
122             // Ideally, we'd validate the pattern contains 1 group.
123             return Pattern.compile(regex);
124         } catch (PatternSyntaxException e) {
125             log.warn("Pattern {} is invalid: {}", regex, e.getMessage());
126             return Pattern.compile("$^");
127         }
128     }
129 
130     // TODO Move this (and the regex pattern) to a component that exposes this and more conventions-wrapping objects (e.g Predicate<Path>)
131     protected String moduleConventionsFormatFor(Registry registry) {
132         return String.format(MODULE_CONVENTIONS_FORMAT, registry.type().getPluralName());
133     }
134 
135 }