Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -49,26 +50,34 @@ public class SystemPropertySupplier<T> implements Supplier<T> {
private final String propName;
private final T defaultValue;
private final Function<String, T> parser;

private Logger log = LOG;
private String successLogLevel = "INFO";
private boolean hideValue = false;
private Predicate<T> validator = (a) -> true;
private Predicate<T> validator = a -> true;
private Function<String, String> sysPropReader = System::getProperty;
private BiFunction<String, T, String> setMessageFormatter = (a, b) -> String.format("System property %s found to be '%s'", a,
hideValue ? HIDDEN_REPLACEMENT : b);

private SystemPropertySupplier(@NotNull String propName, @NotNull T defaultValue) {
private SystemPropertySupplier(@NotNull String propName, @Nullable T defaultValue, @Nullable Class<T> clazz) {
this.propName = Objects.requireNonNull(propName, "propertyName must be non-null");
this.defaultValue = Objects.requireNonNull(defaultValue, "defaultValue must be non-null");
this.parser = getValueParser(defaultValue);
this.defaultValue = defaultValue;
this.parser = getValueParser(defaultValue, clazz);
}

/**
* Create it for a given property name and default value.
*/
public static <U> SystemPropertySupplier<U> create(@NotNull String propName, @NotNull U defaultValue) {
return new SystemPropertySupplier<>(propName, defaultValue);
return new SystemPropertySupplier<>(propName,
Objects.requireNonNull(defaultValue, "defaultValue must not be null"), null);
}

/**
* Create it for a given property name and no default value (but expected {@linkplain Class}).
*/
public static <U> SystemPropertySupplier<U> create(@NotNull String propName, @NotNull Class<U> clazz) {
return new SystemPropertySupplier<>(propName, null,
Objects.requireNonNull(clazz, "clazz must not be null"));
}

/**
Expand Down Expand Up @@ -163,7 +172,7 @@ public T get() {
log.error("Ignoring malformed value '{}' for system property {}", displayedValue, propName);
}

if (!returnValue.equals(defaultValue)) {
if (!Objects.equals(returnValue, defaultValue)) {
String msg = setMessageFormatter.apply(propName, returnValue);
switch (successLogLevel) {
case "INFO":
Expand Down Expand Up @@ -191,8 +200,12 @@ public T get() {
}

@SuppressWarnings("unchecked")
private static <T> Function<String, T> getValueParser(T defaultValue) {
if (defaultValue instanceof Boolean) {
private static <T> Function<String, T> getValueParser(T defaultValue, Class<T> type) {
Class<?> clazz = (defaultValue != null) ? defaultValue.getClass() : type;

if (clazz == null) {
throw new IllegalArgumentException("Cannot determine type: defaultValue is null and no type provided.");
} else if (Boolean.class.isAssignableFrom(clazz)) {
return v -> (T) Boolean.valueOf(v);
} else if (defaultValue instanceof Integer) {
return v -> (T) Integer.valueOf(v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* <em>For Oak internal use only. Do not use outside Oak components.</em>
*/
@Internal(since = "1.1.0")
@Version("2.1.0")
@Version("2.2.0")
package org.apache.jackrabbit.oak.commons.properties;

import org.apache.jackrabbit.oak.commons.annotations.Internal;
Expand Down
35 changes: 35 additions & 0 deletions ...rc/test/java/org/apache/jackrabbit/oak/commons/properties/SystemPropertySupplierTest.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.jackrabbit.oak.commons.properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.apache.jackrabbit.oak.commons.junit.LogCustomizer;
Expand Down Expand Up @@ -147,4 +148,38 @@ public void testUnsupportedLogLevel() {
logSuccessAs("AWESOME").get();
assertEquals(2 , x);
}

@Test
public void testCheckNullDefault() {
try {
assertNull(SystemPropertySupplier.create("foo", Boolean.class).
usingSystemPropertyReader((n) -> null).get());
} catch (IllegalArgumentException expected) {
Comment thread
reschke marked this conversation as resolved.
}
}

@Test
public void testCheckNoDefaultNotSet() {
assertNull(SystemPropertySupplier.create("foo", Boolean.class).
usingSystemPropertyReader((n) -> null).get());
}

@Test
public void testCheckNoDefaultSet() {
int value = SystemPropertySupplier.create("foo", Integer.class).
usingSystemPropertyReader((n) -> "4217").get();
assertEquals(4217, value);
}

@Test
public void testCheckNoDefaultSetInvalid() {
assertNull(SystemPropertySupplier.create("foo", Integer.class).
usingSystemPropertyReader((n) -> "abcd").get());
}

@Test
public void testCheckNoDefaultValidatorRejects() {
assertNull(SystemPropertySupplier.create("foo", String.class).
usingSystemPropertyReader((n) -> "abcd").validateWith(x-> false) .get());
}
}
Loading