1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.api.config.domain;
17
18 import java.util.Collections;
19 import java.util.List;
20
21 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
22 import de.smartics.properties.api.core.domain.PropertyDescriptor;
23 import de.smartics.properties.api.core.domain.PropertyException;
24
25
26
27
28
29
30
31
32 public class ConfigurationValidationException extends ConfigurationException
33 {
34
35
36
37
38
39
40
41
42
43 private static final long serialVersionUID = 1L;
44
45
46
47
48
49
50
51
52 private final List<PropertyException> propertyViolations;
53
54
55
56
57
58
59 private final List<PropertyDescriptor> mandatoryProperties;
60
61
62
63
64
65
66 private final UnknownProperties unknownProperties;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public ConfigurationValidationException(final ConfigurationKey key,
82 final List<PropertyException> propertyViolations,
83 final List<PropertyDescriptor> mandatoryProperties,
84 final UnknownProperties unknownProperties)
85 {
86 this(null, key, propertyViolations, mandatoryProperties, unknownProperties);
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public ConfigurationValidationException(final Throwable cause,
102 final ConfigurationKey key,
103 final List<PropertyException> propertyViolations,
104 final List<PropertyDescriptor> mandatoryProperties,
105 final UnknownProperties unknownProperties)
106 {
107 super(createMessage(key, propertyViolations, mandatoryProperties,
108 unknownProperties), cause, key);
109 this.propertyViolations = propertyViolations;
110 this.mandatoryProperties = mandatoryProperties;
111 this.unknownProperties = unknownProperties;
112 }
113
114
115
116
117
118
119
120 private static String createMessage(final ConfigurationKey key,
121 final List<PropertyException> propertyViolations,
122 final List<PropertyDescriptor> mandatoryProperties,
123 final UnknownProperties unknownProperties)
124 {
125 final StringBuilder buffer = new StringBuilder();
126 buffer.append("Configuration '" + key + "' is invalid.\n");
127
128 addViolations(propertyViolations, buffer);
129 addMandatoryProperties(mandatoryProperties, buffer);
130 addUnknownProperties(unknownProperties, buffer);
131
132 return buffer.toString();
133 }
134
135 private static void addViolations(
136 final List<PropertyException> propertyViolations,
137 final StringBuilder buffer)
138 {
139 if (propertyViolations != null && !propertyViolations.isEmpty())
140 {
141 buffer.append("The following violations have been reported:\n");
142 for (final PropertyException violation : propertyViolations)
143 {
144 buffer.append(" ").append(violation.getMessage()).append('\n');
145 }
146 }
147 }
148
149 private static void addMandatoryProperties(
150 final List<PropertyDescriptor> mandatoryProperties,
151 final StringBuilder buffer)
152 {
153 if (mandatoryProperties != null && !mandatoryProperties.isEmpty())
154 {
155 buffer
156 .append("The following mandatory properties have not been provided:\n");
157 for (final PropertyDescriptor descriptor : mandatoryProperties)
158 {
159 buffer.append(" ").append(descriptor.getKey()).append('\n');
160 }
161 }
162 }
163
164 private static void addUnknownProperties(
165 final UnknownProperties unknownProperties, final StringBuilder buffer)
166 {
167 if (unknownProperties != null && !unknownProperties.isEmpty())
168 {
169 buffer.append("The following unknown properties have been reported:\n");
170 for (final Property property : unknownProperties.getProperties())
171 {
172 buffer.append(" ").append(property).append('\n');
173 }
174 }
175 }
176
177
178
179
180
181
182
183
184 public final List<PropertyException> getPropertyViolations()
185 {
186 return Collections.unmodifiableList(propertyViolations);
187 }
188
189
190
191
192
193
194
195
196 public final List<PropertyDescriptor> getMandatoryProperties()
197 {
198 return Collections.unmodifiableList(mandatoryProperties);
199 }
200
201
202
203
204
205
206 public final UnknownProperties getUnknownProperties()
207 {
208 return unknownProperties;
209 }
210
211
212
213
214 }