in winegrower-core/src/main/java/org/apache/winegrower/Ripener.java [416:488]
public void loadConfiguration(final Properties embedConfig) {
final Map<Object, Method> setters = Stream.of(this.configuration.getClass().getMethods())
.filter(it -> it.getName().startsWith("set") && it.getParameterCount() == 1)
.collect(toMap(it ->
(Character.toLowerCase(it.getName().charAt(3)) + it.getName().substring(4)).toLowerCase(ROOT),
identity()));
final Collection<String> matched = new ArrayList<>();
embedConfig.stringPropertyNames().stream().filter(it -> setters.containsKey(it.toLowerCase(ROOT))).forEach(key -> {
final String value = embedConfig.getProperty(key);
if (value == null) {
return;
}
final String keyLowerCase = key.toLowerCase(ROOT);
final Method setter = setters.get(keyLowerCase);
matched.add(keyLowerCase);
final Class<?> type = setter.getParameters()[0].getType();
try {
if (type == String.class) {
setter.invoke(this.configuration, value);
return;
} else if (type == File.class) {
setter.invoke(this.configuration, new File(value));
return;
}
// from here all parameters are lists
final Collection<String> asList = Stream.of(value.split(","))
.map(String::trim)
.filter(it -> !it.isEmpty())
.collect(toList());
if (type == Predicate.class) { // Predicate<String> + startsWith logic
final Predicate<String> predicate =
val -> val != null && asList.stream().anyMatch(val::startsWith);
setter.invoke(this.configuration, predicate);
} else if (type == List.class) {
setter.invoke(this.configuration, asList);
} else if (type == Collection.class
&& ManifestContributor.class == ParameterizedType.class.cast(
setter.getParameters()[0].getParameterizedType()).getActualTypeArguments()[0]) {
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
setter.invoke(this.configuration, asList.stream()
.map(it -> {
try {
return loader.loadClass(it);
} catch (final ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
})
.collect(toList()));
} else if (type == Collection.class) { // Collection<String>
setter.invoke(this.configuration, asList);
} else {
throw new IllegalArgumentException("Unsupported: " + setter);
}
} catch (final IllegalAccessException e) {
throw new IllegalArgumentException(e);
} catch (final InvocationTargetException e) {
throw new IllegalArgumentException(e.getTargetException());
}
});
if (DefaultConfigurationAdmin.class.isInstance(configurationAdmin)) {
final DefaultConfigurationAdmin dca = DefaultConfigurationAdmin.class.cast(configurationAdmin);
embedConfig.stringPropertyNames()
.stream()
.filter(it -> it.startsWith("winegrower.service."))
.peek(matched::add)
.forEach(key -> dca.getProvidedConfiguration().put(key, embedConfig.getProperty(key)));
}
embedConfig.stringPropertyNames().stream().filter(it -> !matched.contains(it.toLowerCase(ROOT)))
.forEach(it -> LOGGER.warn("Didn't match configuration {}, did you mispell it?", it));
}