feat: 新增命令行配置、优先级最高.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.serliunx.ddns;
|
||||
|
||||
import com.serliunx.ddns.config.CommandLineConfiguration;
|
||||
import com.serliunx.ddns.config.Configuration;
|
||||
import com.serliunx.ddns.config.PropertiesConfiguration;
|
||||
import com.serliunx.ddns.constant.SystemConstants;
|
||||
@@ -8,6 +9,8 @@ import com.serliunx.ddns.core.context.MultipleSourceInstanceContext;
|
||||
import com.serliunx.ddns.support.SystemInitializer;
|
||||
import com.serliunx.ddns.support.okhttp.HttpClient;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
*
|
||||
@@ -33,7 +36,7 @@ public final class ManagerLite {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// 配置初始化
|
||||
initConfiguration();
|
||||
initConfiguration(args);
|
||||
|
||||
// 相关工具初始化
|
||||
initTools();
|
||||
@@ -55,8 +58,10 @@ public final class ManagerLite {
|
||||
/**
|
||||
* 配置初始化
|
||||
*/
|
||||
private static void initConfiguration() {
|
||||
configuration = new PropertiesConfiguration(SystemConstants.USER_SETTINGS_PROPERTIES_PATH);
|
||||
private static void initConfiguration(String[] args) {
|
||||
final CommandLineConfiguration cc = new CommandLineConfiguration(args);
|
||||
cc.from(new PropertiesConfiguration(SystemConstants.USER_SETTINGS_PROPERTIES_PATH));
|
||||
configuration = cc;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,7 +20,7 @@ public abstract class AbstractConfiguration implements Configuration {
|
||||
|
||||
protected final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
protected final Map<String, String> valueMap = new LinkedHashMap<>(16);
|
||||
private final Lock loadLock = new ReentrantLock();
|
||||
protected final Lock loadLock = new ReentrantLock();
|
||||
|
||||
public AbstractConfiguration() {}
|
||||
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
package com.serliunx.ddns.config;
|
||||
|
||||
import com.serliunx.ddns.core.Combination;
|
||||
import com.serliunx.ddns.support.Assert;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 从启动命令中读取的配置信息
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.3
|
||||
* @since 2024/12/24
|
||||
*/
|
||||
public final class CommandLineConfiguration extends AbstractConfiguration implements Combination<Configuration> {
|
||||
|
||||
/**
|
||||
* 原始参数
|
||||
*/
|
||||
private final String[] sourceArgs;
|
||||
/**
|
||||
* 待合并的其他配置信息
|
||||
*/
|
||||
private final Collection<Configuration> configurations;
|
||||
|
||||
/**
|
||||
* 配置项标记
|
||||
*/
|
||||
private static final String TAG = "-D";
|
||||
/**
|
||||
* 配置赋值符号(=)
|
||||
*/
|
||||
private static final String EQUAL = "=";
|
||||
/**
|
||||
* 配置缓存
|
||||
*/
|
||||
private final Map<String, String> cache = new HashMap<>();
|
||||
|
||||
public CommandLineConfiguration(String[] sourceArgs) {
|
||||
this(sourceArgs, new ArrayList<>());
|
||||
}
|
||||
|
||||
public CommandLineConfiguration(String[] sourceArgs, Collection<Configuration> configurations) {
|
||||
this.sourceArgs = sourceArgs;
|
||||
this.configurations = configurations;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取原始启动参数
|
||||
*
|
||||
* @return 原始启动参数
|
||||
*/
|
||||
public String[] getSourceArgs() {
|
||||
return sourceArgs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void from(Configuration configuration) {
|
||||
configurations.add(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void from(Collection<? extends Configuration> configurations) {
|
||||
this.configurations.addAll(configurations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Configuration getOriginal() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends Configuration> getCombinations() {
|
||||
return configurations;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refresh0() {
|
||||
if (sourceArgs == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (String arg : sourceArgs) {
|
||||
if (!arg.startsWith(TAG)) {
|
||||
continue;
|
||||
}
|
||||
String key = arg.substring(TAG.length(), arg.indexOf(EQUAL));
|
||||
String value = arg.substring(arg.indexOf(EQUAL) + 1);
|
||||
cache.put(key, value);
|
||||
}
|
||||
|
||||
// 载入配置
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void load0() {
|
||||
// 合并
|
||||
merge();
|
||||
// 更新
|
||||
valueMap.putAll(cache);
|
||||
// 清除缓存
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将其他配置信息合并到当前配置信息
|
||||
* <li> 命令行参数的配置信息优先级高于其他配置信息
|
||||
* <li> 仅在持有锁的情况下访问
|
||||
*/
|
||||
private void merge() {
|
||||
Assert.notEmpty(configurations);
|
||||
|
||||
for (Configuration configuration : configurations) {
|
||||
if (configuration instanceof AbstractConfiguration) {
|
||||
AbstractConfiguration ac = (AbstractConfiguration) configuration;
|
||||
ac.refresh0();
|
||||
}
|
||||
|
||||
final Map<String, String> keyValue = configuration.getAllKeyAndValue();
|
||||
keyValue.forEach((k, v) -> {
|
||||
if (!cache.containsKey(k)) {
|
||||
cache.put(k, v);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/main/java/com/serliunx/ddns/core/Combination.java
Normal file
43
src/main/java/com/serliunx/ddns/core/Combination.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package com.serliunx.ddns.core;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 组合, 将多个对象以一定的逻辑相组合
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.3
|
||||
* @since 2024/12/24
|
||||
*
|
||||
* @param <E> 合并的类型
|
||||
*/
|
||||
public interface Combination<E> {
|
||||
|
||||
/**
|
||||
* 组合指定对象
|
||||
*
|
||||
* @param e 对象
|
||||
*/
|
||||
void from(E e);
|
||||
|
||||
/**
|
||||
* 批量组合对象
|
||||
*
|
||||
* @param es 对象集合
|
||||
*/
|
||||
void from(Collection<? extends E> es);
|
||||
|
||||
/**
|
||||
* 获取原始对象(未组合前的)
|
||||
*
|
||||
* @return 原始对象
|
||||
*/
|
||||
E getOriginal();
|
||||
|
||||
/**
|
||||
* 获取该对象中所有组合的来源
|
||||
*
|
||||
* @return 所有组合来源
|
||||
*/
|
||||
Collection<? extends E> getCombinations();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.serliunx.ddns.test.config;
|
||||
|
||||
import com.serliunx.ddns.config.CommandLineConfiguration;
|
||||
import com.serliunx.ddns.config.PropertiesConfiguration;
|
||||
import com.serliunx.ddns.constant.SystemConstants;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 命令行配置读取测试
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @since 2024/12/24
|
||||
*/
|
||||
public class CmdConfigurationTest {
|
||||
|
||||
@Test
|
||||
public void testCmd() {
|
||||
CommandLineConfiguration configuration = new CommandLineConfiguration(new String[]{"-Dtest.env=1",
|
||||
"-Dsystem.cfg.log.onstart=false", "-Dapplication.name=jack"},
|
||||
Collections.singleton(new PropertiesConfiguration(SystemConstants.USER_SETTINGS_PROPERTIES_PATH)));
|
||||
configuration.refresh();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user