[Change] 调整Framework接口, 为框架提供特定数据存储目录;

[Change] Framework 适配FrameworkResources的更改, 将`getName`调整为`getIdentify`, 增加`getFrameworkName`方法用于获取框架名(可能会改为注解方式以防止更改);
[Change] FrameworkManager 适配Framework的更改, 增加`checkFramework`方法以对FrameworkName进行检查;
[Change] FrameworkManager, FrameworkResources 将FrameworkResources从FrameworkManager分离成单独的类;
[Change] ConsoleMain, MiraiMain, SpringCQApplication 适配相关更改;
This commit is contained in:
LamGC 2020-07-15 10:57:30 +08:00
parent 9a7d16124a
commit 6fc7d8ad78
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
6 changed files with 86 additions and 37 deletions

View File

@ -6,9 +6,9 @@ public interface Framework {
* 框架初始化方法 * 框架初始化方法
* @param resources 框架所分配到的资源. * @param resources 框架所分配到的资源.
* @throws Exception 当框架抛出异常时, 将不会继续运行框架. * @throws Exception 当框架抛出异常时, 将不会继续运行框架.
* @see FrameworkManager.FrameworkResources * @see FrameworkResources
*/ */
void init(FrameworkManager.FrameworkResources resources) throws Exception; void init(FrameworkResources resources) throws Exception;
/** /**
* 框架运行方法 * 框架运行方法
@ -23,10 +23,19 @@ public interface Framework {
void close() throws Exception; void close() throws Exception;
/** /**
* 获取框架标识名 * 获取框架标识名.
* @return 返回标识名 * <p>可根据需要自行调整框架标识名.</p>
* @return 返回标识名.
*/ */
default String getName() { default String getIdentify() {
return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode()); return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode());
} }
/**
* 获取框架名称.
* <p>框架名称不可更改.</p>
* @return 返回框架名称.
*/
String getFrameworkName();
} }

View File

@ -1,12 +1,9 @@
package net.lamgc.cgj.bot.framework; package net.lamgc.cgj.bot.framework;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap; import java.util.*;
import java.util.HashSet; import java.util.regex.Pattern;
import java.util.Map;
import java.util.Set;
public final class FrameworkManager { public final class FrameworkManager {
@ -22,15 +19,23 @@ public final class FrameworkManager {
} }
public static Thread registerFramework(Framework framework) { public static Thread registerFramework(Framework framework) {
checkFramework(framework);
FrameworkResources resources = new FrameworkResources(framework); FrameworkResources resources = new FrameworkResources(framework);
resourcesMap.put(framework, resources); resourcesMap.put(framework, resources);
Thread frameworkThread = new Thread(resources.getFrameworkThreadGroup(), Thread frameworkThread = new Thread(resources.getFrameworkThreadGroup(),
() -> FrameworkManager.runFramework(framework), "FrameworkThread-" + framework.getName()); () -> FrameworkManager.runFramework(framework), "FrameworkThread-" + framework.getIdentify());
frameworkThread.start(); frameworkThread.start();
return frameworkThread; return frameworkThread;
} }
private static final Pattern FRAMEWORK_NAME_CHECK_PATTERN = Pattern.compile("^[A-Za-z0-9_\\-$]+$");
private static void checkFramework(Framework framework) {
if(!FRAMEWORK_NAME_CHECK_PATTERN.matcher(framework.getFrameworkName()).matches()) {
throw new IllegalStateException("Invalid Framework Name: " + framework.getFrameworkName());
}
}
public static Set<Framework> frameworkSet() { public static Set<Framework> frameworkSet() {
return new HashSet<>(resourcesMap.keySet()); return new HashSet<>(resourcesMap.keySet());
} }
@ -51,6 +56,10 @@ public final class FrameworkManager {
} }
} }
static ThreadGroup getFrameworkRootGroup() {
return frameworkRootGroup;
}
private static void runFramework(Framework framework) { private static void runFramework(Framework framework) {
FrameworkResources frameworkResources = resourcesMap.get(framework); FrameworkResources frameworkResources = resourcesMap.get(framework);
try { try {
@ -63,24 +72,4 @@ public final class FrameworkManager {
} }
} }
public static class FrameworkResources {
private final ThreadGroup frameworkThreadGroup;
private final Logger logger;
public FrameworkResources(Framework framework) {
frameworkThreadGroup = new ThreadGroup(frameworkRootGroup, "Framework-" + framework.getName());
logger = LoggerFactory.getLogger("Framework-" + framework.getName());
}
ThreadGroup getFrameworkThreadGroup() {
return frameworkThreadGroup;
}
public Logger getLogger() {
return logger;
}
}
} }

View File

