1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.impl.config.domain.key.envapp;
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 EnvAppDefinitionKeyHelper 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 EnvAppPropertiesDefinitionContext context;
57
58
59
60
61
62
63
64
65
66 public EnvAppDefinitionKeyHelper()
67 {
68 this(new EnvAppPropertiesDefinitionContext());
69 }
70
71
72
73
74
75
76
77
78 public EnvAppDefinitionKeyHelper(
79 final EnvAppPropertiesDefinitionContext 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 @Override
149 public ConfigurationKey<?> parse(final String pathWithFile)
150 throws IllegalArgumentException
151 {
152 final ConfigurationKey<?> explicitKey = fetchExplicitKey(pathWithFile);
153 if (explicitKey != null)
154 {
155 return explicitKey;
156 }
157
158 final String path = chopFile(pathWithFile);
159
160 final StringTokenizer tokenizer = new StringTokenizer(path, "/");
161 if (tokenizer.hasMoreTokens())
162 {
163 final ConfigurationKey<?> key;
164
165 final String token = tokenizer.nextToken();
166 if (isGroup(token))
167 {
168 key = parseApplicationKey(token, tokenizer);
169 }
170 else
171 {
172 key = parseEnvironmentKey(token, tokenizer);
173 }
174
175 return key;
176 }
177
178 return ConfigurationKeyContextManager.INSTANCE.context()
179 .configurationKeyFactory().createDefaultKey();
180
181
182 }
183
184 private static String chopFile(final String pathWithFile)
185 {
186 if (pathWithFile.endsWith(".properties"))
187 {
188 final int lastSlash = pathWithFile.lastIndexOf('/');
189 if (lastSlash != -1)
190 {
191 final String path = pathWithFile.substring(0, lastSlash);
192 return path;
193 }
194
195 return "";
196 }
197 return pathWithFile;
198 }
199
200 private ConfigurationKey<?> fetchExplicitKey(final String path)
201 {
202 ConfigurationKey<?> key = context.getKey(path);
203 if (key == null)
204 {
205 key = context.getKey(null);
206 }
207 return key;
208 }
209
210 private boolean isGroup(final String token)
211 {
212 if (context.isRegisteredEnvironment(token)
213 || context.isRegisteredNode(token))
214 {
215 return false;
216 }
217
218 return context.isGroup(token);
219 }
220
221 private ConfigurationKey<?> parseEnvironmentKey(final String environment,
222 final StringTokenizer tokenizer)
223 {
224 final EnvironmentId envId;
225 final ApplicationId appId;
226 if (tokenizer.hasMoreTokens())
227 {
228 final String token = tokenizer.nextToken();
229 if (isGroup(token))
230 {
231 envId = new EnvironmentId(environment);
232 appId = parseAppId(token, tokenizer);
233 }
234 else
235 {
236 envId = new EnvironmentId(environment, token);
237 if (tokenizer.hasMoreTokens())
238 {
239 appId = parseAppId(tokenizer.nextToken(), tokenizer);
240 }
241 else
242 {
243 appId = ANY_APP;
244 }
245 }
246 }
247 else
248 {
249 envId = new EnvironmentId(environment);
250 appId = ANY_APP;
251 }
252
253 final ConfigurationKey<?> key = new EnvAppConfigurationKey(envId, appId);
254
255 return key;
256 }
257
258 private ApplicationId parseAppId(final String group,
259 final StringTokenizer tokenizer)
260 {
261 String artifact = null;
262 String version = null;
263 if (tokenizer.hasMoreTokens())
264 {
265 artifact = tokenizer.nextToken();
266 if (tokenizer.hasMoreTokens())
267 {
268 version = tokenizer.nextToken();
269 }
270 }
271
272 return new ApplicationId(group, artifact, version);
273 }
274
275 private ConfigurationKey<?> parseApplicationKey(final String group,
276 final StringTokenizer tokenizer)
277 {
278 final EnvironmentId envId = ANY_ENV;
279 final ApplicationId appId = parseAppId(group, tokenizer);
280
281 return new EnvAppConfigurationKey(envId, appId);
282 }
283
284
285
286 }