|
|
|
|
@ -1,14 +1,12 @@
|
|
|
|
|
package egovframework.com.cmm.service;
|
|
|
|
|
package com.xit.core.config.support;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import org.yaml.snakeyaml.*;
|
|
|
|
|
|
|
|
|
|
import egovframework.com.cmm.service.*;
|
|
|
|
|
import lombok.extern.slf4j.*;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class EgovYmlProperties {
|
|
|
|
|
@ -28,8 +26,8 @@ public class EgovYmlProperties {
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public static String getProperty(String keyName) {
|
|
|
|
|
try (InputStream baseInputStream = EgovProperties.class.getClassLoader().getResourceAsStream(GLOBALS_YAML_FILE);
|
|
|
|
|
InputStream profileInputStream = EgovProperties.class.getClassLoader().getResourceAsStream(getProfileYamlFile())) {
|
|
|
|
|
try (InputStream baseInputStream = EgovYmlProperties.class.getClassLoader().getResourceAsStream(GLOBALS_YAML_FILE);
|
|
|
|
|
InputStream profileInputStream = EgovYmlProperties.class.getClassLoader().getResourceAsStream(getProfileYamlFile())) {
|
|
|
|
|
|
|
|
|
|
Yaml yaml = new Yaml();
|
|
|
|
|
|
|
|
|
|
@ -39,7 +37,7 @@ public class EgovYmlProperties {
|
|
|
|
|
// 프로파일별 YAML 읽기 (override)
|
|
|
|
|
if (profileInputStream != null) {
|
|
|
|
|
Map<String, Object> profileMap = yaml.load(profileInputStream);
|
|
|
|
|
yamlMap.putAll(profileMap);
|
|
|
|
|
yamlMap = mergeYamlMaps(yamlMap, profileMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getValueFromYamlMap(yamlMap, keyName);
|
|
|
|
|
@ -76,18 +74,18 @@ public class EgovYmlProperties {
|
|
|
|
|
public static List<Map<String, String>> loadYamlFile() {
|
|
|
|
|
List<Map<String, String>> keyList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
try (InputStream baseInputStream = EgovProperties.class.getClassLoader().getResourceAsStream(GLOBALS_YAML_FILE);
|
|
|
|
|
InputStream profileInputStream = EgovProperties.class.getClassLoader().getResourceAsStream(getProfileYamlFile())) {
|
|
|
|
|
try (InputStream baseInputStream = EgovYmlProperties.class.getClassLoader().getResourceAsStream(GLOBALS_YAML_FILE);
|
|
|
|
|
InputStream profileInputStream = EgovYmlProperties.class.getClassLoader().getResourceAsStream(getProfileYamlFile())) {
|
|
|
|
|
|
|
|
|
|
Yaml yaml = new Yaml();
|
|
|
|
|
|
|
|
|
|
// 기본 YAML 읽기
|
|
|
|
|
Map<String, Object> yamlMap = baseInputStream != null ? yaml.load(baseInputStream) : new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 프로파일별 YAML 읽기 (override)
|
|
|
|
|
if (profileInputStream != null) {
|
|
|
|
|
Map<String, Object> profileMap = yaml.load(profileInputStream);
|
|
|
|
|
yamlMap.putAll(profileMap);
|
|
|
|
|
yamlMap = mergeYamlMaps(yamlMap, profileMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flattenYamlMap(yamlMap, "", keyList);
|
|
|
|
|
@ -98,6 +96,33 @@ public class EgovYmlProperties {
|
|
|
|
|
return keyList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 두 개의 YAML 맵을 병합한다. 중첩된 구조를 올바르게 처리하며, profileMap의 값으로 override한다.
|
|
|
|
|
* @param baseMap 기본 YAML 맵
|
|
|
|
|
* @param profileMap 프로파일 YAML 맵
|
|
|
|
|
* @return 병합된 맵
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private static Map<String, Object> mergeYamlMaps(Map<String, Object> baseMap, Map<String, Object> profileMap) {
|
|
|
|
|
if (baseMap == null) return profileMap;
|
|
|
|
|
if (profileMap == null) return baseMap;
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, Object> entry : profileMap.entrySet()) {
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
|
Object profileValue = entry.getValue();
|
|
|
|
|
|
|
|
|
|
if (baseMap.containsKey(key) && baseMap.get(key) instanceof Map && profileValue instanceof Map) {
|
|
|
|
|
// 중첩된 Map 병합
|
|
|
|
|
baseMap.put(key, mergeYamlMaps((Map<String, Object>) baseMap.get(key), (Map<String, Object>) profileValue));
|
|
|
|
|
} else {
|
|
|
|
|
// 기본값 덮어쓰기
|
|
|
|
|
baseMap.put(key, profileValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 중첩된 YAML 구조를 평탄화하여 key-value 리스트에 추가한다.
|
|
|
|
|
* @param yamlMap Map<String, Object>
|