Skip to content

Commit

Permalink
added support for app config
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Dec 13, 2023
1 parent 002b11f commit d709577
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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);
}

}

0 comments on commit d709577

Please sign in to comment.