NIFI-3364 Sorted Bootstrap java.arg properties in ascending alphabetical order (#9490)

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
jrsteinebrey 2024-11-07 09:40:40 -05:00 committed by GitHub
parent 11cb2c56e2
commit 6095d950cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 13 deletions

View File

@ -16,6 +16,8 @@
*/
package org.apache.nifi.bootstrap.configuration;
import static java.util.function.Predicate.not;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
@ -24,7 +26,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -64,23 +65,18 @@ public class StandardConfigurationProvider implements ConfigurationProvider {
/**
* Get additional arguments for application command from Bootstrap Properties starting with java.arg
* Return the list sorted by java.arg names in ascending alphabetical order
*
* @return Additional arguments
*/
@Override
public List<String> getAdditionalArguments() {
final List<String> additionalArguments = new ArrayList<>();
for (final String propertyName : bootstrapProperties.stringPropertyNames()) {
if (propertyName.startsWith(BootstrapProperty.JAVA_ARGUMENT.getProperty())) {
final String additionalArgument = bootstrapProperties.getProperty(propertyName);
if (!additionalArgument.isBlank()) {
additionalArguments.add(additionalArgument);
}
}
}
return additionalArguments;
return bootstrapProperties.stringPropertyNames().stream()
.filter(name -> name.startsWith(BootstrapProperty.JAVA_ARGUMENT.getProperty()))
.sorted()
.map(bootstrapProperties::getProperty)
.filter(not(String::isBlank))
.toList();
}
/**

View File

@ -27,6 +27,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
@ -117,6 +118,34 @@ class StandardConfigurationProviderTest {
assertEquals(MANAGEMENT_SERVER_ADDRESS, address.toString());
}
@Test
void testGetAdditionalArguments(@TempDir final Path applicationHomeDirectory) throws IOException {
final Path bootstrapConfiguration = setRequiredConfiguration(applicationHomeDirectory);
// Properties in random order and containing some java.arg and some non-java.arg names.
List<String> propertyNames = List.of("java.arg9", "java.arg2", "java.arg.my2", "non.java.arg.2",
"java.arg1", "java.arg.memory", "java.arg", "java.arg.my1", "non.java.arg.3", "random.nothing");
// The expected returned list of java.arg properties sorted in ascending alphabetical order.
List<String> expectedArguments = List.of("java.arg", "java.arg.memory", "java.arg.my1", "java.arg.my2",
"java.arg1", "java.arg2", "java.arg9");
final Properties bootstrapProperties = new Properties();
for (String propertyName : propertyNames) {
bootstrapProperties.put(propertyName, propertyName);
}
try (OutputStream outputStream = Files.newOutputStream(bootstrapConfiguration)) {
bootstrapProperties.store(outputStream, Properties.class.getSimpleName());
}
final StandardConfigurationProvider provider = new StandardConfigurationProvider(environmentVariables, systemProperties);
final List<String> actualAdditionalArguments = provider.getAdditionalArguments();
assertEquals(expectedArguments.size(), actualAdditionalArguments.size());
for (int i = 0; i < expectedArguments.size(); i++) {
assertEquals(expectedArguments.get(i), actualAdditionalArguments.get(i));
}
}
private Path setRequiredConfiguration(final Path applicationHomeDirectory) throws IOException {
environmentVariables.put(EnvironmentVariable.NIFI_HOME.name(), applicationHomeDirectory.toString());
final Path configurationDirectory = createConfigurationDirectory(applicationHomeDirectory);