/
AbstractConfig.java
432 lines (379 loc) · 16.5 KB
/
AbstractConfig.java
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.kafka.common.config;
import org.apache.kafka.common.Configurable;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.config.types.Password;
import org.apache.kafka.common.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* A convenient base class for configurations to extend.
* <p>
* This class holds both the original configuration that was provided as well as the parsed
*/
public class AbstractConfig {
private final Logger log = LoggerFactory.getLogger(getClass());
/* configs for which values have been requested, used to detect unused configs */
private final Set<String> used;
/* the original values passed in by the user */
private final Map<String, ?> originals;
/* the parsed values */
private final Map<String, Object> values;
private final ConfigDef definition;
@SuppressWarnings("unchecked")
public AbstractConfig(ConfigDef definition, Map<?, ?> originals, boolean doLog) {
/* check that all the keys are really strings */
for (Map.Entry<?, ?> entry : originals.entrySet())
if (!(entry.getKey() instanceof String))
throw new ConfigException(entry.getKey().toString(), entry.getValue(), "Key must be a string.");
this.originals = (Map<String, ?>) originals;
this.values = definition.parse(this.originals);
Map<String, Object> configUpdates = postProcessParsedConfig(Collections.unmodifiableMap(this.values));
for (Map.Entry<String, Object> update : configUpdates.entrySet()) {
this.values.put(update.getKey(), update.getValue());
}
definition.parse(this.values);
this.used = Collections.synchronizedSet(new HashSet<>());
this.definition = definition;
if (doLog)
logAll();
}
public AbstractConfig(ConfigDef definition, Map<?, ?> originals) {
this(definition, originals, true);
}
/**
* Called directly after user configs got parsed (and thus default values got set).
* This allows to change default values for "secondary defaults" if required.
*
* @param parsedValues unmodifiable map of current configuration
* @return a map of updates that should be applied to the configuration (will be validated to prevent bad updates)
*/
protected Map<String, Object> postProcessParsedConfig(Map<String, Object> parsedValues) {
return Collections.emptyMap();
}
protected Object get(String key) {
if (!values.containsKey(key))
throw new ConfigException(String.format("Unknown configuration '%s'", key));
used.add(key);
return values.get(key);
}
public void ignore(String key) {
used.add(key);
}
public Short getShort(String key) {
return (Short) get(key);
}
public Integer getInt(String key) {
return (Integer) get(key);
}
public Long getLong(String key) {
return (Long) get(key);
}
public Double getDouble(String key) {
return (Double) get(key);
}
@SuppressWarnings("unchecked")
public List<String> getList(String key) {
return (List<String>) get(key);
}
public Boolean getBoolean(String key) {
return (Boolean) get(key);
}
public String getString(String key) {
return (String) get(key);
}
public ConfigDef.Type typeOf(String key) {
ConfigDef.ConfigKey configKey = definition.configKeys().get(key);
if (configKey == null)
return null;
return configKey.type;
}
public Password getPassword(String key) {
return (Password) get(key);
}
public Class<?> getClass(String key) {
return (Class<?>) get(key);
}
public Set<String> unused() {
Set<String> keys = new HashSet<>(originals.keySet());
keys.removeAll(used);
return keys;
}
public Map<String, Object> originals() {
Map<String, Object> copy = new RecordingMap<>();
copy.putAll(originals);
return copy;
}
/**
* Get all the original settings, ensuring that all values are of type String.
* @return the original settings
* @throws ClassCastException if any of the values are not strings
*/
public Map<String, String> originalsStrings() {
Map<String, String> copy = new RecordingMap<>();
for (Map.Entry<String, ?> entry : originals.entrySet()) {
if (!(entry.getValue() instanceof String))
throw new ClassCastException("Non-string value found in original settings for key " + entry.getKey() +
": " + (entry.getValue() == null ? null : entry.getValue().getClass().getName()));
copy.put(entry.getKey(), (String) entry.getValue());
}
return copy;
}
/**
* Gets all original settings with the given prefix, stripping the prefix before adding it to the output.
*
* @param prefix the prefix to use as a filter
* @return a Map containing the settings with the prefix
*/
public Map<String, Object> originalsWithPrefix(String prefix) {
return originalsWithPrefix(prefix, true);
}
/**
* Gets all original settings with the given prefix.
*
* @param prefix the prefix to use as a filter
* @param strip strip the prefix before adding to the output if set true
* @return a Map containing the settings with the prefix
*/
public Map<String, Object> originalsWithPrefix(String prefix, boolean strip) {
Map<String, Object> result = new RecordingMap<>(prefix, false);
for (Map.Entry<String, ?> entry : originals.entrySet()) {
if (entry.getKey().startsWith(prefix) && entry.getKey().length() > prefix.length()) {
if (strip)
result.put(entry.getKey().substring(prefix.length()), entry.getValue());
else
result.put(entry.getKey(), entry.getValue());
}
}
return result;
}
/**
* Put all keys that do not start with {@code prefix} and their parsed values in the result map and then
* put all the remaining keys with the prefix stripped and their parsed values in the result map.
*
* This is useful if one wants to allow prefixed configs to override default ones.
* <p>
* Two forms of prefixes are supported:
* <ul>
* <li>listener.name.{listenerName}.some.prop: If the provided prefix is `listener.name.{listenerName}.`,
* the key `some.prop` with the value parsed using the definition of `some.prop` is returned.</li>
* <li>listener.name.{listenerName}.{mechanism}.some.prop: If the provided prefix is `listener.name.{listenerName}.`,
* the key `{mechanism}.some.prop` with the value parsed using the definition of `some.prop` is returned.
* This is used to provide per-mechanism configs for a broker listener (e.g sasl.jaas.config)</li>
* </ul>
* </p>
*/
public Map<String, Object> valuesWithPrefixOverride(String prefix) {
Map<String, Object> result = new RecordingMap<>(values(), prefix, true);
for (Map.Entry<String, ?> entry : originals.entrySet()) {
if (entry.getKey().startsWith(prefix) && entry.getKey().length() > prefix.length()) {
String keyWithNoPrefix = entry.getKey().substring(prefix.length());
ConfigDef.ConfigKey configKey = definition.configKeys().get(keyWithNoPrefix);
if (configKey != null)
result.put(keyWithNoPrefix, definition.parseValue(configKey, entry.getValue(), true));
else {
String keyWithNoSecondaryPrefix = keyWithNoPrefix.substring(keyWithNoPrefix.indexOf('.') + 1);
configKey = definition.configKeys().get(keyWithNoSecondaryPrefix);
if (configKey != null)
result.put(keyWithNoPrefix, definition.parseValue(configKey, entry.getValue(), true));
}
}
}
return result;
}
/**
* If at least one key with {@code prefix} exists, all prefixed values will be parsed and put into map.
* If no value with {@code prefix} exists all unprefixed values will be returned.
*
* This is useful if one wants to allow prefixed configs to override default ones, but wants to use either
* only prefixed configs or only regular configs, but not mix them.
*/
public Map<String, Object> valuesWithPrefixAllOrNothing(String prefix) {
Map<String, Object> withPrefix = originalsWithPrefix(prefix, true);
if (withPrefix.isEmpty()) {
return new RecordingMap<>(values(), "", true);
} else {
Map<String, Object> result = new RecordingMap<>(prefix, true);
for (Map.Entry<String, ?> entry : withPrefix.entrySet()) {
ConfigDef.ConfigKey configKey = definition.configKeys().get(entry.getKey());
if (configKey != null)
result.put(entry.getKey(), definition.parseValue(configKey, entry.getValue(), true));
}
return result;
}
}
public Map<String, ?> values() {
return new RecordingMap<>(values);
}
private void logAll() {
StringBuilder b = new StringBuilder();
b.append(getClass().getSimpleName());
b.append(" values: ");
b.append(Utils.NL);
for (Map.Entry<String, Object> entry : new TreeMap<>(this.values).entrySet()) {
b.append('\t');
b.append(entry.getKey());
b.append(" = ");
b.append(entry.getValue());
b.append(Utils.NL);
}
log.info(b.toString());
}
/**
* Log warnings for any unused configurations
*/
public void logUnused() {
for (String key : unused())
log.warn("The configuration '{}' was supplied but isn't a known config.", key);
}
/**
* Get a configured instance of the give class specified by the given configuration key. If the object implements
* Configurable configure it using the configuration.
*
* @param key The configuration key for the class
* @param t The interface the class should implement
* @return A configured instance of the class
*/
public <T> T getConfiguredInstance(String key, Class<T> t) {
Class<?> c = getClass(key);
if (c == null)
return null;
Object o = Utils.newInstance(c);
if (!t.isInstance(o))
throw new KafkaException(c.getName() + " is not an instance of " + t.getName());
if (o instanceof Configurable)
((Configurable) o).configure(originals());
return t.cast(o);
}
/**
* Get a list of configured instances of the given class specified by the given configuration key. The configuration
* may specify either null or an empty string to indicate no configured instances. In both cases, this method
* returns an empty list to indicate no configured instances.
* @param key The configuration key for the class
* @param t The interface the class should implement
* @return The list of configured instances
*/
public <T> List<T> getConfiguredInstances(String key, Class<T> t) {
return getConfiguredInstances(key, t, Collections.emptyMap());
}
/**
* Get a list of configured instances of the given class specified by the given configuration key. The configuration
* may specify either null or an empty string to indicate no configured instances. In both cases, this method
* returns an empty list to indicate no configured instances.
* @param key The configuration key for the class
* @param t The interface the class should implement
* @param configOverrides Configuration overrides to use.
* @return The list of configured instances
*/
public <T> List<T> getConfiguredInstances(String key, Class<T> t, Map<String, Object> configOverrides) {
return getConfiguredInstances(getList(key), t, configOverrides);
}
/**
* Get a list of configured instances of the given class specified by the given configuration key. The configuration
* may specify either null or an empty string to indicate no configured instances. In both cases, this method
* returns an empty list to indicate no configured instances.
* @param classNames The list of class names of the instances to create
* @param t The interface the class should implement
* @param configOverrides Configuration overrides to use.
* @return The list of configured instances
*/
public <T> List<T> getConfiguredInstances(List<String> classNames, Class<T> t, Map<String, Object> configOverrides) {
List<T> objects = new ArrayList<>();
if (classNames == null)
return objects;
Map<String, Object> configPairs = originals();
configPairs.putAll(configOverrides);
for (Object klass : classNames) {
Object o;
if (klass instanceof String) {
try {
o = Utils.newInstance((String) klass, t);
} catch (ClassNotFoundException e) {
throw new KafkaException(klass + " ClassNotFoundException exception occurred", e);
}
} else if (klass instanceof Class<?>) {
o = Utils.newInstance((Class<?>) klass);
} else
throw new KafkaException("List contains element of type " + klass.getClass().getName() + ", expected String or Class");
if (!t.isInstance(o))
throw new KafkaException(klass + " is not an instance of " + t.getName());
if (o instanceof Configurable)
((Configurable) o).configure(configPairs);
objects.add(t.cast(o));
}
return objects;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractConfig that = (AbstractConfig) o;
return originals.equals(that.originals);
}
@Override
public int hashCode() {
return originals.hashCode();
}
/**
* Marks keys retrieved via `get` as used. This is needed because `Configurable.configure` takes a `Map` instead
* of an `AbstractConfig` and we can't change that without breaking public API like `Partitioner`.
*/
private class RecordingMap<V> extends HashMap<String, V> {
private final String prefix;
private final boolean withIgnoreFallback;
RecordingMap() {
this("", false);
}
RecordingMap(String prefix, boolean withIgnoreFallback) {
this.prefix = prefix;
this.withIgnoreFallback = withIgnoreFallback;
}
RecordingMap(Map<String, ? extends V> m) {
this(m, "", false);
}
RecordingMap(Map<String, ? extends V> m, String prefix, boolean withIgnoreFallback) {
super(m);
this.prefix = prefix;
this.withIgnoreFallback = withIgnoreFallback;
}
@Override
public V get(Object key) {
if (key instanceof String) {
String stringKey = (String) key;
String keyWithPrefix;
if (prefix.isEmpty()) {
keyWithPrefix = stringKey;
} else {
keyWithPrefix = prefix + stringKey;
}
ignore(keyWithPrefix);
if (withIgnoreFallback)
ignore(stringKey);
}
return super.get(key);
}
}
}