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 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.util.lang.Arguments;
30
31
32
33
34 @ThreadSafe
35 public final class DefinitionKeyHelper implements Serializable
36 {
37
38
39
40
41
42
43
44 private static final long serialVersionUID = 1L;
45
46
47
48
49
50
51
52
53 private final PropertiesDefinitionContext context;
54
55
56
57
58
59
60
61
62
63 public DefinitionKeyHelper()
64 {
65 this(new PropertiesDefinitionContext());
66 }
67
68
69
70
71
72
73
74
75 public DefinitionKeyHelper(final PropertiesDefinitionContext context)
76 throws NullPointerException
77 {
78 Arguments.checkNotNull("context", context);
79
80 this.context = context;
81 }
82
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 public ConfigurationKey parse(final String pathWithFile)
147 throws IllegalArgumentException
148 {
149 final ConfigurationKey explicitKey = fetchExplicitKey(pathWithFile);
150 if (explicitKey != null)
151 {
152 return explicitKey;
153 }
154
155 final String path = chopFile(pathWithFile);
156
157 final StringTokenizer tokenizer = new StringTokenizer(path, "/");
158 if (tokenizer.hasMoreTokens())
159 {
160 final ConfigurationKey key;
161
162 final String token = tokenizer.nextToken();
163 if (isGroup(token))
164 {
165 key = parseApplicationKey(token, tokenizer);
166 }
167 else
168 {
169 key = parseEnvironmentKey(token, tokenizer);
170 }
171
172 return key;
173 }
174
175 return new ConfigurationKey(EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
176
177
178 }
179
180 private static String chopFile(final String pathWithFile)
181 {
182 if (pathWithFile.endsWith(".properties"))
183 {
184 final int lastSlash = pathWithFile.lastIndexOf('/');
185 if (lastSlash != -1)
186 {
187 final String path = pathWithFile.substring(0, lastSlash);
188 return path;
189 }
190
191 return "";
192 }
193 return pathWithFile;
194 }
195
196 private ConfigurationKey fetchExplicitKey(final String path)
197 {
198 ConfigurationKey key = context.getKey(path);
199 if (key == null)
200 {
201 key = context.getKey(null);
202 }
203 return key;
204 }
205
206 private boolean isGroup(final String token)
207 {
208 if (context.isRegisteredEnvironment(token)
209 || context.isRegisteredNode(token))
210 {
211 return false;
212 }
213
214 return context.isGroup(token);
215 }
216
217 private ConfigurationKey parseEnvironmentKey(final String environment,
218 final StringTokenizer tokenizer)
219 {
220 final EnvironmentId envId;
221 final ApplicationId appId;
222 if (tokenizer.hasMoreTokens())
223 {
224 final String token = tokenizer.nextToken();
225 if (isGroup(token))
226 {
227 envId = new EnvironmentId(environment);
228 appId = parseAppId(token, tokenizer);
229 }
230 else
231 {
232 envId = new EnvironmentId(environment, token);
233 if (tokenizer.hasMoreTokens())
234 {
235 appId = parseAppId(tokenizer.nextToken(), tokenizer);
236 }
237 else
238 {
239 appId = ANY_APP;
240 }
241 }
242 }
243 else
244 {
245 envId = new EnvironmentId(environment);
246 appId = ANY_APP;
247 }
248
249 final ConfigurationKey key = new ConfigurationKey(envId, appId);
250
251 return key;
252 }
253
254 private ApplicationId parseAppId(final String group,
255 final StringTokenizer tokenizer)
256 {
257 String artifact = null;
258 String version = null;
259 if (tokenizer.hasMoreTokens())
260 {
261 artifact = tokenizer.nextToken();
262 if (tokenizer.hasMoreTokens())
263 {
264 version = tokenizer.nextToken();
265 }
266 }
267
268 return new ApplicationId(group, artifact, version);
269 }
270
271 private ConfigurationKey parseApplicationKey(final String group,
272 final StringTokenizer tokenizer)
273 {
274 final EnvironmentId envId = ANY_ENV;
275 final ApplicationId appId = parseAppId(group, tokenizer);
276
277 return new ConfigurationKey(envId, appId);
278 }
279
280
281
282 }