Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions design_patterns/configuration_management/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
global:
db_url: "jdbc:mysql://localhost:3306/main"
jwt_secret: "default_secret"
vault_key: "global_vault_key"

services:
serviceA:
jwt_secret: "serviceA_secret"
serviceB:
db_url: "jdbc:postgresql://localhost:5432/serviceB"
47 changes: 47 additions & 0 deletions design_patterns/configuration_management/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.thealgorithms</groupId>
<artifactId>configuration-management-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>org.thealgorithms.config.ExampleUsage</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.thealgorithms.config;

import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.Map;

/**
* ConfigLoader reads configuration from a shared YAML file.
* It supports global and service-specific configurations.
*/
public class ConfigLoader {

private final Map<String, Object> config;

public ConfigLoader(String filename) {
Yaml yaml = new Yaml();
try (InputStream in = ConfigLoader.class.getClassLoader().getResourceAsStream(filename)) {
if (in == null) {
throw new IllegalArgumentException("Configuration file not found: " + filename);
}
config = yaml.load(in);
} catch (Exception e) {
throw new RuntimeException("Failed to load configuration: " + e.getMessage(), e);
}
}

/**
* Returns the value of a global config key.
*/
public Object getGlobal(String key) {
Map<String, Object> global = (Map<String, Object>) config.get("global");
return global != null ? global.get(key) : null;
}

/**
* Returns a value for a specific service override.
*/
public Object getServiceOverride(String service, String key) {
Map<String, Map<String, Object>> services =
(Map<String, Map<String, Object>>) config.get("services");
if (services == null || !services.containsKey(service)) {
return null;
}
return services.get(service).getOrDefault(key, getGlobal(key));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.thealgorithms.config;

/**
* Demonstrates how to use ConfigLoader with a shared YAML file.
*/
public class ExampleUsage {
public static void main(String[] args) {
ConfigLoader loader = new ConfigLoader("config.yaml");

System.out.println("Global DB URL: " + loader.getGlobal("db_url"));
System.out.println("Service A JWT Secret: " + loader.getServiceOverride("serviceA", "jwt_secret"));
System.out.println("Service B DB URL: " + loader.getServiceOverride("serviceB", "db_url"));
}
}
Loading