From 379f5c703291bb562a7eccdd13ec142308dde7f0 Mon Sep 17 00:00:00 2001 From: SerLiunx-ctrl <17689543@qq.com> Date: Thu, 23 Jan 2025 16:13:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Econfig=E6=8C=87?= =?UTF-8?q?=E4=BB=A4,=20=E6=94=AF=E6=8C=81=E7=83=AD=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=A1=E6=81=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/serliunx/ddns/ManagerLite.java | 3 ++ .../ddns/config/AbstractConfiguration.java | 33 +++++++++++++++-- .../serliunx/ddns/config/Configuration.java | 19 ++++++++++ .../ddns/support/command/AbstractCommand.java | 9 +++++ .../support/command/CommandDispatcher.java | 10 ------ .../support/command/target/ConfigCommand.java | 36 +++++++++++++++++++ .../support/command/target/HelpCommand.java | 6 ---- .../support/command/target/ReloadCommand.java | 30 +++++++++------- 8 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/serliunx/ddns/support/command/target/ConfigCommand.java diff --git a/src/main/java/com/serliunx/ddns/ManagerLite.java b/src/main/java/com/serliunx/ddns/ManagerLite.java index e2044d2..b1f0d0d 100644 --- a/src/main/java/com/serliunx/ddns/ManagerLite.java +++ b/src/main/java/com/serliunx/ddns/ManagerLite.java @@ -9,6 +9,7 @@ import com.serliunx.ddns.core.context.MultipleSourceInstanceContext; import com.serliunx.ddns.support.InstanceContextHolder; import com.serliunx.ddns.support.SystemInitializer; import com.serliunx.ddns.support.command.CommandDispatcher; +import com.serliunx.ddns.support.command.target.ConfigCommand; import com.serliunx.ddns.support.command.target.HelpCommand; import com.serliunx.ddns.support.command.target.ReloadCommand; import com.serliunx.ddns.support.log.JLineAdaptAppender; @@ -120,6 +121,8 @@ public final class ManagerLite { commandDispatcher.register(new HelpCommand()); // reload commandDispatcher.register(new ReloadCommand(configuration, systemInitializer)); + // config + commandDispatcher.register(new ConfigCommand(configuration)); } /** diff --git a/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java b/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java index f8db3cc..229c18d 100644 --- a/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java +++ b/src/main/java/com/serliunx/ddns/config/AbstractConfiguration.java @@ -21,7 +21,7 @@ public abstract class AbstractConfiguration implements Configuration { protected final Logger log = LoggerFactory.getLogger(this.getClass()); protected final Map valueMap = new LinkedHashMap<>(16); - protected final Lock loadLock = new ReentrantLock(); + protected final Lock contextLock = new ReentrantLock(); public AbstractConfiguration() {} @@ -111,6 +111,33 @@ public abstract class AbstractConfiguration implements Configuration { return valueMap; } + @Override + public boolean modify(String key, Object value) { + try { + contextLock.lock(); + if (!valueMap.containsKey(key)) + return false; + valueMap.put(key, String.valueOf(value)); + return true; + } finally { + contextLock.unlock(); + } + } + + @Override + public void modify(String key, Object value, boolean createIfAbsent) { + try { + contextLock.lock(); + if (!valueMap.containsKey(key)) { + if (createIfAbsent) + valueMap.put(key, String.valueOf(value)); + } else + valueMap.put(key, String.valueOf(value)); + } finally { + contextLock.unlock(); + } + } + @Override public int getPriority() { return Integer.MAX_VALUE; @@ -121,12 +148,12 @@ public abstract class AbstractConfiguration implements Configuration { */ protected void load() { try { - loadLock.lock(); + contextLock.lock(); // 清空原有的配置信息 valueMap.clear(); load0(); }finally { - loadLock.unlock(); + contextLock.unlock(); } } diff --git a/src/main/java/com/serliunx/ddns/config/Configuration.java b/src/main/java/com/serliunx/ddns/config/Configuration.java index 9929933..d56c820 100644 --- a/src/main/java/com/serliunx/ddns/config/Configuration.java +++ b/src/main/java/com/serliunx/ddns/config/Configuration.java @@ -100,4 +100,23 @@ public interface Configuration extends Refreshable, Priority { * @return 配置文件所有成功加载的键值对 */ Map getAllKeyAndValue(); + + /** + * 修改配置项(锁) + * + * @param key 配置键 + * @param value 新的值 + * @return 成功修改返回真, 否则返回假; 指定键不存在时则修改失败 + */ + boolean modify(String key, Object value); + + /** + * 修改配置项(锁) + *
  • 指定配置键不存在时则会根绝是否需要创建而新增 + * + * @param key 配置键 + * @param value 新的值 + * @param createIfAbsent 是否在不存在指定键时创建 + */ + void modify(String key, Object value, boolean createIfAbsent); } diff --git a/src/main/java/com/serliunx/ddns/support/command/AbstractCommand.java b/src/main/java/com/serliunx/ddns/support/command/AbstractCommand.java index d8d1263..8e9564e 100644 --- a/src/main/java/com/serliunx/ddns/support/command/AbstractCommand.java +++ b/src/main/java/com/serliunx/ddns/support/command/AbstractCommand.java @@ -1,5 +1,8 @@ package com.serliunx.ddns.support.command; +import com.serliunx.ddns.ManagerLite; +import org.slf4j.Logger; + import java.util.List; /** @@ -17,6 +20,8 @@ public abstract class AbstractCommand implements Command { private final String description; private final String usage; + protected final Logger log = ManagerLite.getLogger(); + public AbstractCommand(String name, List subCommands, String description, String usage) { this.name = name; this.subCommands = subCommands; @@ -43,4 +48,8 @@ public abstract class AbstractCommand implements Command { public String getUsage() { return usage; } + + protected boolean hasArgs(String[] args) { + return args.length > 0; + } } diff --git a/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java b/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java index 25419ec..76cb8d4 100644 --- a/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java +++ b/src/main/java/com/serliunx/ddns/support/command/CommandDispatcher.java @@ -91,16 +91,6 @@ public final class CommandDispatcher { return newArgs; } - /** - * 检查是否存在参数 - * - * @param args 参数 - * @return 参数长度大于0返回真, 否则返回假 - */ - public static boolean hasArgs(String[] args) { - return args.length > 0; - } - /** * 获取实例 */ diff --git a/src/main/java/com/serliunx/ddns/support/command/target/ConfigCommand.java b/src/main/java/com/serliunx/ddns/support/command/target/ConfigCommand.java new file mode 100644 index 0000000..dd746fe --- /dev/null +++ b/src/main/java/com/serliunx/ddns/support/command/target/ConfigCommand.java @@ -0,0 +1,36 @@ +package com.serliunx.ddns.support.command.target; + +import com.serliunx.ddns.config.Configuration; +import com.serliunx.ddns.support.command.AbstractCommand; + +/** + * 指令: config + * + * @author SerLiunx + * @version 1.0.4 + * @since 2025/1/22 + */ +public class ConfigCommand extends AbstractCommand { + + /** + * 配置信息 + */ + private final Configuration configuration; + + public ConfigCommand(Configuration configuration) { + super("config", null, "调整配置信息", "config <配置项> 新的值"); + this.configuration = configuration; + } + + @Override + public boolean onCommand(String[] args) { + if (!hasArgs(args) || + args.length < 2) { + log.warn("用法 => {}", getUsage()); + return true; + } + final String target = args[0]; + final String value = args[1]; + return configuration.modify(target, value); + } +} diff --git a/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java b/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java index bdd695d..aa25f9c 100644 --- a/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java +++ b/src/main/java/com/serliunx/ddns/support/command/target/HelpCommand.java @@ -1,15 +1,11 @@ package com.serliunx.ddns.support.command.target; -import com.serliunx.ddns.ManagerLite; import com.serliunx.ddns.support.command.AbstractCommand; import com.serliunx.ddns.support.command.Command; import com.serliunx.ddns.support.command.CommandDispatcher; -import org.slf4j.Logger; import java.util.Map; -import static com.serliunx.ddns.support.command.CommandDispatcher.hasArgs; - /** * 指令: help * @@ -19,8 +15,6 @@ import static com.serliunx.ddns.support.command.CommandDispatcher.hasArgs; */ public class HelpCommand extends AbstractCommand { - private static final Logger log = ManagerLite.getLogger(); - public HelpCommand() { super("help", null, "查看帮助信息", "help <指令>"); } diff --git a/src/main/java/com/serliunx/ddns/support/command/target/ReloadCommand.java b/src/main/java/com/serliunx/ddns/support/command/target/ReloadCommand.java index 0a62ebd..edb1e7b 100644 --- a/src/main/java/com/serliunx/ddns/support/command/target/ReloadCommand.java +++ b/src/main/java/com/serliunx/ddns/support/command/target/ReloadCommand.java @@ -1,13 +1,9 @@ package com.serliunx.ddns.support.command.target; -import com.serliunx.ddns.ManagerLite; import com.serliunx.ddns.config.Configuration; import com.serliunx.ddns.support.SystemInitializer; import com.serliunx.ddns.support.command.AbstractCommand; import com.serliunx.ddns.support.ipprovider.ScheduledProvider; -import org.slf4j.Logger; - -import java.util.Objects; import static com.serliunx.ddns.constant.ConfigurationKeys.KEY_TASK_REFRESH_INTERVAL_IP; @@ -20,8 +16,6 @@ import static com.serliunx.ddns.constant.ConfigurationKeys.KEY_TASK_REFRESH_INTE */ public class ReloadCommand extends AbstractCommand { - private static final Logger log = ManagerLite.getLogger(); - /** * 配置信息 */ @@ -47,18 +41,28 @@ public class ReloadCommand extends AbstractCommand { configuration.refresh(); // 更新定时查询IP任务 + triggerScheduledProvider(oldIpInterval); + + log.info("配置文件已重新载入!"); + return true; + } + + /** + * 获取更新周期 + */ + private long getIpInterval() { + return configuration.getLong(KEY_TASK_REFRESH_INTERVAL_IP, 300L); + } + + /** + * 更新定时查询IP任务 + */ + private void triggerScheduledProvider(long oldIpInterval) { final ScheduledProvider scheduledProvider = systemInitializer.getScheduledProvider(); final long newIpInterval = getIpInterval(); if (scheduledProvider != null && oldIpInterval != newIpInterval) { scheduledProvider.changeTimePeriod(newIpInterval); } - - log.info("配置文件已重新载入!"); - return true; - } - - private long getIpInterval() { - return configuration.getLong(KEY_TASK_REFRESH_INTERVAL_IP, 300L); } }