1 package de.dlr.shepard.util;
2
3 import java.io.IOException;
4 import java.util.Map;
5 import java.util.Properties;
6
7 import lombok.extern.slf4j.Slf4j;
8
9 @Slf4j
10 public class PropertiesHelper {
11 private static final String PROPERTIES = "db.properties";
12
13 private Properties propertiesFile = new Properties();
14 private Map<String, String> environment = System.getenv();
15
16 private void init() {
17 log.info("Reading properties file at {}", PROPERTIES);
18 try (var resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES);) {
19 propertiesFile.load(resourceAsStream);
20 } catch (IOException e) {
21 log.error("IOException while reading properties file");
22 }
23 }
24
25 private String asEnvironmentVariable(String variable) {
26 return variable.toUpperCase().replace(".", "_");
27 }
28
29 public String getProperty(String name) {
30
31 var envName = asEnvironmentVariable(name);
32 if (environment.containsKey(envName)) {
33 log.debug("Getting {} from environment", envName);
34 return environment.get(envName);
35 }
36
37
38 if (propertiesFile.isEmpty())
39 init();
40
41
42 String property = propertiesFile.getProperty(name);
43 if (property == null || property.isBlank()) {
44 log.warn("Could not find property {}", name);
45 return "";
46 }
47 log.debug("Getting {} from prop file", name);
48 return property;
49 }
50
51 }