You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/05/20 07:09:26 UTC
[GitHub] [nifi] michael81877 commented on a diff in pull request #6061: NIFIDEVS-5378: prevent duplicate keys with different values in nifi.p…
michael81877 commented on code in PR #6061:
URL: https://github.com/apache/nifi/pull/6061#discussion_r877812454
##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/main/java/org/apache/nifi/properties/NiFiPropertiesLoader.java:
##########
@@ -211,6 +220,45 @@ public NiFiProperties get() {
return instance;
}
+ private void checkForDuplicates(File file) {
+ Map<String, String> properties = new HashMap<>();
+ Map<String, Set<String>> duplicateProperties = new HashMap<>();
+ Pattern keyPattern = Pattern.compile("([^!#=][^=]*)=(.*)");
+
+ if (file == null) {
+ throw new IllegalArgumentException("NiFi properties file missing or unreadable");
+ }
+ // Scan the properties file for duplicate keys
+ try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ Matcher matcher = keyPattern.matcher(line);
+
+ if (matcher.matches()) {
+ String key = matcher.group(1);
+ String value = matcher.group(2);
+
+ String existingValue = properties.put(key, value);
+
+ if (existingValue != null && !existingValue.equals(value)) {
+ Set<String> dupes = duplicateProperties.computeIfAbsent(key, k -> new HashSet<>(Collections.singleton(existingValue)));
+ dupes.add(value);
+ }
+ }
+ }
+ } catch (IOException ioe) {
+ throw new IllegalArgumentException("Cannot load properties file [" + file.getAbsolutePath() + "] due to "
+ + ioe.getLocalizedMessage(), ioe);
+ }
+
+ if (!duplicateProperties.isEmpty()) {
+ duplicateProperties.keySet().forEach(
+ k -> logger.error("Duplicate values found for key '{}': {}", k, duplicateProperties.get(k))
+ );
+ throw new IllegalArgumentException("Duplicate keys were detected in the properties file. See previous errors.");
Review Comment:
NiFI can be pretty chatty at startup. Do you think the error message aggregated and logged all at once so the user doesn't need to sift through logs to find the problem?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org