feat: 新增yaml配置文件的支持.
This commit is contained in:
@@ -99,11 +99,16 @@ public abstract class AbstractConfiguration implements Configuration {
|
|||||||
public void refresh() {
|
public void refresh() {
|
||||||
// 刷新配置信息
|
// 刷新配置信息
|
||||||
refresh0();
|
refresh0();
|
||||||
final Boolean needPrint = getBoolean(ConfigurationKeys.KEY_CFG_LOG_ONSTART);
|
final Boolean needPrint = getBoolean(ConfigurationKeys.KEY_CFG_LOG_ONSTART, false);
|
||||||
if (needPrint)
|
if (needPrint)
|
||||||
printDetails();
|
printDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String> getAllKeyAndValue() {
|
||||||
|
return valueMap;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getPriority() {
|
public int getPriority() {
|
||||||
return Integer.MAX_VALUE;
|
return Integer.MAX_VALUE;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.serliunx.ddns.config;
|
|||||||
import com.serliunx.ddns.core.Priority;
|
import com.serliunx.ddns.core.Priority;
|
||||||
import com.serliunx.ddns.core.Refreshable;
|
import com.serliunx.ddns.core.Refreshable;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置信息逻辑定义
|
* 配置信息逻辑定义
|
||||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
@@ -91,4 +93,10 @@ public interface Configuration extends Refreshable, Priority {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
<T extends Enum> T getEnum(Class<T> clazz, String key, T defaultValue);
|
<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) {
|
public FileConfiguration(String path) {
|
||||||
this.path = 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);
|
InstanceContextHolder.setInstance(this);
|
||||||
// 调用子类的初始化逻辑
|
// 调用子类的初始化逻辑
|
||||||
init();
|
init();
|
||||||
InstanceContextHolder.clear();
|
InstanceContextHolder.clearInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -99,7 +99,7 @@ public abstract class AbstractInstance implements Instance {
|
|||||||
} finally {
|
} finally {
|
||||||
this.value = null;
|
this.value = null;
|
||||||
// 移除实例信息
|
// 移除实例信息
|
||||||
InstanceContextHolder.clear();
|
InstanceContextHolder.clearInstance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ public abstract class AbstractInstance implements Instance {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
boolean result = validate0();
|
boolean result = validate0();
|
||||||
InstanceContextHolder.clear();
|
InstanceContextHolder.clearInstance();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import com.serliunx.ddns.core.instance.Instance;
|
|||||||
import com.serliunx.ddns.support.InstanceContextHolder;
|
import com.serliunx.ddns.support.InstanceContextHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前任何线程的实例信息
|
* 获取当前任何线程的实例信息或者附加信息
|
||||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
* @version 1.0.0
|
* @version 1.0.0
|
||||||
* @since 2024/6/15
|
* @since 2024/6/15
|
||||||
@@ -16,8 +16,13 @@ public class InstanceNameConverter extends ClassicConverter {
|
|||||||
@Override
|
@Override
|
||||||
public String convert(ILoggingEvent event) {
|
public String convert(ILoggingEvent event) {
|
||||||
Instance instance = InstanceContextHolder.getInstance();
|
Instance instance = InstanceContextHolder.getInstance();
|
||||||
if (instance != null)
|
String content;
|
||||||
return instance.getName();
|
if (instance != null) {
|
||||||
return "----";
|
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">
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
<encoder>
|
<encoder>
|
||||||
<pattern>
|
<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>
|
</pattern>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
|||||||
Reference in New Issue
Block a user