Skip to content

Commit e8c41ac

Browse files
Add centralized configuration management module using YAML
1 parent bb6385e commit e8c41ac

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
global:
2+
db_url: "jdbc:mysql://localhost:3306/main"
3+
jwt_secret: "default_secret"
4+
vault_key: "global_vault_key"
5+
6+
services:
7+
serviceA:
8+
jwt_secret: "serviceA_secret"
9+
serviceB:
10+
db_url: "jdbc:postgresql://localhost:5432/serviceB"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.thealgorithms</groupId>
9+
<artifactId>configuration-management-example</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
<packaging>jar</packaging>
12+
13+
<properties>
14+
<maven.compiler.release>21</maven.compiler.release>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.yaml</groupId>
21+
<artifactId>snakeyaml</artifactId>
22+
<version>2.1</version>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<version>3.14.1</version>
32+
<configuration>
33+
<release>${maven.compiler.release}</release>
34+
</configuration>
35+
</plugin>
36+
37+
<plugin>
38+
<groupId>org.codehaus.mojo</groupId>
39+
<artifactId>exec-maven-plugin</artifactId>
40+
<version>3.1.0</version>
41+
<configuration>
42+
<mainClass>org.thealgorithms.config.ExampleUsage</mainClass>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.thealgorithms.config;
2+
3+
import org.yaml.snakeyaml.Yaml;
4+
import java.io.InputStream;
5+
import java.util.Map;
6+
7+
/**
8+
* ConfigLoader reads configuration from a shared YAML file.
9+
* It supports global and service-specific configurations.
10+
*/
11+
public class ConfigLoader {
12+
13+
private final Map<String, Object> config;
14+
15+
public ConfigLoader(String filename) {
16+
Yaml yaml = new Yaml();
17+
try (InputStream in = ConfigLoader.class.getClassLoader().getResourceAsStream(filename)) {
18+
if (in == null) {
19+
throw new IllegalArgumentException("Configuration file not found: " + filename);
20+
}
21+
config = yaml.load(in);
22+
} catch (Exception e) {
23+
throw new RuntimeException("Failed to load configuration: " + e.getMessage(), e);
24+
}
25+
}
26+
27+
/**
28+
* Returns the value of a global config key.
29+
*/
30+
public Object getGlobal(String key) {
31+
Map<String, Object> global = (Map<String, Object>) config.get("global");
32+
return global != null ? global.get(key) : null;
33+
}
34+
35+
/**
36+
* Returns a value for a specific service override.
37+
*/
38+
public Object getServiceOverride(String service, String key) {
39+
Map<String, Map<String, Object>> services =
40+
(Map<String, Map<String, Object>>) config.get("services");
41+
if (services == null || !services.containsKey(service)) {
42+
return null;
43+
}
44+
return services.get(service).getOrDefault(key, getGlobal(key));
45+
}
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.thealgorithms.config;
2+
3+
/**
4+
* Demonstrates how to use ConfigLoader with a shared YAML file.
5+
*/
6+
public class ExampleUsage {
7+
public static void main(String[] args) {
8+
ConfigLoader loader = new ConfigLoader("config.yaml");
9+
10+
System.out.println("Global DB URL: " + loader.getGlobal("db_url"));
11+
System.out.println("Service A JWT Secret: " + loader.getServiceOverride("serviceA", "jwt_secret"));
12+
System.out.println("Service B DB URL: " + loader.getServiceOverride("serviceB", "db_url"));
13+
}
14+
}

0 commit comments

Comments
 (0)