1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.support;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import javax.annotation.CheckForNull;
24 import javax.annotation.concurrent.NotThreadSafe;
25
26 import de.smartics.properties.api.config.domain.ConfigurationException;
27 import de.smartics.properties.api.config.domain.PropertyProvider;
28 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29 import de.smartics.properties.api.core.domain.ConfigException;
30 import de.smartics.properties.spi.config.definition.DefinitionConfigParser;
31 import de.smartics.properties.spi.config.definition.DefinitionKeyHelper;
32 import de.smartics.properties.spi.config.definition.PropertiesDefinitionContext;
33 import de.smartics.util.lang.ClassPathContext;
34
35
36
37
38 @NotThreadSafe
39 public final class MultiSourcePropertiesManager
40 {
41
42
43
44
45
46
47
48
49
50
51 private final Collection<PropertyProvider> rootPropertyProviders;
52
53
54
55
56
57 private final boolean lenient;
58
59
60
61
62 private final Map<ConfigurationKey, MultiSourceProperties> dictionary =
63 new HashMap<ConfigurationKey, MultiSourceProperties>();
64
65
66
67
68
69
70
71
72
73
74
75
76
77 private final Map<ClassPathContext, DefinitionKeyHelper> definitionCache =
78 new HashMap<ClassPathContext, DefinitionKeyHelper>();
79
80
81
82
83
84 MultiSourcePropertiesManager(final boolean lenient,
85 final Collection<PropertyProvider> rootPropertyProviders)
86 {
87 this.lenient = lenient;
88 this.rootPropertyProviders = rootPropertyProviders;
89 }
90
91
92
93
94
95
96
97
98
99
100
101 MultiSourceProperties create(final ConfigurationKey key)
102 {
103 MultiSourceProperties allProperties = dictionary.get(key);
104 if (allProperties == null)
105 {
106 allProperties =
107 new MultiSourceProperties(key, lenient ? null
108 : new ArrayList<ConfigurationException>());
109 allProperties.addProviders(rootPropertyProviders);
110 dictionary.put(key, allProperties);
111 }
112
113 return allProperties;
114 }
115
116 void create()
117 {
118 for (final PropertyProvider provider : rootPropertyProviders)
119 {
120 final ConfigurationKey key = provider.getConfigurationKey();
121 MultiSourceProperties allProperties = dictionary.get(key);
122 if (allProperties == null)
123 {
124 allProperties =
125 new MultiSourceProperties(key, lenient ? null
126 : new ArrayList<ConfigurationException>());
127 allProperties.addProvider(provider);
128 dictionary.put(key, allProperties);
129 }
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 @CheckForNull
146 DefinitionKeyHelper getDefinition(final ClassPathContext context)
147 {
148 DefinitionKeyHelper definition = definitionCache.get(context);
149 if (definition == null)
150 {
151 final DefinitionConfigParser parser = new DefinitionConfigParser();
152 try
153 {
154 final PropertiesDefinitionContext definitionContext =
155 parser.parse(context);
156 definition = new DefinitionKeyHelper(definitionContext);
157 }
158 catch (final ConfigException e)
159 {
160 definition = new DefinitionKeyHelper();
161 }
162
163 definitionCache.put(context, definition);
164 }
165
166 return definition;
167 }
168
169 Collection<MultiSourceProperties> getProperties()
170 {
171 return dictionary.values();
172 }
173
174
175
176 }