From 68f13a38c1f5ec02972be3e24f0eaf476c361c44 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sun, 21 Feb 2021 20:29:33 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=20Framework-API=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E7=BB=99=E5=BA=94=E7=94=A8=E5=86=85=E9=83=A8?= =?UTF-8?q?(=E6=88=96=E5=91=BD=E4=BB=A4=E6=89=A9=E5=B1=95)=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E4=B8=BB=E5=8A=A8=E8=8E=B7=E5=8F=96=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=20Sender=20=E7=9A=84=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] SenderFactory 添加用于主动获取 Sender 的扩展接口; [Add] NoFoundSenderException 添加相关异常; --- .../bot/framework/NoFoundSenderException.java | 58 +++++++++++++++++++ .../cgj/bot/framework/SenderFactory.java | 46 +++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/NoFoundSenderException.java create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/SenderFactory.java diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/NoFoundSenderException.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/NoFoundSenderException.java new file mode 100644 index 0000000..e05a262 --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/NoFoundSenderException.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2021 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. + * + * 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.framework.message.MessageSource; + +/** + * 无对应消息源异常. + *

当 {@link SenderFactory} 无法通过传入的 {@link MessageSource} 和 Id 找到对应消息源时, 将抛出本异常. + * @see SenderFactory + */ +public class NoFoundSenderException extends RuntimeException { + + private final MessageSource source; + private final long id; + + /** + * 构造异常. + * @param source 传入的消息源类型. + * @param id 传入的消息源 Id. + */ + public NoFoundSenderException(MessageSource source, long id) { + super("Source Type: " + source + ", id: " + id); + this.source = source; + this.id = id; + } + + /** + * 获取引发该异常时传入的 {@link MessageSource} 类型. + * @return 返回引发该异常时提供的 MessageSource. + */ + public MessageSource getSource() { + return source; + } + + /** + * 获取引发该异常时传入的消息源 Id. + * @return 返回引发该异常时提供的消息源 Id. + */ + public long getId() { + return id; + } +} diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/SenderFactory.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/SenderFactory.java new file mode 100644 index 0000000..52445a8 --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/SenderFactory.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2021 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. + * + * 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.framework.message.MessageSender; +import net.lamgc.cgj.bot.framework.message.MessageSource; +import org.pf4j.ExtensionPoint; + +/** + * 消息源发送器构造工厂. + *

该接口实现将由对应框架实现, 每个框架仅允许实现一个. + * @author LamGC + */ +public interface SenderFactory extends ExtensionPoint { + + /** + * 获取所属平台. + * @return 返回平台信息对象. + */ + Platform getPlatform(); + + /** + * 获取发送器. + * @param source 消息源类型. + * @param id 消息源 Id. + * @return 返回消息发送器, 本方法不允许返回 null. + * @throws NoFoundSenderException 当无法获取对应的消息源发送器时, 将抛出该异常. + */ + MessageSender getSender(MessageSource source, long id) throws NoFoundSenderException; + +}