[Change] 调整包路径, 增加 loadGlobalProperties 命令以提供重载方式;

This commit is contained in:
LamGC 2020-04-11 12:50:27 +08:00
parent 841a368767
commit e20f9d34c9

View File

@ -1,4 +1,4 @@
package net.lamgc.cgj; package net.lamgc.cgj.bot;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import net.lamgc.utils.base.runner.Argument; import net.lamgc.utils.base.runner.Argument;
@ -6,25 +6,26 @@ import net.lamgc.utils.base.runner.Command;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.Properties;
public class CQBotAdminProcess { public class BotAdminCommandProcess {
private final static Logger log = LoggerFactory.getLogger("CQBotAdminProcess"); private final static Logger log = LoggerFactory.getLogger(BotAdminCommandProcess.class.getSimpleName());
private final static File globalPropFile = new File("./global.properties");
@Command @Command
public String clearCache() { public String clearCache() {
CQProcess.clearCache(); BotCommandProcess.clearCache();
return "操作已完成."; return "操作已完成.";
} }
@Command @Command
public String setGlobalProperty(@Argument(name = "key") String key, @Argument(name = "value") String value, @Argument(name = "save", force = false) boolean saveNow) { public String setGlobalProperty(@Argument(name = "key") String key, @Argument(name = "value") String value, @Argument(name = "save", force = false) boolean saveNow) {
String lastValue = CQPluginMain.globalProp.getProperty(key); String lastValue = BotCommandProcess.globalProp.getProperty(key);
CQPluginMain.globalProp.setProperty(key, Strings.nullToEmpty(value)); BotCommandProcess.globalProp.setProperty(key, Strings.nullToEmpty(value));
if(saveNow) { if(saveNow) {
saveGlobalProperties(); saveGlobalProperties();
} }
@ -33,13 +34,13 @@ public class CQBotAdminProcess {
@Command @Command
public String getGlobalProperty(@Argument(name = "key") String key) { public String getGlobalProperty(@Argument(name = "key") String key) {
return "全局配置项 " + key + " 当前值: " + CQPluginMain.globalProp.getProperty(key, "(Empty)"); return "全局配置项 " + key + " 当前值: " + BotCommandProcess.globalProp.getProperty(key, "(Empty)");
} }
@Command @Command
public String saveGlobalProperties() { public String saveGlobalProperties() {
log.info("正在保存全局配置文件..."); log.info("正在保存全局配置文件...");
File globalPropFile = new File("./global.properties");
try { try {
if(!globalPropFile.exists()) { if(!globalPropFile.exists()) {
if(!globalPropFile.createNewFile()) { if(!globalPropFile.createNewFile()) {
@ -47,7 +48,7 @@ public class CQBotAdminProcess {
return "全局配置项文件保存失败!"; return "全局配置项文件保存失败!";
} }
} }
CQPluginMain.globalProp.store(new FileOutputStream(globalPropFile), ""); BotCommandProcess.globalProp.store(new FileOutputStream(globalPropFile), "");
log.info("全局配置文件保存成功!"); log.info("全局配置文件保存成功!");
return "保存全局配置文件 - 操作已完成."; return "保存全局配置文件 - 操作已完成.";
} catch (IOException e) { } catch (IOException e) {
@ -56,10 +57,31 @@ public class CQBotAdminProcess {
} }
} }
@Command
public String loadGlobalProperties(@Argument(name = "reload", force = false) boolean reload) {
Properties cache = new Properties();
if(!globalPropFile.exists()) {
return "未找到全局配置文件, 无法重载";
}
try(Reader reader = new BufferedReader(new FileReader(globalPropFile))) {
cache.load(reader);
} catch (IOException e) {
log.error("重载全局配置文件时发生异常", e);
return "加载全局配置文件时发生错误!";
}
if(reload) {
BotCommandProcess.globalProp.clear();
}
BotCommandProcess.globalProp.putAll(cache);
return "全局配置文件重载完成.";
}
@Command @Command
public String runUpdateTask(@Argument(force = false, name = "date") Date queryTime) { public String runUpdateTask(@Argument(force = false, name = "date") Date queryTime) {
try { try {
CQProcess.runUpdateTimer(queryTime); BotCommandProcess.runUpdateTimer(queryTime);
} catch (Exception e) { } catch (Exception e) {
log.error("执行更新任务时发生异常", e); log.error("执行更新任务时发生异常", e);
return "操作执行时发生错误!"; return "操作执行时发生错误!";