From 2875426f7284112858579262ea0832c13615b9a6 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 5 Nov 2020 00:58:07 +0800 Subject: [PATCH] =?UTF-8?q?[Add][Change]=20Framework-API,=20Core=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20FrameworkContext=20=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E5=90=91=20Framework=20=E4=BC=A0=E9=80=92=E4=B8=8E=20Bot=20?= =?UTF-8?q?=E6=9C=89=E5=85=B3=E7=9A=84=E7=9B=B8=E5=85=B3=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] FrameworkContext, DefaultFrameworkContext 添加 FrameworkContext 和一个默认实现; [Change] Framework, FrameworkFactory 调整代码以支持传递 FrameworkContext; --- .../framework/DefaultFrameworkContext.java | 47 +++++++++++++++++++ .../cgj/bot/framework/FrameworkFactory.java | 16 +++++-- .../lamgc/cgj/bot/framework/Framework.java | 11 ++++- .../cgj/bot/framework/FrameworkContext.java | 45 ++++++++++++++++++ 4 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/DefaultFrameworkContext.java create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/FrameworkContext.java diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/DefaultFrameworkContext.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/DefaultFrameworkContext.java new file mode 100644 index 0000000..d97996c --- /dev/null +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/DefaultFrameworkContext.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 LamGC + * + * ContentGrabbingJi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ContentGrabbingJi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.lamgc.cgj.bot.framework; + +import net.lamgc.cgj.bot.cache.CacheStoreBuilder; +import net.lamgc.cgj.bot.event.EventExecutor; + +/** + * 框架上下文的默认实现. + * @author LamGC + */ +class DefaultFrameworkContext implements FrameworkContext { + + private final EventExecutor eventExecutor; + private final CacheStoreBuilder cacheStoreBuilder; + + public DefaultFrameworkContext(EventExecutor eventExecutor, CacheStoreBuilder cacheStoreBuilder) { + this.eventExecutor = eventExecutor; + + this.cacheStoreBuilder = cacheStoreBuilder; + } + + @Override + public EventExecutor getEventExecutor() { + return eventExecutor; + } + + @Override + public CacheStoreBuilder getCacheStoreBuilder() { + return cacheStoreBuilder; + } +} diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/FrameworkFactory.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/FrameworkFactory.java index 2699819..0605afa 100644 --- a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/FrameworkFactory.java +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/FrameworkFactory.java @@ -17,6 +17,8 @@ package net.lamgc.cgj.bot.framework; +import net.lamgc.cgj.bot.cache.CacheStoreBuilder; +import net.lamgc.cgj.bot.event.EventExecutor; import org.pf4j.Plugin; import org.pf4j.PluginFactory; import org.pf4j.PluginWrapper; @@ -36,9 +38,13 @@ final class FrameworkFactory implements PluginFactory { private final static Logger log = LoggerFactory.getLogger(FrameworkFactory.class); private final File dataRootFolder; + private final CacheStoreBuilder cacheStoreBuilder; + private final EventExecutor eventExecutor; - public FrameworkFactory(File dataRootFolder) { + public FrameworkFactory(File dataRootFolder, CacheStoreBuilder cacheStoreBuilder, EventExecutor eventExecutor) { this.dataRootFolder = dataRootFolder; + this.cacheStoreBuilder = cacheStoreBuilder; + this.eventExecutor = eventExecutor; if (!this.dataRootFolder.exists() && !this.dataRootFolder.mkdirs()) { log.warn("框架数据目录创建异常, 可能会导致后续框架存取数据失败!"); } @@ -67,9 +73,11 @@ final class FrameworkFactory implements PluginFactory { try { // (PluginWrapper, DataFolder) - Constructor constructor = pluginClass.getConstructor(PluginWrapper.class, File.class); - return (Plugin) constructor.newInstance(pluginWrapper, - new File(dataRootFolder, pluginWrapper.getPluginId())); + Constructor constructor = pluginClass + .getConstructor(PluginWrapper.class, File.class, FrameworkContext.class); + return (Framework) constructor.newInstance(pluginWrapper, + new File(dataRootFolder, pluginWrapper.getPluginId()), + new DefaultFrameworkContext(eventExecutor, cacheStoreBuilder)); } catch (Exception e) { log.error(e.getMessage(), e); } diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java index d020ee5..ec94aa0 100644 --- a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java @@ -31,6 +31,7 @@ import java.io.File; public abstract class Framework extends Plugin { private final File dataFolder; + private final FrameworkContext context; /** * 由 FrameworkManager 执行的构造方法. @@ -38,8 +39,9 @@ public abstract class Framework extends Plugin { * * @param wrapper 包含框架运行期间需要使用对象的包装器. */ - public Framework(PluginWrapper wrapper, File dataFolder) { + public Framework(PluginWrapper wrapper, File dataFolder, FrameworkContext context) { super(wrapper); + this.context = context; if (!(wrapper.getDescriptor() instanceof FrameworkDescriptor)) { throw new IllegalStateException("Invalid description object"); } @@ -86,4 +88,11 @@ public abstract class Framework extends Plugin { throw new IllegalStateException("无法转换 Descriptor 的类型, 框架管理器可能遭到修改!"); } + /** + * 获取当前框架对象与所属 ContentGrabbingJiBot 的上下文. + * @return 返回上下文对象. + */ + protected FrameworkContext getContext() { + return context; + } } diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/FrameworkContext.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/FrameworkContext.java new file mode 100644 index 0000000..c645dba --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/FrameworkContext.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2020 LamGC + * + * ContentGrabbingJi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ContentGrabbingJi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.lamgc.cgj.bot.framework; + +import net.lamgc.cgj.bot.cache.CacheStoreBuilder; +import net.lamgc.cgj.bot.event.EventExecutor; + +/** + * 框架上下文对象. + *

由于 ContentGrabbingJi 按实例分配资源, + * 故 Context 可获取专属于框架所属实例的相关可用资源以供框架使用. + * @author LamGC + */ +public interface FrameworkContext { + + /** + * 获取事件执行器. + *

事件执行器用于将事件提交给 CGJ 进行处理. + * @return 返回事件执行器. + */ + EventExecutor getEventExecutor(); + + /** + * 获取缓存存储容器构造器. + *

如需使用缓存, 可通过 {@link CacheStoreBuilder} 获取独立的缓存容器. + * @return 返回构造器. + */ + CacheStoreBuilder getCacheStoreBuilder(); + +}