-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
metainfo/src/main/java/io/github/mmm/base/metainfo/impl/AppConfigHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> map = new HashMap<>((Map) properties); | ||
return metaInfo.with(map); | ||
} | ||
|
||
} |