DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
...
- Command line arguments
- Properties passed as key=value pairs via command line (Command line properties)
- Configuration files
- Default values set by tool scripts and default values of command line arguments
- Default values set by Kafka components
In addition, a warning is issued when the same configuration appears in multiple sources (command line, command line properties, or configuration file), which is helpful for troubleshooting if users did not intend to specify the same configuration multiple times.
Note: property or map entries may have empty string or null values, as some configurations allow them, so the value is used verbatim.
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
/**
* Merge multiple configuration sources according to priority, from highest to lowest:
* 1) Command line arguments
* 2) Properties passed as key=value pairs via command line (Command line properties)
* 3) Configuration files
* 4) Default values set by tool scripts and default values of command line arguments
*
* @param commandLineMap Map of configuration from command line arguments (highest priority)
* @param commandLineKeyValMap Map of configuration from key=value pairs passed via command line
(command line properties)
* @param configMap Map of configuration loaded from configuration files
* @param toolDefaultMap Map of default values provided by tool scripts or default command line options
* @return a merged Map of all configuration sources, where higher-priority sources override lower-priority ones
*/
public static Map<String, Object> mergePropertiesWithPrecedence(
Map<String, Object> commandLineMap,
Map<String, Object> commandLineKeyValMap,
Map<String, Object> configMap,
Map<String, Object> toolDefaultMap
) {
warnConfigFromMultipleSources(commandLineMap, commandLineKeyValMap, configMap);
Map<String, Object> map = new HashMap<>();
// Default values set by tool scripts and default values of command line arguments
if (toolDefaultMap != null) {
map.putAll(toolDefaultMap);
}
// Configuration file
if (configMap != null) {
map.putAll(configMap);
}
// Properties passed as key=value pairs via command line (Command line properties)
if (commandLineKeyValMap != null) {
map.putAll(commandLineKeyValMap);
}
// Command line arguments
if (commandLineMap != null) {
map.putAll(commandLineMap);
}
return map;
}
/**
* Print warnings when the same configurations is defined in multiple sources.
* - Command line arguments override command property values and configuration files.
* - Command property values override configuration files if no command line argument exists.
*
* @param commandLineMap Map of configuration from command line arguments
* @param commandLineKeyValMap Map of configuration from command line properties
* @param configMap Map of configuration loaded from configuration files
*/
public static void warnConfigFromMultipleSources(
Map<String, Object> commandLineMap,
Map<String, Object> commandLineKeyValMap,
Map<String, Object> configMap
) {
// Command line argument overrides command property and config file
for (Map.Entry<String, Object> entry : commandLineMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
StringBuilder warning = new StringBuilder();
if (commandLineKeyValMap.containsKey(key) && configMap.containsKey(key)) {
warning.append(key).append("=").append(value)
.append(" from command line argument overrides command property value (")
.append(commandLineKeyValMap.get(key)).append(")")
.append(" and configuration file value (")
.append(configMap.get(key)).append(").");
} else if (commandLineKeyValMap.containsKey(key)) {
warning.append(key).append("=").append(value)
.append(" from command line argument overrides command property value (")
.append(commandLineKeyValMap.get(key)).append(").");
} else if (configMap.containsKey(key)) {
warning.append(key).append("=").append(value)
.append(" from command line overrides configuration file value (")
.append(configMap.get(key)).append(").");
}
if (!warning.isEmpty()) {
System.out.println("Warning: " + warning);
}
}
// Command property override config file
for (Map.Entry<String, Object> entry : commandLineKeyValMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (commandLineMap.containsKey(key)) continue;
if (configMap.containsKey(key)) {
System.out.println("Warning: " + key + "=" + value
+ " from command property overrides configuration file value ("
+ configMap.get(key) + ").");
}
}
}
// joptsimple-specific helper method
public static <T> void putIfOption(OptionSet options, OptionSpec<T> spec, Map<String, Object> map, String key) {
if (options.has(spec)) {
T value = options.valueOf(spec);
map.put(key, value);
}
}
// joptsimple-specific helper method
public static <T> void putOption(OptionSet options, OptionSpec<T> spec, Map<String, Object> map, String key) {
T value = options.valueOf(spec);
map.put(key, value);
}
|
...