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.util.lang.Arguments;
34 import de.smartics.util.lang.NullArgumentException;
35
36
37
38
39
40 public final class ConfigurationValidator implements Serializable
41 {
42
43
44
45
46
47
48
49 private static final long serialVersionUID = 1L;
50
51
52
53
54 private static final Logger LOG = LoggerFactory
55 .getLogger(ConfigurationValidator.class);
56
57
58
59
60
61
62 private final ConfigurationPropertiesManagement configuration;
63
64
65
66
67
68
69 private final boolean lenient;
70
71
72
73
74
75
76
77
78
79
80 public ConfigurationValidator(
81 final ConfigurationPropertiesManagement configuration)
82 {
83 this(configuration, false);
84 }
85
86
87
88
89
90
91
92
93 public ConfigurationValidator(
94 final ConfigurationPropertiesManagement configuration,
95 final boolean lenient)
96 {
97 this.configuration = configuration;
98 this.lenient = lenient;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public void validate(final PropertyCollection properties)
120 throws NullArgumentException, ConfigurationValidationException
121 {
122 Arguments.checkNotNull("properties", properties);
123
124 final List<PropertyException> propertyViolations =
125 new ArrayList<PropertyException>();
126 final UnknownProperties unvalidatedProperties = new UnknownProperties();
127 final List<PropertyDescriptor> mandatoryProperties =
128 configuration.getMandatoryPropertyDescriptors();
129
130 for (final Property property : properties)
131 {
132 final String key = property.getName();
133
134 try
135 {
136 final PropertyDescriptor descriptor = configuration.getDescriptor(key);
137
138 try
139 {
140 configuration.getPropertyValue(descriptor);
141 mandatoryProperties.remove(descriptor);
142 }
143 catch (final PropertyException e)
144 {
145 if (property.getValue() != null)
146 {
147 propertyViolations.add(e);
148 }
149 }
150 }
151 catch (final UnknownPropertyException e)
152 {
153 unvalidatedProperties.add(property);
154 }
155 }
156
157 if (isExceptionToBeThrown(propertyViolations, mandatoryProperties,
158 unvalidatedProperties))
159 {
160 throw new ConfigurationValidationException(configuration.getKey(),
161 propertyViolations, mandatoryProperties, unvalidatedProperties);
162 }
163
164 if (!unvalidatedProperties.isEmpty() && !lenient)
165 {
166 LOG.warn("Encountered unknown properies: {}", unvalidatedProperties);
167 }
168 }
169
170 private boolean isExceptionToBeThrown(
171 final List<PropertyException> propertyViolations,
172 final List<PropertyDescriptor> mandatoryProperties,
173 final UnknownProperties unvalidatedProperties)
174 {
175 boolean exceptional =
176 !(propertyViolations.isEmpty() && mandatoryProperties.isEmpty());
177 if (!exceptional)
178 {
179 exceptional = !(lenient || unvalidatedProperties.isEmpty());
180 }
181 return exceptional;
182 }
183
184
185
186 }