Skip to content

Commit

Permalink
NIFI-3364 Sorted Bootstrap java.arg properties in ascending alphabeti…
Browse files Browse the repository at this point in the history
…cal order (apache#9490)

Signed-off-by: David Handermann <exceptionfactory@apache.org>
  • Loading branch information
jrsteinebrey authored Nov 7, 2024
1 parent 11cb2c5 commit 6095d95
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +26,6 @@
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;
Expand Down Expand Up @@ -64,23 +65,18 @@ public StandardConfigurationProvider(final Map<String, String> environmentVariab

/**
* 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -117,6 +118,34 @@ void testGetManagementServerAddress(@TempDir final Path applicationHomeDirectory
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);
Expand Down

0 comments on commit 6095d95

Please sign in to comment.