Skip to content

Commit

Permalink
Add support for loading kafka properties from classpath (#637)
Browse files Browse the repository at this point in the history
* Add support for loading kafka properties from classpath

* Silently ignore missing but configured property file

* Refactor code

* Improve variable name

---------

Co-authored-by: Alvin Kwekel <[email protected]>
  • Loading branch information
alvinkwekel and Alvin Kwekel authored Jun 11, 2024
1 parent d52cd1e commit 4594fa0
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/main/java/kafdrop/config/KafkaConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package kafdrop.config;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.common.config.SaslConfigs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;


@Component
Expand Down Expand Up @@ -48,12 +52,16 @@ public void applyCommon(Properties properties) {
}

LOG.info("Checking properties file {}", propertiesFile);
final var propertiesFile = new File(this.propertiesFile);
if (propertiesFile.isFile()) {
LOG.info("Loading properties from {}", this.propertiesFile);
Optional<AbstractResource> propertiesResource = StringUtils.isBlank(propertiesFile) ? Optional.empty() :
Stream.of(new FileSystemResource(propertiesFile),
new ClassPathResource(propertiesFile))
.filter(Resource::isReadable)
.findFirst();
if (propertiesResource.isPresent()) {
LOG.info("Loading properties from {}", propertiesFile);
final var propertyOverrides = new Properties();
try (var propsReader = new BufferedReader(new FileReader(propertiesFile))) {
propertyOverrides.load(propsReader);
try {
propertyOverrides.load(propertiesResource.get().getInputStream());
} catch (IOException e) {
throw new KafkaConfigurationException(e);
}
Expand Down

0 comments on commit 4594fa0

Please sign in to comment.