1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.definition;
17
18 import java.io.Serializable;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
25
26 import javax.annotation.CheckForNull;
27 import javax.annotation.concurrent.ThreadSafe;
28
29 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
30
31
32
33
34
35 @ThreadSafe
36 public final class PropertiesDefinitionContext implements Serializable
37 {
38
39
40
41
42
43
44
45 private static final long serialVersionUID = 1L;
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public static final Set<String> DEFAULT_TLDS;
60
61
62
63
64
65
66
67
68
69 private final Set<String> tlds;
70
71
72
73
74
75
76 private final Set<String> environments;
77
78
79
80
81
82
83 private final Set<String> nodes;
84
85
86
87
88
89
90 private final Set<String> groups;
91
92
93
94
95
96
97
98 private final Map<String, ConfigurationKey> files;
99
100
101
102 static
103 {
104 final Set<String> defaults = new HashSet<String>();
105 defaults.add("biz");
106 defaults.add("com");
107 defaults.add("edu");
108 defaults.add("gov");
109 defaults.add("info");
110 defaults.add("net");
111 defaults.add("org");
112 DEFAULT_TLDS = Collections.unmodifiableSet(defaults);
113 }
114
115
116
117
118
119
120
121 public PropertiesDefinitionContext()
122 {
123 this(DEFAULT_TLDS, null, null, null, null);
124 }
125
126
127
128
129
130
131
132
133 public PropertiesDefinitionContext(final Set<String> environments,
134 final Set<String> nodes, final Set<String> groups)
135 {
136 this(DEFAULT_TLDS, environments, nodes, groups);
137 }
138
139
140
141
142
143
144
145
146
147
148 public PropertiesDefinitionContext(final Set<String> tlds,
149 final Set<String> environments, final Set<String> nodes,
150 final Set<String> groups)
151 {
152 this(tlds, environments, nodes, groups, null);
153 }
154
155
156
157
158
159
160
161
162
163
164 public PropertiesDefinitionContext(final Set<String> environments,
165 final Set<String> nodes, final Set<String> groups,
166 final Map<String, ConfigurationKey> files)
167 {
168 this(DEFAULT_TLDS, environments, nodes, groups, files);
169 }
170
171
172
173
174
175
176
177
178
179
180
181 public PropertiesDefinitionContext(final Set<String> tlds,
182 final Set<String> environments, final Set<String> nodes,
183 final Set<String> groups, final Map<String, ConfigurationKey> files)
184 {
185 this.tlds = create(tlds);
186 this.environments = create(environments);
187 this.nodes = create(nodes);
188 this.groups = create(groups);
189 this.files = create(files);
190 }
191
192
193
194
195
196
197
198 private static Set<String> create(final Set<String> set)
199 {
200 return set != null ? new HashSet<String>(set) : new HashSet<String>();
201 }
202
203 private static Map<String, ConfigurationKey> create(
204 final Map<String, ConfigurationKey> map)
205 {
206 return map != null ? new HashMap<String, ConfigurationKey>(map)
207 : new HashMap<String, ConfigurationKey>();
208 }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 public boolean isRegisteredGroupTld(final String token)
224 {
225 return token != null && (tlds.contains(token) || isCountryTld(token));
226 }
227
228 private static boolean isCountryTld(final String tld)
229 {
230 return tld.length() == 2 && Character.isLetter(tld.charAt(0))
231 && Character.isLetter(tld.charAt(1));
232 }
233
234
235
236
237
238
239
240
241 public boolean isRegisteredEnvironment(final String token)
242 {
243 return environments.contains(token);
244 }
245
246
247
248
249
250
251
252
253 public boolean isRegisteredNode(final String token)
254 {
255 return nodes.contains(token);
256 }
257
258
259
260
261
262
263
264
265 public boolean isRegisteredGroup(final String token)
266 {
267 return groups.contains(token);
268 }
269
270
271
272
273
274
275
276
277
278 @CheckForNull
279 public ConfigurationKey getKey(final String path)
280 {
281 return files.get(path);
282 }
283
284
285
286
287
288
289
290
291
292
293 public boolean isGroup(final String token)
294 {
295 if (!isRegisteredGroup(token))
296 {
297 final String tld = determineTld(token);
298 if (isRegisteredGroupTld(tld))
299 {
300 return true;
301 }
302
303 return false;
304 }
305
306 return true;
307 }
308
309 private String determineTld(final String token)
310 {
311 final int index = token.indexOf('.');
312 if (index != -1)
313 {
314 return token.substring(0, index);
315 }
316 return null;
317 }
318
319
320
321 @Override
322 public String toString()
323 {
324 final StringBuilder buffer = new StringBuilder(128);
325
326 buffer.append("TLDs :");
327 for (final String tld : tlds)
328 {
329 buffer.append(' ').append(tld);
330 }
331
332 buffer.append("\nEnvironments :");
333 for (final String environment : environments)
334 {
335 buffer.append(' ').append(environment);
336 }
337
338 buffer.append("\nNodes :");
339 for (final String node : nodes)
340 {
341 buffer.append(' ').append(node);
342 }
343
344 buffer.append("\nGroups :");
345 for (final String group : groups)
346 {
347 buffer.append(' ').append(group);
348 }
349
350 buffer.append("\nExplicit Paths:");
351 for (final Entry<String, ConfigurationKey> entry : files.entrySet())
352 {
353 buffer.append(' ').append(entry.getKey()).append('=')
354 .append(entry.getValue());
355 }
356
357 return buffer.toString();
358 }
359 }