feat: 新增yaml配置文件的支持.
This commit is contained in:
@@ -99,11 +99,16 @@ public abstract class AbstractConfiguration implements Configuration {
|
||||
public void refresh() {
|
||||
// 刷新配置信息
|
||||
refresh0();
|
||||
final Boolean needPrint = getBoolean(ConfigurationKeys.KEY_CFG_LOG_ONSTART);
|
||||
final Boolean needPrint = getBoolean(ConfigurationKeys.KEY_CFG_LOG_ONSTART, false);
|
||||
if (needPrint)
|
||||
printDetails();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getAllKeyAndValue() {
|
||||
return valueMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return Integer.MAX_VALUE;
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.serliunx.ddns.config;
|
||||
import com.serliunx.ddns.core.Priority;
|
||||
import com.serliunx.ddns.core.Refreshable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 配置信息逻辑定义
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
@@ -91,4 +93,10 @@ public interface Configuration extends Refreshable, Priority {
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
<T extends Enum> T getEnum(Class<T> clazz, String key, T defaultValue);
|
||||
|
||||
/**
|
||||
* 获取所有的键值对
|
||||
* @return 配置文件所有成功加载的键值对
|
||||
*/
|
||||
Map<String, String> getAllKeyAndValue();
|
||||
}
|
||||
|
||||
@@ -14,4 +14,8 @@ public abstract class FileConfiguration extends AbstractConfiguration {
|
||||
public FileConfiguration(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.serliunx.ddns.config;
|
||||
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* yml/yaml格式的配置文件,目前用于语言文件
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/6/17
|
||||
*/
|
||||
public class YamlConfiguration extends FileConfiguration {
|
||||
|
||||
public YamlConfiguration(String path, boolean refresh) {
|
||||
super(path);
|
||||
if (refresh)
|
||||
refresh();
|
||||
}
|
||||
|
||||
public YamlConfiguration(String path) {
|
||||
this(path, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refresh0() {
|
||||
try (InputStream inputStream = Files.newInputStream(Paths.get(path))) {
|
||||
Yaml yaml = new Yaml();
|
||||
Map<String, String> keyAndValue = yaml.load(inputStream);
|
||||
if (keyAndValue != null)
|
||||
valueMap.putAll(keyAndValue);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void load0() {
|
||||
// do nothing.
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,7 @@ public abstract class AbstractInstance implements Instance {
|
||||
InstanceContextHolder.setInstance(this);
|
||||
// 调用子类的初始化逻辑
|
||||
init();
|
||||
InstanceContextHolder.clear();
|
||||
InstanceContextHolder.clearInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -99,7 +99,7 @@ public abstract class AbstractInstance implements Instance {
|
||||
} finally {
|
||||
this.value = null;
|
||||
// 移除实例信息
|
||||
InstanceContextHolder.clear();
|
||||
InstanceContextHolder.clearInstance();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ public abstract class AbstractInstance implements Instance {
|
||||
return false;
|
||||
}
|
||||
boolean result = validate0();
|
||||
InstanceContextHolder.clear();
|
||||
InstanceContextHolder.clearInstance();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import com.serliunx.ddns.core.instance.Instance;
|
||||
import com.serliunx.ddns.support.InstanceContextHolder;
|
||||
|
||||
/**
|
||||
* 获取当前任何线程的实例信息
|
||||
* 获取当前任何线程的实例信息或者附加信息
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/6/15
|
||||
@@ -16,8 +16,13 @@ public class InstanceNameConverter extends ClassicConverter {
|
||||
@Override
|
||||
public String convert(ILoggingEvent event) {
|
||||
Instance instance = InstanceContextHolder.getInstance();
|
||||
if (instance != null)
|
||||
return instance.getName();
|
||||
return "----";
|
||||
String content;
|
||||
if (instance != null) {
|
||||
content = instance.getName();
|
||||
} else {
|
||||
content = InstanceContextHolder.getAdditional();
|
||||
return content == null ? "----" : content;
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>
|
||||
%boldGreen(%d{yyyy-MM-dd HH:mm:ss(SSS)}) %cyan([%pid]) %magenta([%15.15thread]) %yellow([%16.16instance]) %highlight([%-6level]) %boldYellow(%-36logger{32}): %msg%n
|
||||
%boldGreen(%d{yyyy-MM-dd HH:mm:ss(SSS)}) %cyan([%pid]) %magenta([%15.15thread]) %green([%16.16instance]) %highlight([%-6level]) %boldCyan(%-36logger{32}): %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
Reference in New Issue
Block a user