-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathEnvConfigSource.java
More file actions
373 lines (320 loc) · 12.6 KB
/
Copy pathEnvConfigSource.java
File metadata and controls
373 lines (320 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright 2017 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.smallrye.config;
import static io.smallrye.config.common.utils.ConfigSourceUtil.CONFIG_ORDINAL_KEY;
import static io.smallrye.config.common.utils.StringUtil.isAsciiLetterOrDigit;
import static io.smallrye.config.common.utils.StringUtil.isNumeric;
import static io.smallrye.config.common.utils.StringUtil.replaceNonAlphanumericByUnderscores;
import static io.smallrye.config.common.utils.StringUtil.toLowerCaseAndDotted;
import static java.lang.Character.toLowerCase;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import io.smallrye.config.common.AbstractConfigSource;
/**
* A {@link org.eclipse.microprofile.config.spi.ConfigSource} to access Environment Variables.
* <p>
*
* A property name matches to an environment variable with the following rules:
*
* <ol>
* <li>Match alphanumeric characters (any case)</li>
* <li>Match non-alphanumeric characters with <code>_</code></li>
* <li>Closing quotes in the end of a property name require a double <code>_</code></li>
* </ol>
* <p>
*
* Additionally, this implementation provides candidate matching dotted property name from the Environment
* Variable name. These are required when a consumer relies on the list of properties to find additional
* configurations. The MicroProfile Config specification defines a set of conversion rules to look up and find
* values from environment variables even when using their dotted version, but it is unclear about property names.
* <br>
* Because an environment variable name may only be represented by a subset of characters, it is not possible
* to represent exactly a dotted version name from an environment variable name, so consumers must be aware of such
* limitations.
*/
public class EnvConfigSource extends AbstractConfigSource {
private static final long serialVersionUID = -4525015934376795496L;
public static final String NAME = "EnvConfigSource";
public static final int ORDINAL = 300;
private final EnvVars envVars;
protected EnvConfigSource() {
this(ORDINAL);
}
protected EnvConfigSource(final int ordinal) {
this(getEnvProperties(), ordinal);
}
public EnvConfigSource(final Map<String, String> properties, final int ordinal) {
super(NAME, getEnvOrdinal(properties, ordinal));
this.envVars = new EnvVars(properties);
}
@Override
public Map<String, String> getProperties() {
Map<String, String> properties = new HashMap<>();
for (Map.Entry<EnvName, EnvEntry> entry : envVars.getEnv().entrySet()) {
EnvEntry entryValue = entry.getValue();
if (entryValue.getEntries() != null) {
properties.putAll(entryValue.getEntries());
} else {
properties.put(entryValue.getName(), entryValue.getValue());
}
}
return properties;
}
@Override
public Set<String> getPropertyNames() {
return envVars.getNames();
}
@Override
public String getValue(final String propertyName) {
return envVars.get(propertyName);
}
boolean hasPropertyName(final String propertyName) {
return envVars.getEnv().containsKey(new EnvName(propertyName));
}
/**
* A new Map with the contents of System.getEnv. In the Windows implementation, the Map is an extension of
* ProcessEnvironment. This causes issues with Graal and native mode, since ProcessEnvironment should not be
* instantiated in the heap.
*/
private static Map<String, String> getEnvProperties() {
return new HashMap<>(System.getenv());
}
private static int getEnvOrdinal(final Map<String, String> properties, final int ordinal) {
String value = properties.get(CONFIG_ORDINAL_KEY);
if (value == null) {
value = properties.get(CONFIG_ORDINAL_KEY.toUpperCase());
}
if (value != null) {
return Converters.INTEGER_CONVERTER.convert(value);
}
return ordinal;
}
Object writeReplace() {
return new Ser();
}
static final class Ser implements Serializable {
private static final long serialVersionUID = 6812312718645271331L;
Object readResolve() {
return new EnvConfigSource();
}
}
static final class EnvVars implements Serializable {
private static final long serialVersionUID = -56318356411229247L;
private final Map<EnvName, EnvEntry> env;
private final Set<String> names;
public EnvVars(final Map<String, String> properties) {
this.env = new HashMap<>(properties.size());
this.names = new HashSet<>(properties.size() * 2);
for (Map.Entry<String, String> entry : properties.entrySet()) {
EnvName envName = new EnvName(entry.getKey());
EnvEntry envEntry = env.get(envName);
if (envEntry == null) {
env.put(envName, new EnvEntry(entry.getKey(), entry.getValue()));
} else {
envEntry.add(entry.getKey(), entry.getValue());
}
this.names.add(entry.getKey());
this.names.add(toLowerCaseAndDotted(entry.getKey()));
}
}
public String get(final String propertyName) {
EnvEntry envEntry = env.get(new EnvName(propertyName));
if (envEntry != null) {
String value = envEntry.get();
if (value != null) {
return value;
}
value = envEntry.getEntries().get(propertyName);
if (value != null) {
return value;
}
String envName = replaceNonAlphanumericByUnderscores(propertyName);
value = envEntry.getEntries().get(envName);
if (value != null) {
return value;
}
return envEntry.envEntries.get(envName.toUpperCase());
}
return null;
}
public Map<EnvName, EnvEntry> getEnv() {
return env;
}
public Set<String> getNames() {
return names;
}
}
static final class EnvName implements Serializable {
private static final long serialVersionUID = -2679716955093904512L;
private final String name;
public EnvName(final String name) {
assert name != null;
this.name = name;
}
public String getName() {
return name;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final EnvName that = (EnvName) o;
return equals(this.name, that.name);
}
@Override
public int hashCode() {
int h = 0;
int length = name.length();
if (length >= 2) {
if (name.charAt(length - 1) == '_' && name.charAt(length - 2) == '_') {
length = length - 1;
}
}
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if (i == 0 && length > 1) {
// The first '%' or '_' is meaninful because it represents a profiled property name
if ((c == '%' || c == '_') && isAsciiLetterOrDigit(name.charAt(i + 1))) {
h = 31 * h + 31;
continue;
}
}
if (isAsciiLetterOrDigit(c)) {
h = 31 * h + toLowerCase(c);
}
}
return h;
}
@SuppressWarnings("squid:S4973")
static boolean equals(final String name, final String other) {
//noinspection StringEquality
if (name == other) {
return true;
}
if (name.isEmpty() && other.isEmpty()) {
return true;
}
if (name.isEmpty() || other.isEmpty()) {
return false;
}
char n;
char o;
int matchPosition = name.length() - 1;
for (int i = other.length() - 1; i >= 0; i--) {
if (matchPosition == -1) {
return false;
}
o = other.charAt(i);
n = name.charAt(matchPosition);
// profile
if (i == 0 && (o == '%' || o == '_')) {
if (n == '%' || n == '_') {
return true;
}
}
if (o == '.') {
if (n != '.' && n != '-' && n != '_' && n != '/') {
return false;
}
} else if (o == '-') {
if (n != '.' && n != '-' && n != '_' && n != '/') {
return false;
}
} else if (o == '"') {
if (n != '"' && n != '_') {
return false;
} else if (n == '_' && name.length() - 1 == matchPosition) {
matchPosition = name.lastIndexOf("_", matchPosition - 1);
if (matchPosition == -1) {
return false;
}
}
} else if (o == ']') {
if (n != ']' && n != '_') {
return false;
}
int beginIndexed = other.lastIndexOf('[', i);
if (beginIndexed != -1) {
int range = i - beginIndexed - 1;
if (name.lastIndexOf('_', matchPosition - 1) == matchPosition - range - 1
|| name.lastIndexOf('[', matchPosition - 1) == matchPosition - range - 1) {
if (isNumeric(other, beginIndexed + range, i)
&& isNumeric(name, matchPosition - range, matchPosition)) {
matchPosition = matchPosition - range - 2;
i = i - range - 1;
continue;
}
}
}
return false;
} else if (o == '_') {
if (isAsciiLetterOrDigit(n)) {
return false;
} else if (n == '"' && other.length() - 1 == i) {
i = other.lastIndexOf("_", i - 1);
if (i == -1) {
return false;
}
}
} else if (!isAsciiLetterOrDigit(o)) {
if (o != n && n != '_') {
return false;
}
} else if (toLowerCase(o) != toLowerCase(n)) {
return false;
}
matchPosition--;
}
return matchPosition <= 0;
}
}
static final class EnvEntry implements Serializable {
private static final long serialVersionUID = -8786927401082731020L;
private final String name;
private final String value;
private Map<String, String> envEntries;
EnvEntry(final String name, final String value) {
this.name = name;
this.value = value;
}
String getName() {
return name;
}
String getValue() {
return value;
}
String get() {
return envEntries == null ? value : null;
}
Map<String, String> getEntries() {
return envEntries;
}
void add(String name, String value) {
if (envEntries == null) {
envEntries = new HashMap<>();
envEntries.put(this.name, this.value);
}
envEntries.put(name, value);
}
}
}