From d709577991110bdd3ce3f839bbb06a4225de351a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Thu, 14 Dec 2023 00:25:24 +0100 Subject: [PATCH] added support for app config --- .../io/github/mmm/base/metainfo/MetaInfo.java | 8 +++ .../base/metainfo/impl/AppConfigHolder.java | 49 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 metainfo/src/main/java/io/github/mmm/base/metainfo/impl/AppConfigHolder.java diff --git a/metainfo/src/main/java/io/github/mmm/base/metainfo/MetaInfo.java b/metainfo/src/main/java/io/github/mmm/base/metainfo/MetaInfo.java index d6947c0..3b2c81f 100644 --- a/metainfo/src/main/java/io/github/mmm/base/metainfo/MetaInfo.java +++ b/metainfo/src/main/java/io/github/mmm/base/metainfo/MetaInfo.java @@ -309,4 +309,12 @@ static MetaInfo empty() { return MetaInfoEmpty.INSTANCE; } + + /** + * @return the {@link MetaInfo} with the configuration for the running application. + */ + static MetaInfo config() { + + return io.github.mmm.base.metainfo.impl.AppConfigHolder.CONFIG; + } } diff --git a/metainfo/src/main/java/io/github/mmm/base/metainfo/impl/AppConfigHolder.java b/metainfo/src/main/java/io/github/mmm/base/metainfo/impl/AppConfigHolder.java new file mode 100644 index 0000000..3e705d0 --- /dev/null +++ b/metainfo/src/main/java/io/github/mmm/base/metainfo/impl/AppConfigHolder.java @@ -0,0 +1,49 @@ +package io.github.mmm.base.metainfo.impl; + +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import io.github.mmm.base.exception.RuntimeIoException; +import io.github.mmm.base.metainfo.MetaInfo; + +/** + * Holder for the {@link MetaInfo#config() app config}. + */ +public class AppConfigHolder { + + /** @see MetaInfo#config() */ + public static final MetaInfo CONFIG; + + static { + MetaInfo metaInfo = MetaInfo.empty(); + metaInfo = metaInfo.with(System.getenv()); + metaInfo = withApplicationProperties(metaInfo); + metaInfo = metaInfo.with(System.getProperties()); + CONFIG = metaInfo; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private static MetaInfo withApplicationProperties(MetaInfo metaInfo) { + + Path configPath = Paths.get("./config/application.properties"); + if (!Files.exists(configPath)) { + return metaInfo; + } + Properties properties = new Properties(); + try (BufferedReader reader = Files.newBufferedReader(configPath, StandardCharsets.UTF_8)) { + properties.load(reader); + } catch (IOException e) { + throw new RuntimeIoException(e); + } + Map map = new HashMap<>((Map) properties); + return metaInfo.with(map); + } + +}