1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.validation;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
26 import de.smartics.properties.api.config.domain.ConfigurationValidationException;
27 import de.smartics.properties.api.config.domain.Property;
28 import de.smartics.properties.api.config.domain.PropertyCollection;
29 import de.smartics.properties.api.config.domain.UnknownProperties;
30 import de.smartics.properties.api.config.domain.UnknownPropertyException;
31 import de.smartics.properties.api.core.domain.PropertyDescriptor;
32 import de.smartics.properties.api.core.domain.PropertyException;
33 import de.smartics.properties.spi.core.validate.PropertyValidator;
34 import de.smartics.util.lang.Arg;
35 import de.smartics.util.lang.NullArgumentException;
36
37
38
39
40
41 public final class ConfigurationValidator implements Serializable
42 {
43
44
45
46
47
48
49
50 private static final long serialVersionUID = 1L;
51
52
53
54
55 private static final Logger LOG = LoggerFactory
56 .getLogger(ConfigurationValidator.class);
57
58
59
60
61
62
63 private final ConfigurationPropertiesManagement configuration;
64
65
66
67
68
69
70 private final boolean lenient;
71
72
73
74
75
76
77
78
79
80
81 public ConfigurationValidator(
82 final ConfigurationPropertiesManagement configuration)
83 {
84 this(configuration, false);
85 }
86
87
88
89
90
91
92
93
94 public ConfigurationValidator(
95 final ConfigurationPropertiesManagement configuration,
96 final boolean lenient)
97 {
98 this.configuration = configuration;
99 this.lenient = lenient;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public void validate(final PropertyCollection properties,
121 final Class<?>... groups)
122 throws NullArgumentException, ConfigurationValidationException
123 {
124 Arg.checkNotNull("properties", properties);
125
126 final List<PropertyException> propertyViolations =
127 new ArrayList<PropertyException>();
128 final UnknownProperties unvalidatedProperties = new UnknownProperties();
129 final List<PropertyDescriptor> mandatoryProperties =
130 configuration.getMandatoryPropertyDescriptors();
131
132 try
133 {
134 for (final Class<?> group : calcGroups(groups))
135 {
136 for (final Property property : properties)
137 {
138 final String key = property.getName();
139
140 try
141 {
142 final PropertyDescriptor descriptor =
143 configuration.getDescriptor(key);
144
145 try
146 {
147 final Object value = configuration.getPropertyAsType(descriptor);
148 validate(descriptor, value, group);
149 mandatoryProperties.remove(descriptor);
150 }
151 catch (final PropertyException e)
152 {
153 if (property.getValue() != null)
154 {
155 propertyViolations.add(e);
156 }
157 }
158 }
159 catch (final UnknownPropertyException e)
160 {
161 unvalidatedProperties.add(property);
162 }
163 }
164 }
165 }
166 finally
167 {
168 properties.close();
169 }
170
171 if (isExceptionToBeThrown(propertyViolations, mandatoryProperties,
172 unvalidatedProperties))
173 {
174 throw new ConfigurationValidationException(configuration.getKey(),
175 propertyViolations, mandatoryProperties, unvalidatedProperties);
176 }
177
178 if (!unvalidatedProperties.isEmpty() && !lenient)
179 {
180 LOG.warn("Encountered unknown properies: {}", unvalidatedProperties);
181 }
182 }
183
184 private Class<?>[] calcGroups(final Class<?>[] groups)
185 {
186 if (groups != null && groups.length > 0)
187 {
188 return groups;
189 }
190 return new Class[] { null };
191 }
192
193
194
195
196
197
198
199
200
201 public void validate(final PropertyDescriptor descriptor, final Object value,
202 final Class<?>... groups)
203 {
204 final PropertyValidator validator =
205 new PropertyValidator(configuration.isInAdminMode());
206 validator.validate(descriptor, value, groups);
207 }
208
209 private boolean isExceptionToBeThrown(
210 final List<PropertyException> propertyViolations,
211 final List<PropertyDescriptor> mandatoryProperties,
212 final UnknownProperties unvalidatedProperties)
213 {
214 boolean exceptional =
215 !(propertyViolations.isEmpty() && mandatoryProperties.isEmpty());
216 if (!exceptional)
217 {
218 exceptional = !(lenient || unvalidatedProperties.isEmpty());
219 }
220 return exceptional;
221 }
222
223
224
225 }