@ -0,0 +1,35 @@
package net.lamgc.cgj.bot.framework;
import net.lamgc.cgj.bot.boot.BotGlobal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public class FrameworkResources {
private final static File frameworkDataStoreRootDir = new File(BotGlobal.getGlobal().getDataStoreDir(),
"frameworks/");
private final ThreadGroup frameworkThreadGroup;
private final Logger logger;
private final File frameworkDataStoreDir;
public FrameworkResources(Framework framework) {
frameworkThreadGroup = new ThreadGroup(FrameworkManager.getFrameworkRootGroup(),
"Framework-" + framework.getIdentify());
frameworkDataStoreDir = new File(frameworkDataStoreRootDir, framework.getClass().getSimpleName());
logger = LoggerFactory.getLogger("Framework-" + framework.getIdentify());
}
ThreadGroup getFrameworkThreadGroup() {
return frameworkThreadGroup;
}
public Logger getLogger() {
return logger;
}
}

View File

@ -4,6 +4,7 @@ import net.lamgc.cgj.bot.boot.ApplicationBoot;
import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.event.BotEventHandler;
import net.lamgc.cgj.bot.framework.Framework; import net.lamgc.cgj.bot.framework.Framework;
import net.lamgc.cgj.bot.framework.FrameworkManager; import net.lamgc.cgj.bot.framework.FrameworkManager;
import net.lamgc.cgj.bot.framework.FrameworkResources;
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageEvent; import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageEvent;
import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageSenderFactory; import net.lamgc.cgj.bot.framework.cli.message.ConsoleMessageSenderFactory;
import net.lamgc.cgj.bot.message.MessageSenderBuilder; import net.lamgc.cgj.bot.message.MessageSenderBuilder;
@ -22,7 +23,7 @@ public class ConsoleMain implements Framework {
private final AtomicBoolean quitState = new AtomicBoolean(); private final AtomicBoolean quitState = new AtomicBoolean();
@Override @Override
public void init(FrameworkManager.FrameworkResources resources) { } public void init(FrameworkResources resources) { }
@Override @Override
public void run() throws Exception { public void run() throws Exception {
@ -62,7 +63,12 @@ public class ConsoleMain implements Framework {
} }
@Override @Override
public String getName() { public String getIdentify() {
return this.toString(); return this.toString();
} }
@Override
public String getFrameworkName() {
return "console";
}
} }

View File

@ -2,7 +2,7 @@ package net.lamgc.cgj.bot.framework.coolq;
import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.boot.BotGlobal;
import net.lamgc.cgj.bot.framework.Framework; import net.lamgc.cgj.bot.framework.Framework;
import net.lamgc.cgj.bot.framework.FrameworkManager; import net.lamgc.cgj.bot.framework.FrameworkResources;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -20,7 +20,7 @@ public class SpringCQApplication implements Framework {
private final Object quitLock = new Object(); private final Object quitLock = new Object();
@Override @Override
public void init(FrameworkManager.FrameworkResources resources) { public void init(FrameworkResources resources) {
this.log = resources.getLogger(); this.log = resources.getLogger();
} }
@ -56,4 +56,9 @@ public class SpringCQApplication implements Framework {
} }
} }
@Override
public String getFrameworkName() {
return "SpringCoolQ";
}
} }

View File

@ -4,7 +4,7 @@ import net.lamgc.cgj.bot.boot.ApplicationBoot;
import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.boot.BotGlobal;
import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.event.BotEventHandler;
import net.lamgc.cgj.bot.framework.Framework; import net.lamgc.cgj.bot.framework.Framework;
import net.lamgc.cgj.bot.framework.FrameworkManager; import net.lamgc.cgj.bot.framework.FrameworkResources;
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent; import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageEvent;
import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageSenderFactory; import net.lamgc.cgj.bot.framework.mirai.message.MiraiMessageSenderFactory;
import net.lamgc.cgj.bot.message.MessageSenderBuilder; import net.lamgc.cgj.bot.message.MessageSenderBuilder;
@ -35,7 +35,7 @@ public class MiraiMain implements Framework {
private final static Properties botProperties = new Properties(); private final static Properties botProperties = new Properties();
@Override @Override
public void init(FrameworkManager.FrameworkResources resources) { public void init(FrameworkResources resources) {
Runtime.getRuntime().addShutdownHook(new Thread(this::close)); Runtime.getRuntime().addShutdownHook(new Thread(this::close));
try { try {
Class.forName(BotEventHandler.class.getName()); Class.forName(BotEventHandler.class.getName());
@ -122,4 +122,9 @@ public class MiraiMain implements Framework {
log.warn("机器人已关闭."); log.warn("机器人已关闭.");
} }
@Override
public String getFrameworkName() {
return "MiraiQQ";
}
} }