Skip to content

Commit

Permalink
Polish Kafka config producer
Browse files Browse the repository at this point in the history
* Remove lambdas
* Use proper CDI scopes
* Use CDI injection instead of Config lookup
  • Loading branch information
geoand committed Mar 12, 2021
1 parent 6d7c3f8 commit a02ad54
Showing 1 changed file with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,37 @@

import java.util.HashMap;
import java.util.Map;
import java.util.stream.StreamSupport;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.inject.Singleton;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.arc.DefaultBean;

@Dependent
@Singleton
public class KafkaRuntimeConfigProducer {

private String configPrefix = "kafka";
private static final String CONFIG_PREFIX = "kafka";

@Produces
@DefaultBean
@ApplicationScoped
@Singleton
@Named("default-kafka-broker")
public Map<String, Object> createKafkaRuntimeConfig() {
Map<String, Object> properties = new HashMap<>();
final Config config = ConfigProvider.getConfig();

StreamSupport
.stream(config.getPropertyNames().spliterator(), false)
.map(String::toLowerCase)
.filter(name -> name.startsWith(configPrefix))
.distinct()
.sorted()
.forEach(name -> {
final String key = name.substring(configPrefix.length() + 1).toLowerCase().replaceAll("[^a-z0-9.]", ".");
final String value = config.getOptionalValue(name, String.class).orElse("");
properties.put(key, value);
});

return properties;
public Map<String, Object> createKafkaRuntimeConfig(Config config) {
Map<String, Object> result = new HashMap<>();
for (String propertyName : config.getPropertyNames()) {
String propertyNameLowerCase = propertyName.toLowerCase();
if (!propertyNameLowerCase.startsWith(CONFIG_PREFIX)) {
continue;
}
String effectivePropertyName = propertyNameLowerCase.substring(CONFIG_PREFIX.length() + 1).toLowerCase()
.replaceAll("[^a-z0-9.]", ".");
String value = config.getOptionalValue(propertyName, String.class).orElse("");
result.put(effectivePropertyName, value);
}
return result;
}

}

0 comments on commit a02ad54

Please sign in to comment.