diff --git a/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java b/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java index fd9c047..738ca7b 100644 --- a/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/CreeperConfigInitMachine.java @@ -3,12 +3,10 @@ package org.example.init; import org.example.config.CreeperConfigFile; import org.example.constpool.ConstPool; import org.example.constpool.CreeperModuleConstPool; +import org.example.constpool.ModuleName; import org.example.constpool.PluginName; -import org.example.log.ChopperLogFactory; -import org.example.log.LoggerType; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; -import org.example.taskcenter.TaskCenter; +import org.example.plugin.annotation.Plugin; import org.example.util.FileUtil; import java.io.IOException; @@ -22,7 +20,7 @@ import java.util.List; * @date 2023/07/29 01:56 **/ -@Plugin(moduleName = ConstPool.CREEPER, +@Plugin(moduleName = ModuleName.CREEPER, pluginName = PluginName.CREEPER_CONFIG_PLUGIN, needPlugin = {PluginName.FILE_CACHE_PLUGIN}, pluginClass= CreeperConfigFile.class ) diff --git a/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java b/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java index ebe390b..bcdb6a0 100644 --- a/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java +++ b/CreeperModule/src/main/java/org/example/init/TaskCenterInitMachine.java @@ -1,10 +1,9 @@ package org.example.init; import org.example.constpool.ConstPool; +import org.example.constpool.ModuleName; import org.example.constpool.PluginName; -import org.example.log.ChopperLogFactory; -import org.example.log.LoggerType; -import org.example.plugin.Plugin; +import org.example.plugin.annotation.Plugin; import org.example.taskcenter.TaskCenter; import java.util.List; @@ -14,7 +13,7 @@ import java.util.List; * @date 2023/07/28 23:49 **/ -@Plugin(moduleName = ConstPool.CREEPER, +@Plugin(moduleName = ModuleName.CREEPER, pluginName = PluginName.TASK_CENTER_PLUGIN, needPlugin = {PluginName.FILE_CACHE_PLUGIN,PluginName.CREEPER_CONFIG_PLUGIN}, pluginClass= TaskCenter.class ) diff --git a/FileModule/src/main/java/org/example/constpool/GlobalFileCache.java b/FileModule/src/main/java/org/example/constpool/GlobalFileCache.java index c25057b..1edea34 100644 --- a/FileModule/src/main/java/org/example/constpool/GlobalFileCache.java +++ b/FileModule/src/main/java/org/example/constpool/GlobalFileCache.java @@ -1,10 +1,6 @@ package org.example.constpool; import org.example.cache.FileCache; -import org.example.exception.FileCacheException; -import org.example.pojo.configfile.ModuleSrcConfigFile; - -import java.util.List; /** * @author Genius diff --git a/FileModule/src/main/java/org/example/init/ConfigInitMachine.java b/FileModule/src/main/java/org/example/init/ConfigInitMachine.java index 20c25b2..8bc1c34 100644 --- a/FileModule/src/main/java/org/example/init/ConfigInitMachine.java +++ b/FileModule/src/main/java/org/example/init/ConfigInitMachine.java @@ -2,9 +2,8 @@ package org.example.init; import org.example.bean.ConfigFile; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; +import org.example.plugin.annotation.Plugin; import org.example.util.ConfigFileUtil; -import org.slf4j.Logger; import java.nio.file.Paths; import java.util.List; diff --git a/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java b/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java index 30d752c..7f759e3 100644 --- a/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java +++ b/FileModule/src/main/java/org/example/init/FileCacheManagerInitMachine.java @@ -3,11 +3,10 @@ package org.example.init; import org.example.cache.FileCacheManager; import org.example.cache.FileCacheManagerInstance; import org.example.constpool.ConstPool; +import org.example.constpool.ModuleName; import org.example.constpool.PluginName; -import org.example.log.ChopperLogFactory; -import org.example.log.LoggerType; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; +import org.example.plugin.annotation.Plugin; import java.util.List; @@ -16,7 +15,7 @@ import java.util.List; * @date 2023/04/26 02:09 **/ -@Plugin(moduleName = ConstPool.FILE, +@Plugin(moduleName = ModuleName.FILE, pluginName = PluginName.FILE_CACHE_PLUGIN, needPlugin = {}, pluginClass= FileCacheManager.class ) diff --git a/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java b/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java index 154caaf..cb91942 100644 --- a/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java +++ b/FileModule/src/main/java/org/example/init/module/FileModuleInitMachine.java @@ -1,14 +1,10 @@ package org.example.init.module; import org.example.constpool.ConstPool; -import org.example.constpool.PluginName; -import org.example.init.FileCacheManagerInitMachine; +import org.example.constpool.ModuleName; import org.example.init.ModuleInitMachine; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; -import org.example.plugin.Plugin; - -import java.util.List; /** * @author Genius @@ -18,7 +14,7 @@ import java.util.List; public class FileModuleInitMachine extends ModuleInitMachine { public FileModuleInitMachine() { - super(ConstPool.FILE, ChopperLogFactory.getLogger(LoggerType.File)); + super(ModuleName.FILE, ChopperLogFactory.getLogger(LoggerType.File)); } } diff --git a/FileModule/src/main/java/org/example/pojo/configfile/ModuleSrcConfigFile.java b/FileModule/src/main/java/org/example/pojo/configfile/ChopperBotConfigFile.java similarity index 81% rename from FileModule/src/main/java/org/example/pojo/configfile/ModuleSrcConfigFile.java rename to FileModule/src/main/java/org/example/pojo/configfile/ChopperBotConfigFile.java index 979a3fa..d86817e 100644 --- a/FileModule/src/main/java/org/example/pojo/configfile/ModuleSrcConfigFile.java +++ b/FileModule/src/main/java/org/example/pojo/configfile/ChopperBotConfigFile.java @@ -15,14 +15,14 @@ import java.util.Map; **/ -public class ModuleSrcConfigFile extends ConfigFile> { +public class ChopperBotConfigFile extends ConfigFile> { private static final String filePath = "./config/"; private static final String fileName = "chopperBotConfig.json"; - public ModuleSrcConfigFile() { - super( "ChopperBot", PluginName.MODULE_CONFIG_PLUGIN,List.of(),true,filePath, fileName, + public ChopperBotConfigFile() { + super( "ChopperBot", PluginName.CHOPPER_BOT_CONFIG_PLUGIN,List.of(),true,filePath, fileName, Map.of("src",Map.of( ConstPool.ACCOUNT, "./config/"+ConstPool.ACCOUNT, ConstPool.SECTION, "./config/"+ConstPool.SECTION, ConstPool.BARRAGE, "./config/"+ConstPool.BARRAGE, @@ -30,7 +30,7 @@ public class ModuleSrcConfigFile extends ConfigFile> { ConstPool.SECTION_WORK, "./config/"+ConstPool.SECTION_WORK, ConstPool.HOT, "./config/"+ConstPool.HOT, ConstPool.PUBLISH, "./config/"+ConstPool.PUBLISH), - "plugin",Map.of()), FileType.CHOPPER_BOT); + "pluginStart",Map.of()), FileType.CHOPPER_BOT); } diff --git a/FileModule/src/test/java/org/example/cache/FileCacheTest.java b/FileModule/src/test/java/org/example/cache/FileCacheTest.java index b0ed6c0..62950a7 100644 --- a/FileModule/src/test/java/org/example/cache/FileCacheTest.java +++ b/FileModule/src/test/java/org/example/cache/FileCacheTest.java @@ -2,7 +2,7 @@ package org.example.cache; import org.example.constpool.GlobalFileCache; import org.example.pojo.Student; -import org.example.pojo.configfile.ModuleSrcConfigFile; +import org.example.pojo.configfile.ChopperBotConfigFile; import org.example.exception.FileCacheException; import org.junit.jupiter.api.Test; @@ -35,8 +35,8 @@ public class FileCacheTest { @Test public void TestFileCache() throws FileCacheException, InterruptedException { - ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile(); - FileCache fileCache = new FileCache(moduleSrcConfigFile); + ChopperBotConfigFile chopperBotConfigFile = new ChopperBotConfigFile(); + FileCache fileCache = new FileCache(chopperBotConfigFile); FileCacheManager manager = new FileCacheManager(List.of(fileCache)); manager.start(); @@ -45,8 +45,8 @@ public class FileCacheTest { @Test public void TestFileCacheManager() throws FileCacheException, InterruptedException { - ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile(); - FileCache fileCache = new FileCache(moduleSrcConfigFile); + ChopperBotConfigFile chopperBotConfigFile = new ChopperBotConfigFile(); + FileCache fileCache = new FileCache(chopperBotConfigFile); FileCacheManager manager = new FileCacheManager(List.of(fileCache)); manager.start(); Thread.sleep(500000); @@ -54,8 +54,8 @@ public class FileCacheTest { @Test public void TestGet() throws FileCacheException, InterruptedException { - ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile(); - FileCache fileCache = new FileCache(moduleSrcConfigFile); + ChopperBotConfigFile chopperBotConfigFile = new ChopperBotConfigFile(); + FileCache fileCache = new FileCache(chopperBotConfigFile); FileCacheManager manager = new FileCacheManager(List.of(fileCache)); manager.start(); @@ -101,8 +101,8 @@ public class FileCacheTest { @Test public void TestArrayAdd() throws InterruptedException, FileCacheException { - ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile(); - FileCache fileCache = new FileCache(moduleSrcConfigFile); + ChopperBotConfigFile chopperBotConfigFile = new ChopperBotConfigFile(); + FileCache fileCache = new FileCache(chopperBotConfigFile); FileCacheManager manager = new FileCacheManager(List.of(fileCache)); manager.start(); diff --git a/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java b/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java index bf0e06c..d114a30 100644 --- a/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HeatRecommendationInitMachine.java @@ -1,13 +1,11 @@ package org.example.init; import org.example.constpool.ConstPool; +import org.example.constpool.ModuleName; import org.example.constpool.PluginName; import org.example.core.recommend.HeatRecommendation; -import org.example.log.ChopperLogFactory; -import org.example.log.LoggerType; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; -import org.example.taskcenter.TaskCenter; +import org.example.plugin.annotation.Plugin; import java.util.List; @@ -16,7 +14,7 @@ import java.util.List; * @date 2023/07/29 14:48 **/ -@Plugin(moduleName = ConstPool.HOT, +@Plugin(moduleName = ModuleName.HOT, pluginName = PluginName.HOT_RECOMMENDATION_PLUGIN, needPlugin = {PluginName.HOT_CONFIG_PLUGIN,PluginName.HOT_GUARD_PLUGIN}, pluginClass= HeatRecommendation.class ) diff --git a/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java b/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java index 60fb755..a1c9e99 100644 --- a/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HotConfigInitMachine.java @@ -7,19 +7,17 @@ package org.example.init; import org.example.config.HotModuleConfig; import org.example.constpool.ConstPool; +import org.example.constpool.ModuleName; import org.example.constpool.PluginName; -import org.example.core.recommend.HeatRecommendation; -import org.example.log.ChopperLogFactory; -import org.example.log.LoggerType; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; +import org.example.plugin.annotation.Plugin; import java.util.List; /** * 热门模块配置文件初始化机器 */ -@Plugin(moduleName = ConstPool.HOT, +@Plugin(moduleName = ModuleName.HOT, pluginName = PluginName.HOT_CONFIG_PLUGIN, needPlugin = {PluginName.FILE_CACHE_PLUGIN}, pluginClass= HotModuleConfig.class ) diff --git a/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java b/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java index 5ed0a23..7fb69ab 100644 --- a/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java +++ b/HotModule/src/main/java/org/example/init/HotGuardInitMachine.java @@ -1,36 +1,21 @@ package org.example.init; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import org.example.cache.FileCache; -import org.example.cache.FileCacheManagerInstance; -import org.example.config.HotModuleConfig; -import org.example.config.HotModuleSetting; import org.example.constpool.ConstPool; +import org.example.constpool.ModuleName; import org.example.constpool.PluginName; -import org.example.core.control.HotModuleLoadTask; import org.example.core.guard.HotModuleGuard; import org.example.core.guard.HotModuleGuardInstance; -import org.example.core.guard.Guard; -import org.example.log.ChopperLogFactory; -import org.example.log.LoggerType; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; -import org.example.util.ClassUtil; +import org.example.plugin.annotation.Plugin; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; - -import static org.example.constpool.HotModuleConstPool.LOAD_TASK_CLASS_ROOT; /** * @author Genius * @date 2023/07/21 09:58 **/ -@Plugin(moduleName = ConstPool.HOT, +@Plugin(moduleName = ModuleName.HOT, pluginName = PluginName.HOT_GUARD_PLUGIN, needPlugin = {PluginName.FILE_CACHE_PLUGIN,PluginName.HOT_CONFIG_PLUGIN}, pluginClass= HotModuleGuard.class ) diff --git a/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java b/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java index 39e438a..6efc85d 100644 --- a/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java +++ b/HotModule/src/main/java/org/example/init/module/HotModuleInitMachine.java @@ -1,6 +1,7 @@ package org.example.init.module; import org.example.constpool.ConstPool; +import org.example.constpool.ModuleName; import org.example.init.HeatRecommendationInitMachine; import org.example.init.HotConfigInitMachine; import org.example.init.HotGuardInitMachine; @@ -23,7 +24,7 @@ public class HotModuleInitMachine extends ModuleInitMachine { super( List.of(ConstPool.FILE,ConstPool.CREEPER), ChopperLogFactory.getLogger(LoggerType.Hot), - ConstPool.HOT + ModuleName.HOT ); } } diff --git a/common/pom.xml b/common/pom.xml index 938adad..fc4e926 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -43,5 +43,10 @@ reflections 0.9.11 + + org.springframework.boot + spring-boot-starter-aop + + diff --git a/common/src/main/java/org/example/aop/PluginAspect.java b/common/src/main/java/org/example/aop/PluginAspect.java new file mode 100644 index 0000000..e34d5f6 --- /dev/null +++ b/common/src/main/java/org/example/aop/PluginAspect.java @@ -0,0 +1,37 @@ +package org.example.aop; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.reflect.MethodSignature; +import org.example.exception.plugin.PluginNotRegisterException; +import org.example.init.InitPluginRegister; +import org.example.plugin.annotation.CheckPlugin; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +/** + * @author Genius + * @date 2023/08/02 16:43 + **/ + +@Aspect +@Component +public class PluginAspect { + + @Before("@annotation(org.example.plugin.annotation.CheckPlugin)") + public void checkPluginRegister(JoinPoint point){ + Signature signature = point.getSignature(); + MethodSignature methodSignature = (MethodSignature) signature; + Method method = methodSignature.getMethod(); + CheckPlugin annotation = method.getAnnotation(CheckPlugin.class); + + for (String plugin : annotation.needPlugin()) { + if(!InitPluginRegister.isRegister(plugin)){ + throw new PluginNotRegisterException(); + } + } + } +} diff --git a/common/src/main/java/org/example/bean/ConfigFile.java b/common/src/main/java/org/example/bean/ConfigFile.java index f4373d4..accb4e1 100644 --- a/common/src/main/java/org/example/bean/ConfigFile.java +++ b/common/src/main/java/org/example/bean/ConfigFile.java @@ -1,5 +1,6 @@ package org.example.bean; +import org.example.constpool.PluginName; import org.example.plugin.CommonPlugin; import java.time.LocalDateTime; @@ -25,7 +26,7 @@ public abstract class ConfigFile extends CommonPlugin { private LocalDateTime updateTime; public ConfigFile() { - super(null, null, null, true); + super(null, null, List.of(PluginName.FILE_CACHE_PLUGIN), true); } public ConfigFile(String filePath, String fileName, T data,FileType fileType) { diff --git a/common/src/main/java/org/example/constpool/ModuleName.java b/common/src/main/java/org/example/constpool/ModuleName.java new file mode 100644 index 0000000..6ac6a73 --- /dev/null +++ b/common/src/main/java/org/example/constpool/ModuleName.java @@ -0,0 +1,16 @@ +package org.example.constpool; + +/** + * 模块名 + * @author Genius + * @date 2023/08/02 18:44 + **/ +public class ModuleName { + public static final String CHOPPER_BOT = "ChopperBot"; + + public static final String HOT = ConstPool.HOT; + + public static final String CREEPER = ConstPool.CREEPER; + + public static final String FILE = ConstPool.FILE; +} diff --git a/common/src/main/java/org/example/constpool/PluginName.java b/common/src/main/java/org/example/constpool/PluginName.java index ecc23a2..a795f2b 100644 --- a/common/src/main/java/org/example/constpool/PluginName.java +++ b/common/src/main/java/org/example/constpool/PluginName.java @@ -11,8 +11,8 @@ package org.example.constpool; public class PluginName { //File - public static final String MODULE_CONFIG_PLUGIN = "ModuleConfig"; - public static final String FILE_CACHE_PLUGIN = "FileCache"; + public static final String CHOPPER_BOT_CONFIG_PLUGIN = "ChopperBotConfig"; + public static final String FILE_CACHE_PLUGIN = "FileCacheManager"; //Creeper public static final String CREEPER_CONFIG_PLUGIN = "CreeperConfig"; diff --git a/common/src/main/java/org/example/exception/BaseException.java b/common/src/main/java/org/example/exception/BaseException.java index 24c6e56..b0470fc 100644 --- a/common/src/main/java/org/example/exception/BaseException.java +++ b/common/src/main/java/org/example/exception/BaseException.java @@ -11,13 +11,13 @@ import org.example.exception.Impl.ResultCode; public class BaseException extends RuntimeException { private static final long serialVersionUID = 5225171867523879342L; - private int code; + protected int code; - private String msg; + protected String msg; - private Object[] params; + protected Object[] params; - ResultCode resultCode; + protected ResultCode resultCode; public ResultCode getResultCode() { return resultCode; diff --git a/common/src/main/java/org/example/exception/ChopperBotException.java b/common/src/main/java/org/example/exception/ChopperBotException.java new file mode 100644 index 0000000..bc11580 --- /dev/null +++ b/common/src/main/java/org/example/exception/ChopperBotException.java @@ -0,0 +1,20 @@ +package org.example.exception; + +import org.example.log.ChopperLogFactory; +import org.example.log.LoggerType; +import org.slf4j.Logger; + +/** + * @author Genius + * @date 2023/08/02 16:52 + **/ +public class ChopperBotException extends BaseException{ + + + + protected Logger logger = ChopperLogFactory.getLogger(LoggerType.System); + + public ChopperBotException(String message) { + super(message); + } +} diff --git a/common/src/main/java/org/example/exception/GlobalExceptionHandler.java b/common/src/main/java/org/example/exception/GlobalExceptionHandler.java index 9e3282b..e89e2f1 100644 --- a/common/src/main/java/org/example/exception/GlobalExceptionHandler.java +++ b/common/src/main/java/org/example/exception/GlobalExceptionHandler.java @@ -2,6 +2,11 @@ package org.example.exception; import org.example.exception.Impl.ResultCode; +import org.example.exception.plugin.PluginDependOnException; +import org.example.exception.plugin.PluginException; +import org.example.exception.plugin.PluginNotRegisterException; +import org.example.log.ChopperLogFactory; +import org.example.log.LoggerType; import org.example.util.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,6 +16,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; +import java.util.Map; /** * @author : [Welsir] @@ -21,9 +27,26 @@ import javax.servlet.http.HttpServletRequest; @ResponseBody public class GlobalExceptionHandler { - public static Logger logger = LoggerFactory.getLogger(BaseException.class); + public static Logger logger = ChopperLogFactory.getLogger(LoggerType.System); - @ExceptionHandler(Exception.class) + + @ExceptionHandler(PluginException.class) + public Result handlerPluginException(HttpServletRequest request,PluginException ex){ + logger.error("Handle Exception Request Url:{},Exception:{}", request.getRequestURL(), ex); + if(ex instanceof PluginNotRegisterException){ + PluginNotRegisterException exception = (PluginNotRegisterException) ex; + return Result.error(exception.getResultCode()); + }else if(ex instanceof PluginDependOnException){ + PluginDependOnException exception = (PluginDependOnException) ex; + return Result.error(exception.getResultCode(), Map.of( + "fatherName",exception.getFatherName(), + "sonName",exception.getSonName() + )); + } + return Result.error("error"); + } + + @ExceptionHandler(BaseException.class) public Result handleException(HttpServletRequest request, Exception ex) { logger.error("Handle Exception Request Url:{},Exception:{}", request.getRequestURL(), ex); diff --git a/common/src/main/java/org/example/exception/Impl/ResultCode.java b/common/src/main/java/org/example/exception/Impl/ResultCode.java index 171059b..ffe3977 100644 --- a/common/src/main/java/org/example/exception/Impl/ResultCode.java +++ b/common/src/main/java/org/example/exception/Impl/ResultCode.java @@ -12,8 +12,12 @@ public enum ResultCode { OK(200, "成功"), ERROR(300,"系统异常"), - PARAMETER_ERROR(301, "参数错误"); + PARAMETER_ERROR(301, "参数错误"), + + PLUGIN_NOT_REGISTER(114514,"插件没有注册"), + + PLUGIN_DEPEND_ERROR(114515,""); private int code; private String msg; diff --git a/common/src/main/java/org/example/exception/plugin/PluginDependOnException.java b/common/src/main/java/org/example/exception/plugin/PluginDependOnException.java new file mode 100644 index 0000000..c3c7900 --- /dev/null +++ b/common/src/main/java/org/example/exception/plugin/PluginDependOnException.java @@ -0,0 +1,45 @@ +package org.example.exception.plugin; + +import lombok.Data; +import org.example.exception.Impl.ResultCode; + +/** + * @author Genius + * @date 2023/08/02 18:54 + **/ +public class PluginDependOnException extends PluginException{ + + private String fatherName; + private String sonName; + public PluginDependOnException(String msg,String fatherName,String sonName) { + super(msg); + this.fatherName = fatherName; + this.sonName = sonName; + } + + public static PluginDependOnException MissingFatherPlugin(String fatherName, String sonName){ + PluginDependOnException pluginDependOnException = new PluginDependOnException( + String.format("Error! [%s] plugin missing [%s] plugin ! Please Check~",sonName,fatherName),fatherName,sonName); + ResultCode code = ResultCode.PLUGIN_DEPEND_ERROR; + code.setMsg(pluginDependOnException.msg); + pluginDependOnException.setResultCode(code); + return pluginDependOnException; + } + + public static PluginDependOnException DependOnPlugin(String fatherName,String sonName){ + PluginDependOnException pluginDependOnException = new PluginDependOnException( + String.format("Error! [%s] plugin depend on [%s] plugin !",sonName,fatherName),fatherName,sonName); + ResultCode code = ResultCode.PLUGIN_DEPEND_ERROR; + code.setMsg(pluginDependOnException.msg); + pluginDependOnException.setResultCode(code); + return pluginDependOnException; + } + + public String getFatherName() { + return fatherName; + } + + public String getSonName() { + return sonName; + } +} diff --git a/common/src/main/java/org/example/exception/plugin/PluginException.java b/common/src/main/java/org/example/exception/plugin/PluginException.java new file mode 100644 index 0000000..490f5f7 --- /dev/null +++ b/common/src/main/java/org/example/exception/plugin/PluginException.java @@ -0,0 +1,22 @@ +package org.example.exception.plugin; + +import org.example.exception.ChopperBotException; + +/** + * @author Genius + * @date 2023/08/02 16:50 + **/ +public class PluginException extends ChopperBotException { + + public PluginException(String message) { + super(message); + } + + + + @Override + public String getMessage() { + return super.getMessage(); + } + +} diff --git a/common/src/main/java/org/example/exception/plugin/PluginNotRegisterException.java b/common/src/main/java/org/example/exception/plugin/PluginNotRegisterException.java new file mode 100644 index 0000000..f36f9d9 --- /dev/null +++ b/common/src/main/java/org/example/exception/plugin/PluginNotRegisterException.java @@ -0,0 +1,15 @@ +package org.example.exception.plugin; + +import org.example.exception.Impl.ResultCode; + +/** + * @author Genius + * @date 2023/08/02 16:59 + **/ +public class PluginNotRegisterException extends PluginException{ + + public PluginNotRegisterException() { + super("Error! Plugin Not Register!"); + resultCode = ResultCode.PLUGIN_NOT_REGISTER; + } +} diff --git a/common/src/main/java/org/example/init/CommonInitMachine.java b/common/src/main/java/org/example/init/CommonInitMachine.java index 92e826e..ea3c9bb 100644 --- a/common/src/main/java/org/example/init/CommonInitMachine.java +++ b/common/src/main/java/org/example/init/CommonInitMachine.java @@ -7,11 +7,9 @@ package org.example.init; import org.example.log.ResultLogger; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; +import org.example.plugin.annotation.Plugin; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; @@ -68,7 +66,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine, ResultL public boolean checkNeedPlugin() { for (String needPlugin : needPlugins) { if(!InitPluginRegister.registerPluginTable.containsKey(needPlugin)){ - fail(String.format("Missing {%s} plugin,please check your plugin init!",needPlugin)); + fail(String.format("Missing [%s] plugin,please check your plugin init!",needPlugin)); return false; } } @@ -94,7 +92,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine, ResultL @Override public void successLog() { - successLog(String.format("[✔] {%s} init success!",pluginName)); + successLog(String.format("[✔] [%s] init success!",pluginName)); } @Override public void successLog(String str) { @@ -102,7 +100,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine, ResultL } @Override public void failLog() { - failLog(String.format("[❌] {%s} init error!",pluginName)); + failLog(String.format("[❌] [%s] init error!",pluginName)); } @Override public void failLog(String str) { @@ -130,7 +128,7 @@ public abstract class CommonInitMachine implements ComponentInitMachine, ResultL } private void shutdownLog(){ - logger.info("[🆖] {} close success.",pluginName); + logger.info("[🆖] [{}] close success.",pluginName); } @Override diff --git a/common/src/main/java/org/example/init/InitPluginRegister.java b/common/src/main/java/org/example/init/InitPluginRegister.java index ffe9b98..1d027bd 100644 --- a/common/src/main/java/org/example/init/InitPluginRegister.java +++ b/common/src/main/java/org/example/init/InitPluginRegister.java @@ -6,17 +6,19 @@ package org.example.init; **/ import org.example.constpool.ConstPool; +import org.example.exception.plugin.PluginDependOnException; +import org.example.log.ChopperLogFactory; +import org.example.log.LoggerType; import org.example.plugin.CommonPlugin; -import org.example.plugin.Plugin; +import org.example.plugin.annotation.Plugin; import org.example.util.ClassUtil; -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; /** * 插件注册中心 @@ -32,7 +34,7 @@ public class InitPluginRegister { public static ConcurrentHashMap allPlugins = new ConcurrentHashMap<>(); public static ConcurrentHashMap registerPluginTable = new ConcurrentHashMap<>(); - public static Map pluginSetting; + public static Map pluginStartSetting; public static boolean initPluginRegister(){ Set> initMachineClasses = ClassUtil.getAnnotationClass(ConstPool.PROJECT_PATH + ".init", Plugin.class); @@ -42,14 +44,17 @@ public class InitPluginRegister { String moduleName = ano.moduleName(); String pluginName = ano.pluginName(); List needPlugins = List.of(ano.needPlugin()); - boolean autoStart = pluginSetting.containsKey(pluginName)?pluginSetting.get(pluginName):ano.autoStart(); + boolean autoStart = pluginStartSetting.containsKey(pluginName)?pluginStartSetting.get(pluginName):ano.autoStart(); CommonInitMachine initMachine = (CommonInitMachine) initMachineClass .getDeclaredConstructor(List.class,boolean.class,String.class,String.class,Class.class) .newInstance(needPlugins,autoStart,moduleName,pluginName,ano.pluginClass()); + pluginStartSetting.put(pluginName,autoStart); + //全部插件 if(allPlugins.containsKey(pluginName))return false; allPlugins.put(pluginName,initMachine); + //模块下的所有插件 if(modulePlugin.containsKey(moduleName)){ modulePlugin.get(moduleName).add(pluginName); }else{ @@ -57,6 +62,8 @@ public class InitPluginRegister { list.add(pluginName); modulePlugin.put(moduleName,list); } + + //所有模块的插件依赖 if(!fatherAndSonPlugin.containsKey(pluginName)){ fatherAndSonPlugin.put(pluginName,new ArrayList<>()); } @@ -73,6 +80,7 @@ public class InitPluginRegister { return false; } } + //完善 模块下插件的依赖和一次插件初始化检测 try { allPlugins.forEach( (k,v)->{ @@ -93,11 +101,54 @@ public class InitPluginRegister { } ); + AtomicBoolean res = new AtomicBoolean(true); + InitPluginRegister.allPlugins.forEach( + (k,v)->{ + if(!InitPluginRegister.needStart(k)){ + List list = fatherAndSonPlugin.get(k); + if(list!=null){ + for (String sonPlugin : list) { + if(InitPluginRegister.needStart(sonPlugin)){ + ChopperLogFactory.getLogger(LoggerType.System) + .error("🙃ChopperBot Plugin Error!The [{}] plugin requires the [{}] plugin, " + + "but the [{}] plugin is not set to start automatically. " + + "Please go to the ./config/chopperBotConfig.json to start the [{}] plugin !",sonPlugin,k,k,k); + res.set(false); + break; + } + } + } + } + } + ); + return res.get(); }catch (Exception e){ - e.printStackTrace(); + return false; } - return true; + } + + public static boolean startPlugin(String pluginName){ + if(allPlugins.containsKey(pluginName)){ + if(!registerPluginTable.containsKey(pluginName)){ + CommonInitMachine initMachine = allPlugins.get(pluginName); + for (String needPlugin : initMachine.getNeedPlugins()) { + if(!InitPluginRegister.isRegister(needPlugin)){ + throw PluginDependOnException.MissingFatherPlugin(needPlugin,pluginName); + } + } + if (initMachine.init()) { + registerPluginTable.put(pluginName,initMachine); + initMachine.afterInit(); + return true; + }else{ + return false; + } + }else{ + return false; + } + } + return false; } @@ -110,11 +161,14 @@ public class InitPluginRegister { if (registerPluginTable.containsKey(pluginName)) { for (String plugin : fatherAndSonPlugin.get(pluginName)) { if (registerPluginTable.containsKey(plugin)) { - return false; + throw PluginDependOnException.DependOnPlugin(pluginName,plugin); + } } registerPluginTable.get(pluginName).shutdown(); registerPluginTable.remove(pluginName); + }else{ + return false; } return true; } @@ -134,4 +188,8 @@ public class InitPluginRegister { return null; } + public static boolean needStart(String pluginName){ + return pluginStartSetting.getOrDefault(pluginName, false); + } + } diff --git a/common/src/main/java/org/example/init/ModuleInitMachine.java b/common/src/main/java/org/example/init/ModuleInitMachine.java index 117fe08..608a017 100644 --- a/common/src/main/java/org/example/init/ModuleInitMachine.java +++ b/common/src/main/java/org/example/init/ModuleInitMachine.java @@ -19,13 +19,15 @@ public abstract class ModuleInitMachine extends CommonInitMachine{ protected List initMachines = new ArrayList<>(); + protected List allInitMachines; + private String moduleName; @Override public boolean checkNeedPlugin() { for (String needPlugin : needPlugins) { - if(!InitPluginRegister.registerPluginTable.containsKey(needPlugin)){ + if(!InitPluginRegister.isRegister(needPlugin)){ fail(String.format("Missing {%s} module,please check your module init!",needPlugin)); return false; } @@ -41,7 +43,12 @@ public abstract class ModuleInitMachine extends CommonInitMachine{ @Override public boolean init() { try { - initMachines = PluginUtil.getModuleAllPluginInit(moduleName); + allInitMachines = PluginUtil.getModuleAllPluginInit(moduleName); + for (CommonInitMachine initMachine : allInitMachines) { + if(InitPluginRegister.needStart(initMachine.pluginName)){ + initMachines.add(initMachine); + } + } } catch (Exception e) { return fail(e.getMessage()); } @@ -49,16 +56,17 @@ public abstract class ModuleInitMachine extends CommonInitMachine{ return initLogger(()->{ if(checkNeedPlugin()){ for (CommonInitMachine initMachine : initMachines) { - if (initMachine.checkNeedPlugin()) { - initMachine.setLogger(logger); - if(!initMachine.init()){ - return fail(); + + if (initMachine.checkNeedPlugin()) { + initMachine.setLogger(logger); + if(!initMachine.init()){ + return fail(); + } + initMachine.registerPlugin(); + }else{ + return false; } - initMachine.registerPlugin(); - }else{ - return false; } - } return success(); } return false; diff --git a/common/src/main/java/org/example/plugin/GuardPlugin.java b/common/src/main/java/org/example/plugin/GuardPlugin.java index 9136fd6..09fad19 100644 --- a/common/src/main/java/org/example/plugin/GuardPlugin.java +++ b/common/src/main/java/org/example/plugin/GuardPlugin.java @@ -22,6 +22,7 @@ public abstract class GuardPlugin extends CommonPlugin implements ChopperBotGuar @Override public boolean init() { + isStop = false; if(!afterDo) this.guardian(); return true; } diff --git a/common/src/main/java/org/example/plugin/annotation/CheckPlugin.java b/common/src/main/java/org/example/plugin/annotation/CheckPlugin.java new file mode 100644 index 0000000..371da59 --- /dev/null +++ b/common/src/main/java/org/example/plugin/annotation/CheckPlugin.java @@ -0,0 +1,18 @@ +package org.example.plugin.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * api需要哪些插件的注解 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface CheckPlugin { + + String[] needPlugin() default {}; + +} diff --git a/common/src/main/java/org/example/plugin/Plugin.java b/common/src/main/java/org/example/plugin/annotation/Plugin.java similarity index 84% rename from common/src/main/java/org/example/plugin/Plugin.java rename to common/src/main/java/org/example/plugin/annotation/Plugin.java index de5faec..7593908 100644 --- a/common/src/main/java/org/example/plugin/Plugin.java +++ b/common/src/main/java/org/example/plugin/annotation/Plugin.java @@ -1,4 +1,6 @@ -package org.example.plugin; +package org.example.plugin.annotation; + +import org.example.plugin.CommonPlugin; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/common/src/main/java/org/example/util/Result.java b/common/src/main/java/org/example/util/Result.java index b5a1689..afaeae1 100644 --- a/common/src/main/java/org/example/util/Result.java +++ b/common/src/main/java/org/example/util/Result.java @@ -8,6 +8,7 @@ package org.example.util; import org.example.exception.Impl.ResultCode; import java.io.Serializable; +import java.util.Map; /** * @author welsir @@ -59,6 +60,18 @@ public class Result implements Serializable { return result; } + public static Result error(ResultCode resultCode) + { + return error(resultCode, Map.of()); + } + + public static Result error(ResultCode resultCode,Object data){ + Result result = new Result(); + result.setCode(resultCode.getCode()); + result.setMsg(resultCode.getMsg()); + result.setData(data); + return result; + } public Object getData() { return data; diff --git a/console/src/main/java/org/example/bean/Plugin.java b/console/src/main/java/org/example/bean/Plugin.java new file mode 100644 index 0000000..50ec9a1 --- /dev/null +++ b/console/src/main/java/org/example/bean/Plugin.java @@ -0,0 +1,20 @@ +package org.example.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +/** + * @author Genius + * @date 2023/08/02 18:17 + **/ +@Data +@AllArgsConstructor +public class Plugin { + private String pluginName; + private String pluginModule; + private List needPlugins; + private boolean isStart; + private boolean isRegister; +} diff --git a/console/src/main/java/org/example/controller/HotController.java b/console/src/main/java/org/example/controller/HotController.java index 03c79dd..b7cbb77 100644 --- a/console/src/main/java/org/example/controller/HotController.java +++ b/console/src/main/java/org/example/controller/HotController.java @@ -6,7 +6,9 @@ import org.example.bean.Live; import org.example.bean.HotModule; import org.example.bean.hotmodule.HotModuleList; import org.example.constpool.ConstPool; +import org.example.constpool.PluginName; import org.example.core.HotModuleDataCenter; +import org.example.plugin.annotation.CheckPlugin; import org.example.service.HotModuleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -27,6 +29,7 @@ public class HotController { @Autowired HotModuleService hotModuleService; + @CheckPlugin(needPlugin = {PluginName.HOT_GUARD_PLUGIN}) @GetMapping("/douyu/allHotLive") public Result getDouyuAllHotLive(@RequestParam(defaultValue = "0") int latest){ List lives; @@ -38,6 +41,7 @@ public class HotController { return Result.success(lives); } + @CheckPlugin(needPlugin = {PluginName.HOT_GUARD_PLUGIN}) @GetMapping("/douyu/allHotModule") public Result getDouyuAllHotModule(@RequestParam(defaultValue = "0") int latest){ HotModuleList hotModuleList; @@ -49,6 +53,7 @@ public class HotController { return Result.success(hotModuleList.getHotModuleList()); } + @CheckPlugin(needPlugin = {PluginName.HOT_GUARD_PLUGIN}) @GetMapping("/douyu/getHotModuleLives") public Result getDouyuHotModuleLives(@RequestParam int moduleId){ HotModule moduleHotLives = hotModuleService.getModuleHotLives(ConstPool.PLATFORM.DOUYU.getName(), moduleId); diff --git a/console/src/main/java/org/example/controller/PluginController.java b/console/src/main/java/org/example/controller/PluginController.java new file mode 100644 index 0000000..8f5507b --- /dev/null +++ b/console/src/main/java/org/example/controller/PluginController.java @@ -0,0 +1,52 @@ +package org.example.controller; + +import org.example.bean.Plugin; +import org.example.init.InitPluginRegister; +import org.example.service.PluginService; +import org.example.util.Result; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author Genius + * @date 2023/08/01 23:39 + **/ +@RestController +@RequestMapping("/plugin") +public class PluginController { + + @Resource + PluginService pluginService; + + @GetMapping("/get") + public Result getPlugin(){ + return Result.success(pluginService.getPlugins(null)); + } + + @GetMapping("/get/{moduleName}") + public Result getPlugin(@PathVariable(required = false) String moduleName){ + return Result.success(pluginService.getPlugins(moduleName)); + } + + @GetMapping("/close") + public Result closePlugin(@RequestParam("plugin")String plugin){ + boolean res = InitPluginRegister.closePlugin(plugin); + if(!res){ + return Result.error(plugin+" not register"); + } + return Result.success(Map.of("plugin",plugin)); + } + + @GetMapping("/start") + public Result startPlugin(@RequestParam("plugin")String plugin){ + if (InitPluginRegister.startPlugin(plugin)) { + return Result.success(Map.of("plugin",plugin)); + } + return Result.error("The plugin has been started or does not exist"); + } + +} diff --git a/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java b/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java index e44f726..c0e9719 100644 --- a/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java +++ b/console/src/main/java/org/example/init/ChopperBotConfigFileInitMachine.java @@ -2,11 +2,13 @@ package org.example.init; import com.alibaba.fastjson.JSONObject; import org.example.cache.FileCache; +import org.example.constpool.ConstPool; import org.example.constpool.GlobalFileCache; +import org.example.constpool.ModuleName; import org.example.constpool.PluginName; import org.example.log.ChopperLogFactory; import org.example.log.LoggerType; -import org.example.pojo.configfile.ModuleSrcConfigFile; +import org.example.pojo.configfile.ChopperBotConfigFile; import org.example.util.FileUtil; import org.example.util.JsonFileUtil; import org.springframework.stereotype.Component; @@ -25,14 +27,18 @@ import java.util.Map; @Component public class ChopperBotConfigFileInitMachine extends CommonInitMachine { - ModuleSrcConfigFile moduleSrcConfigFile; + ChopperBotConfigFile chopperBotConfigFile; private boolean initFlag; public ChopperBotConfigFileInitMachine() { - super( PluginName.MODULE_CONFIG_PLUGIN,ChopperLogFactory.getLogger(LoggerType.System)); - moduleSrcConfigFile = new ModuleSrcConfigFile(); + super(ModuleName.CHOPPER_BOT,ChopperLogFactory.getLogger(LoggerType.System)); + chopperBotConfigFile = new ChopperBotConfigFile(); initFlag = true; + isAutoStart = true; + pluginName = PluginName.CHOPPER_BOT_CONFIG_PLUGIN; + pluginClass = ChopperBotConfigFile.class; + plugin = chopperBotConfigFile; } @@ -40,13 +46,13 @@ public class ChopperBotConfigFileInitMachine extends CommonInitMachine { @PostConstruct @Override public boolean init() { - Path dir = Paths.get(moduleSrcConfigFile.getFilePath()); + Path dir = Paths.get(chopperBotConfigFile.getFilePath()); if (!createConfigDirectory(dir)) { initFlag = fail("./config directory"); return initFlag; } if (!createConfigFile(dir)) { - initFlag = fail(ModuleSrcConfigFile.getFullPath()); + initFlag = fail(ChopperBotConfigFile.getFullPath()); return initFlag; } if (!createModuleDirectory()) { @@ -68,18 +74,20 @@ public class ChopperBotConfigFileInitMachine extends CommonInitMachine { } private boolean createConfigFile(Path dir) { - Path path = Paths.get(dir.toString(), moduleSrcConfigFile.getFileName()); + Path path = Paths.get(dir.toString(), chopperBotConfigFile.getFileName()); try { if (!Files.exists(path)) { - JsonFileUtil.writeJsonFile(path.toString(),moduleSrcConfigFile.packageConfig()); - GlobalFileCache.ModuleSrcConfigFile = new FileCache(moduleSrcConfigFile); + JsonFileUtil.writeJsonFile(path.toString(), chopperBotConfigFile.packageConfig()); + GlobalFileCache.ModuleSrcConfigFile = new FileCache(chopperBotConfigFile); }else{ - Map data = JsonFileUtil.readJsonFile(Paths.get(dir.toString(), moduleSrcConfigFile.getFileName()).toString()); - moduleSrcConfigFile.setData(data); - GlobalFileCache.ModuleSrcConfigFile = new FileCache(moduleSrcConfigFile); + Map data = JsonFileUtil.readJsonFile(Paths.get(dir.toString(), chopperBotConfigFile.getFileName()).toString()); + chopperBotConfigFile.setData(data); + GlobalFileCache.ModuleSrcConfigFile = new FileCache(chopperBotConfigFile); } - InitPluginRegister.pluginSetting = JSONObject.parseObject(GlobalFileCache.ModuleSrcConfigFile.get("plugin").toString(),Map.class); - InitPluginRegister.allPlugins.put(PluginName.MODULE_CONFIG_PLUGIN,this); + + //全局插件注册表插入 + InitPluginRegister.pluginStartSetting = JSONObject.parseObject(GlobalFileCache.ModuleSrcConfigFile.get("pluginStart").toString(),Map.class); + InitPluginRegister.allPlugins.put(PluginName.CHOPPER_BOT_CONFIG_PLUGIN,this); }catch (Exception e) { return false; } diff --git a/console/src/main/java/org/example/init/InitWorld.java b/console/src/main/java/org/example/init/InitWorld.java index 87dfcd3..e8bea08 100644 --- a/console/src/main/java/org/example/init/InitWorld.java +++ b/console/src/main/java/org/example/init/InitWorld.java @@ -1,6 +1,8 @@ package org.example.init; -import org.example.plugin.Plugin; +import org.example.constpool.GlobalFileCache; +import org.example.exception.FileCacheException; +import org.example.plugin.annotation.Plugin; import org.example.util.ClassUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; @@ -9,8 +11,8 @@ import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import static org.example.constpool.ConstPool.PROJECT_PATH; @@ -35,14 +37,20 @@ public class InitWorld { } + private void initPluginStartSetting() throws FileCacheException, InterruptedException { + GlobalFileCache.ModuleSrcConfigFile.write(InitPluginRegister.pluginStartSetting,"pluginStart"); + } @PostConstruct private void init(){ - InitPluginRegister.initPluginRegister(); + if (!InitPluginRegister.initPluginRegister()) { + close(); + } if(moduleSrcConfigFileInitMachine.isInitFlag()){ WorldInitMachine world = null; try { + initPluginStartSetting(); world = new WorldInitMachine(); } catch (Exception e) { close(); diff --git a/console/src/main/java/org/example/init/WorldInitMachine.java b/console/src/main/java/org/example/init/WorldInitMachine.java index a09cc43..3cbf288 100644 --- a/console/src/main/java/org/example/init/WorldInitMachine.java +++ b/console/src/main/java/org/example/init/WorldInitMachine.java @@ -42,18 +42,18 @@ public class WorldInitMachine extends ModuleInitMachine{ } (initMachine).registerPlugin(); }else{ - return false; + return fail(); } } registerPlugin(); return success(); } - return false; + return fail(); }); } catch (Exception e) { - throw new RuntimeException(e); + return fail(e.getMessage()); } } diff --git a/console/src/main/java/org/example/service/PluginService.java b/console/src/main/java/org/example/service/PluginService.java new file mode 100644 index 0000000..28f7d59 --- /dev/null +++ b/console/src/main/java/org/example/service/PluginService.java @@ -0,0 +1,10 @@ +package org.example.service; + +import org.example.bean.Plugin; + +import java.util.List; + +public interface PluginService { + + List getPlugins(String moduleName); +} diff --git a/console/src/main/java/org/example/service/impl/PluginServiceImpl.java b/console/src/main/java/org/example/service/impl/PluginServiceImpl.java new file mode 100644 index 0000000..5193e90 --- /dev/null +++ b/console/src/main/java/org/example/service/impl/PluginServiceImpl.java @@ -0,0 +1,52 @@ +package org.example.service.impl; + +import org.example.bean.Plugin; +import org.example.init.CommonInitMachine; +import org.example.init.InitPluginRegister; +import org.example.service.PluginService; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Genius + * @date 2023/08/02 18:28 + **/ +@Service +public class PluginServiceImpl implements PluginService { + + @Override + public List getPlugins(String moduleName) { + List list = new ArrayList<>(); + Map commonInitMachineMap; + if(moduleName==null){ + commonInitMachineMap = InitPluginRegister.allPlugins; + }else{ + commonInitMachineMap = new HashMap<>(); + for (String s : InitPluginRegister.modulePlugin.get(moduleName)) { + commonInitMachineMap.put(s,InitPluginRegister.allPlugins.get(s)); + } + } + + commonInitMachineMap.forEach( + (k,v)->{ + list.add(packToPlugin(v)); + } + ); + + return list; + } + + + private Plugin packToPlugin(CommonInitMachine commonInitMachine){ + String moduleName = commonInitMachine.getModuleName(); + String pluginName = commonInitMachine.getPluginName(); + boolean autoStart = commonInitMachine.isAutoStart(); + boolean register = InitPluginRegister.isRegister(pluginName); + List needPlugins = commonInitMachine.getNeedPlugins(); + return new Plugin(pluginName,moduleName,needPlugins,autoStart,register); + } +} diff --git a/doc/docs/07.技术文档/01.模块文档/02.FileModule/02.FileCache.md b/doc/docs/07.技术文档/01.模块文档/02.FileModule/02.FileCache.md index 107c2b4..24dc309 100644 --- a/doc/docs/07.技术文档/01.模块文档/02.FileModule/02.FileCache.md +++ b/doc/docs/07.技术文档/01.模块文档/02.FileModule/02.FileCache.md @@ -23,8 +23,8 @@ article: false 1. 先创建一个类继承[ConfigFile](/pages/e3a691/#configfile)类 2. 创建一个FileCache类并将ConfigFile放入Cache中即可 ```java -ModuleSrcConfigFile moduleSrcConfigFile = new ModuleSrcConfigFile(); -FileCache fileCache = new FileCache(moduleSrcConfigFile); +ModuleSrcConfigFile chopperBotConfigFile = new ModuleSrcConfigFile(); +FileCache fileCache = new FileCache(chopperBotConfigFile); ``` | 构造方法参数 | 说明 | | :-: | :-: |