1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 import org.yaml.snakeyaml.constructor.Construct;
50
51
52
53
54 public class YamlConfigurationSourceBuilder extends AbstractConfigurationSourceBuilder {
55
56 private static final Logger log = LoggerFactory.getLogger(YamlConfigurationSourceBuilder.class);
57
58
59
60
61
62
63 private static final String MODULE_CONVENTIONS_FORMAT = "^/(?<module>[a-zA-Z0-9-_]+)/(?<deftype>%s)/(?<relPath>.*/)?(?<name>[a-zA-Z0-9-_\\.]+)\\.yaml$";
64
65 private final ResourceOrigin origin;
66 private final YamlReader yamlReader;
67 private final MagnoliaConfigurationProperties magnoliaConfigurationProperties;
68 private final Map2BeanTransformer map2BeanTransformer;
69 private final ModuleRegistry moduleRegistry;
70 private String pathPattern;
71
72
73
74
75 @Deprecated
76 public YamlConfigurationSourceBuilder(ResourceOrigin origin, info.magnolia.config.map2bean.Map2BeanTransformer map2BeanTransformer, YamlReader yamlReader) {
77 this(origin, map2BeanTransformer, yamlReader, Components.getComponent(MagnoliaConfigurationProperties.class), Components.getComponent(ModuleRegistry.class));
78 }
79
80
81
82
83 @Deprecated
84 public YamlConfigurationSourceBuilder(ResourceOrigin origin, Map2BeanTransformer map2BeanTransformer, YamlReader yamlReader, MagnoliaConfigurationProperties magnoliaConfigurationProperties) {
85 this(origin, map2BeanTransformer, yamlReader, magnoliaConfigurationProperties, Components.getComponent(ModuleRegistry.class));
86 }
87
88 public YamlConfigurationSourceBuilder(ResourceOrigin origin, Map2BeanTransformer map2BeanTransformer, YamlReader yamlReader, MagnoliaConfigurationProperties magnoliaConfigurationProperties, ModuleRegistry moduleRegistry) {
89 this.origin = origin;
90 this.map2BeanTransformer = map2BeanTransformer;
91 this.yamlReader = yamlReader;
92 this.magnoliaConfigurationProperties = magnoliaConfigurationProperties;
93 this.moduleRegistry = moduleRegistry;
94 }
95
96 public YamlConfigurationSourceBuilder withPattern(String pathPattern) {
97 this.pathPattern = pathPattern;
98 return this;
99 }
100
101 public YamlConfigurationSourceBuilder withCustomMultiConstruct(String tagPrefix, Construct construct) {
102 this.yamlReader.registerCustomMultiConstruct(tagPrefix, construct);
103 return this;
104 }
105
106 public YamlConfigurationSourceBuilder withCustomConstruct(String tag, Construct construct) {
107 this.yamlReader.registerCustomConstruct(tag, construct);
108 return this;
109 }
110
111 @Override
112 public void bindTo(Registry<?> registry) {
113 final Pattern pathPattern = validatePathPattern(this.pathPattern);
114 if (registry == null) {
115 throw new NullPointerException("Must pass a registry instance");
116 }
117
118 new YamlConfigurationSource<>(origin, map2BeanTransformer, registry, pathPattern, yamlReader, magnoliaConfigurationProperties, moduleRegistry).start();
119 }
120
121 public void bindWithDefaults(Registry registry) {
122 if (pathPattern != null) {
123 throw new IllegalStateException(this + " is already partially configured, don't use bindWithDefaults().");
124 }
125 withPattern(moduleConventionsFormatFor(registry)).bindTo(registry);
126 }
127
128 protected Pattern validatePathPattern(String regex) {
129 if (pathPattern == null) {
130 throw new IllegalStateException(this + " is incomplete.");
131 }
132 try {
133
134 return Pattern.compile(regex);
135 } catch (PatternSyntaxException e) {
136 log.warn("Pattern {} is invalid: {}", regex, e.getMessage());
137 return Pattern.compile("$^");
138 }
139 }
140
141
142 protected String moduleConventionsFormatFor(Registry registry) {
143 return String.format(MODULE_CONVENTIONS_FORMAT, registry.type().getPluralName());
144 }
145
146 }