1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.domain.key.rtaware;
17
18 import static de.smartics.properties.api.config.domain.key.ApplicationId.ANY_APP;
19 import static de.smartics.properties.api.config.domain.key.EnvironmentId.ANY_ENV;
20
21 import java.io.Serializable;
22 import java.util.StringTokenizer;
23
24 import javax.annotation.concurrent.ThreadSafe;
25
26 import de.smartics.properties.api.config.domain.key.ApplicationId;
27 import de.smartics.properties.api.config.domain.key.ConfigurationKey;
28 import de.smartics.properties.api.config.domain.key.EnvironmentId;
29 import de.smartics.properties.spi.config.definition.DefinitionKeyHelper;
30 import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContextManager;
31 import de.smartics.util.lang.Arg;
32
33
34
35
36 @ThreadSafe
37 public final class TenantUserDefinitionKeyHelper implements Serializable,
38 DefinitionKeyHelper
39 {
40
41
42
43
44
45
46
47 private static final long serialVersionUID = 1L;
48
49
50
51
52
53
54
55
56 private final TenantUserPropertiesDefinitionContext context;
57
58
59
60
61
62
63
64
65
66 public TenantUserDefinitionKeyHelper()
67 {
68 this(new TenantUserPropertiesDefinitionContext());
69 }
70
71
72
73
74
75
76
77
78 public TenantUserDefinitionKeyHelper(
79 final TenantUserPropertiesDefinitionContext context)
80 throws NullPointerException
81 {
82 this.context = Arg.checkNotNull("context", context);
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 @Override
152 public ConfigurationKey<?> parse(final String pathWithFile)
153 throws IllegalArgumentException
154 {
155 final ConfigurationKey<?> explicitKey = fetchExplicitKey(pathWithFile);
156 if (explicitKey != null)
157 {
158 return explicitKey;
159 }
160
161 final String path = chopFile(pathWithFile);
162
163 final StringTokenizer tokenizer = new StringTokenizer(path, "/");
164 final UserId userId = UserId.ANY_USER;
165 final TenantId tenantId = TenantId.ANY_TENANT;
166 if (tokenizer.hasMoreTokens())
167 {
168 final ConfigurationKey<?> key;
169
170 final String token = tokenizer.nextToken();
171 if (isUser(token))
172 {
173 key = parseUserKey(token, tokenizer);
174 }
175 else if (isTenant(token))
176 {
177 key = parseTenantKey(userId, token, tokenizer);
178 }
179 else if (isGroup(token))
180 {
181 key = parseApplicationKey(userId, tenantId, token, tokenizer);
182 }
183 else
184 {
185 key = parseEnvironmentKey(userId, tenantId, token, tokenizer);
186 }
187
188 return key;
189 }
190
191 return ConfigurationKeyContextManager.INSTANCE.context()
192 .configurationKeyFactory().createDefaultKey();
193
194
195 }
196
197 private boolean isUser(final String token)
198 {
199 return token.startsWith("user.");
200 }
201
202 private boolean isTenant(final String token)
203 {
204 return token.startsWith("tenant.");
205 }
206
207 private static String chopFile(final String pathWithFile)
208 {
209 if (pathWithFile.endsWith(".properties"))
210 {
211 final int lastSlash = pathWithFile.lastIndexOf('/');
212 if (lastSlash != -1)
213 {
214 final String path = pathWithFile.substring(0, lastSlash);
215 return path;
216 }
217
218 return "";
219 }
220 return pathWithFile;
221 }
222
223 private ConfigurationKey<?> fetchExplicitKey(final String path)
224 {
225 ConfigurationKey<?> key = context.getKey(path);
226 if (key == null)
227 {
228 key = context.getKey(null);
229 }
230 return key;
231 }
232
233 private boolean isGroup(final String token)
234 {
235 if (context.isRegisteredEnvironment(token)
236 || context.isRegisteredNode(token))
237 {
238 return false;
239 }
240
241 return context.isGroup(token);
242 }
243
244 private UserId parseUserId(final String user)
245 {
246 final UserId id = new UserId(user.substring(user.indexOf('.') + 1));
247 return id;
248 }
249
250 private TenantId parseTenantId(final String tenant)
251 {
252 final TenantId id = new TenantId(tenant.substring(tenant.indexOf('.') + 1));
253 return id;
254 }
255
256 private ConfigurationKey<?> parseUserKey(final String user,
257 final StringTokenizer tokenizer)
258 {
259 final UserId userId = parseUserId(user);
260 final TenantId tenantId = TenantId.ANY_TENANT;
261 final ConfigurationKey<?> key;
262 if (tokenizer.hasMoreTokens())
263 {
264
265 final String token = tokenizer.nextToken();
266 if (isTenant(token))
267 {
268 key = parseTenantKey(userId, token, tokenizer);
269 }
270 else if (isGroup(token))
271 {
272 key = parseApplicationKey(userId, tenantId, token, tokenizer);
273 }
274 else
275 {
276 key = parseEnvironmentKey(userId, tenantId, token, tokenizer);
277 }
278 }
279 else
280 {
281 key =
282 new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
283 ApplicationId.ANY_APP, tenantId, userId);
284 }
285
286 return key;
287 }
288
289 private ConfigurationKey<?> parseTenantKey(final UserId userId,
290 final String tenant, final StringTokenizer tokenizer)
291 {
292 final TenantId tenantId = parseTenantId(tenant);
293
294 final ConfigurationKey<?> key;
295 if (tokenizer.hasMoreTokens())
296 {
297 final String token = tokenizer.nextToken();
298 if (isGroup(token))
299 {
300 key = parseApplicationKey(userId, tenantId, token, tokenizer);
301 }
302 else
303 {
304 key = parseEnvironmentKey(userId, tenantId, token, tokenizer);
305 }
306 }
307 else
308 {
309 key =
310 new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
311 ApplicationId.ANY_APP, tenantId, userId);
312 }
313
314 return key;
315 }
316
317 private ConfigurationKey<?> parseEnvironmentKey(final UserId userId,
318 final TenantId tenantId, final String environment,
319 final StringTokenizer tokenizer)
320 {
321 final EnvironmentId envId;
322 final ApplicationId appId;
323 if (tokenizer.hasMoreTokens())
324 {
325 final String token = tokenizer.nextToken();
326 if (isGroup(token))
327 {
328 envId = new EnvironmentId(environment);
329 appId = parseAppId(token, tokenizer);
330 }
331 else
332 {
333 envId = new EnvironmentId(environment, token);
334 if (tokenizer.hasMoreTokens())
335 {
336 appId = parseAppId(tokenizer.nextToken(), tokenizer);
337 }
338 else
339 {
340 appId = ANY_APP;
341 }
342 }
343 }
344 else
345 {
346 envId = new EnvironmentId(environment);
347 appId = ANY_APP;
348 }
349
350 final ConfigurationKey<?> key =
351 new TenantUserConfigurationKey(envId, appId, tenantId, userId);
352
353 return key;
354 }
355
356 private ApplicationId parseAppId(final String group,
357 final StringTokenizer tokenizer)
358 {
359 String artifact = null;
360 String version = null;
361 if (tokenizer.hasMoreTokens())
362 {
363 artifact = tokenizer.nextToken();
364 if (tokenizer.hasMoreTokens())
365 {
366 version = tokenizer.nextToken();
367 }
368 }
369
370 return new ApplicationId(group, artifact, version);
371 }
372
373 private ConfigurationKey<?> parseApplicationKey(final UserId userId,
374 final TenantId tenantId, final String group,
375 final StringTokenizer tokenizer)
376 {
377 final EnvironmentId envId = ANY_ENV;
378 final ApplicationId appId = parseAppId(group, tokenizer);
379
380 return new TenantUserConfigurationKey(envId, appId, tenantId, userId);
381 }
382
383
384
385 }