From a0b0764ac3b0ac72bb27b44f2637ea855c9faa1f Mon Sep 17 00:00:00 2001 From: SerLiunx-ctrl <17689543@qq.com> Date: Wed, 25 Dec 2024 14:28:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E9=85=8D=E7=BD=AE=E3=80=81=E4=BC=98=E5=85=88=E7=BA=A7?= =?UTF-8?q?=E6=9C=80=E9=AB=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/serliunx/ddns/ManagerLite.java | 11 +- .../ddns/config/AbstractConfiguration.java | 2 +- .../ddns/config/CommandLineConfiguration.java | 133 ++++++++++++++++++ .../com/serliunx/ddns/core/Combination.java | 43 ++++++ .../test/config/CmdConfigurationTest.java | 25 ++++ 5 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/serliunx/ddns/config/CommandLineConfiguration.java create mode 100644 src/main/java/com/serliunx/ddns/core/Combination.java create mode 100644 src/test/java/com/serliunx/ddns/test/config/CmdConfigurationTest.java diff --git a/src/main/java/com/serliunx/ddns/ManagerLite.java b/src/main/java/com/serliunx/ddns/ManagerLite.java index 6131a37..7f03955 100644 --- a/src/main/java/com/serliunx/ddns/ManagerLite.java +++ b/src/main/java/com/serliunx/ddns/ManagerLite.java @@ -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; } /** diff --git a/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java b/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java index 1f57cd6..16f3f02 100644 --- a/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java +++ b/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java @@ -20,7 +20,7 @@ public abstract class AbstractConfiguration implements Configuration { protected final Logger log = LoggerFactory.getLogger(this.getClass()); protected final Map valueMap = new LinkedHashMap<>(16); - private final Lock loadLock = new ReentrantLock(); + protected final Lock loadLock = new ReentrantLock(); public AbstractConfiguration() {} diff --git a/src/main/java/com/serliunx/ddns/config/CommandLineConfiguration.java b/src/main/java/com/serliunx/ddns/config/CommandLineConfiguration.java new file mode 100644 index 0000000..b31cd02 --- /dev/null +++ b/src/main/java/com/serliunx/ddns/config/CommandLineConfiguration.java @@ -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 SerLiunx + * @version 1.0.3 + * @since 2024/12/24 + */ +public final class CommandLineConfiguration extends AbstractConfiguration implements Combination { + + /** + * 原始参数 + */ + private final String[] sourceArgs; + /** + * 待合并的其他配置信息 + */ + private final Collection configurations; + + /** + * 配置项标记 + */ + private static final String TAG = "-D"; + /** + * 配置赋值符号(=) + */ + private static final String EQUAL = "="; + /** + * 配置缓存 + */ + private final Map cache = new HashMap<>(); + + public CommandLineConfiguration(String[] sourceArgs) { + this(sourceArgs, new ArrayList<>()); + } + + public CommandLineConfiguration(String[] sourceArgs, Collection 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 configurations) { + this.configurations.addAll(configurations); + } + + @Override + public Configuration getOriginal() { + return this; + } + + @Override + public Collection 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(); + } + + /** + * 将其他配置信息合并到当前配置信息 + *
  • 命令行参数的配置信息优先级高于其他配置信息 + *
  • 仅在持有锁的情况下访问 + */ + private void merge() { + Assert.notEmpty(configurations); + + for (Configuration configuration : configurations) { + if (configuration instanceof AbstractConfiguration) { + AbstractConfiguration ac = (AbstractConfiguration) configuration; + ac.refresh0(); + } + + final Map keyValue = configuration.getAllKeyAndValue(); + keyValue.forEach((k, v) -> { + if (!cache.containsKey(k)) { + cache.put(k, v); + } + }); + } + } +} diff --git a/src/main/java/com/serliunx/ddns/core/Combination.java b/src/main/java/com/serliunx/ddns/core/Combination.java new file mode 100644 index 0000000..2cb49c6 --- /dev/null +++ b/src/main/java/com/serliunx/ddns/core/Combination.java @@ -0,0 +1,43 @@ +package com.serliunx.ddns.core; + +import java.util.Collection; + +/** + * 组合, 将多个对象以一定的逻辑相组合 + * + * @author SerLiunx + * @version 1.0.3 + * @since 2024/12/24 + * + * @param 合并的类型 + */ +public interface Combination { + + /** + * 组合指定对象 + * + * @param e 对象 + */ + void from(E e); + + /** + * 批量组合对象 + * + * @param es 对象集合 + */ + void from(Collection es); + + /** + * 获取原始对象(未组合前的) + * + * @return 原始对象 + */ + E getOriginal(); + + /** + * 获取该对象中所有组合的来源 + * + * @return 所有组合来源 + */ + Collection getCombinations(); +} diff --git a/src/test/java/com/serliunx/ddns/test/config/CmdConfigurationTest.java b/src/test/java/com/serliunx/ddns/test/config/CmdConfigurationTest.java new file mode 100644 index 0000000..086a26b --- /dev/null +++ b/src/test/java/com/serliunx/ddns/test/config/CmdConfigurationTest.java @@ -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 SerLiunx + * @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(); + } +}