mirror of
https://github.com/CarmJos/MineSQL.git
synced 2024-09-19 12:15:45 +00:00
refactor(api): 支持独立创建注册池,便于插件内部管理链接
This commit is contained in:
parent
38afb05fc8
commit
2321ed35c2
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -27,6 +27,8 @@ interface IMineSQL {
|
||||
|
||||
@NotNull SQLRegistry getRegistry();
|
||||
|
||||
@NotNull SQLRegistry createRegistry();
|
||||
|
||||
/**
|
||||
* 创建一个新的 SQLManager 实例
|
||||
*
|
||||
|
@ -43,6 +43,15 @@ public class MineSQL {
|
||||
return instance.getRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个独立的管理器注册池。
|
||||
*
|
||||
* @return {@link SQLRegistry}
|
||||
*/
|
||||
public static @NotNull SQLRegistry createRegistry() {
|
||||
return instance.createRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的 SQLManager 实例
|
||||
*
|
||||
|
@ -12,8 +12,7 @@ import java.util.Optional;
|
||||
* 入口类
|
||||
*/
|
||||
public interface SQLRegistry {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取原生注册的指定名称的 SQLManager 实例
|
||||
*
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
|
@ -29,6 +29,8 @@ import java.util.logging.Logger;
|
||||
|
||||
public class MineSQLCore implements IMineSQL {
|
||||
|
||||
protected static MineSQLCore instance;
|
||||
|
||||
public static final String REPO_OWNER = "CarmJos";
|
||||
public static final String REPO_NAME = "MineSQL";
|
||||
|
||||
@ -39,6 +41,7 @@ public class MineSQLCore implements IMineSQL {
|
||||
protected final PluginConfiguration config;
|
||||
|
||||
public MineSQLCore(MineSQLPlatform platform) {
|
||||
instance = this;
|
||||
this.platform = platform;
|
||||
|
||||
getLogger().info("加载配置文件...");
|
||||
@ -50,18 +53,59 @@ public class MineSQLCore implements IMineSQL {
|
||||
MineSQL.initializeAPI(this);
|
||||
|
||||
getLogger().info("初始化注册池...");
|
||||
this.registry = new MineSQLRegistry(this);
|
||||
this.registry = createRegistry();
|
||||
|
||||
Map<String, Properties> dbProperties = readProperties();
|
||||
Map<String, SQLSourceConfig> dbConfigurations = readConfigurations();
|
||||
|
||||
if (dbProperties.isEmpty() && dbConfigurations.isEmpty()) {
|
||||
getLogger().warning("未检测到任何数据库配置,将不会预创建任何SQLManager。");
|
||||
return;
|
||||
}
|
||||
|
||||
dbProperties.forEach((id, properties) -> {
|
||||
try {
|
||||
getLogger().info("正在初始化数据库 #" + id + " ...");
|
||||
SQLManagerImpl sqlManager = create(id, properties);
|
||||
this.registry.getManagers().put(id, sqlManager);
|
||||
getLogger().info("完成成初始化数据库 #" + id + " 。");
|
||||
} catch (Exception ex) {
|
||||
getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
dbConfigurations.forEach((id, configuration) -> {
|
||||
try {
|
||||
getLogger().info("正在初始化数据库 #" + id + " ...");
|
||||
SQLManagerImpl sqlManager = create(id, configuration);
|
||||
this.registry.getManagers().put(id, sqlManager);
|
||||
getLogger().info("完成初始化数据库 #" + id + " 。");
|
||||
} catch (Exception ex) {
|
||||
getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public MineSQLPlatform getPlatform() {
|
||||
return platform;
|
||||
}
|
||||
|
||||
public static MineSQLCore getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull MineSQLRegistry getRegistry() {
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull MineSQLRegistry createRegistry() {
|
||||
return new MineSQLRegistry();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull SQLSourceConfig conf) {
|
||||
BeeDataSourceConfig config = new BeeDataSourceConfig();
|
||||
|
@ -1,84 +1,24 @@
|
||||
package cc.carm.plugin.minesql;
|
||||
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.manager.SQLManagerImpl;
|
||||
import cc.carm.plugin.minesql.api.SQLRegistry;
|
||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.Unmodifiable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MineSQLRegistry implements SQLRegistry {
|
||||
|
||||
private static MineSQLRegistry instance;
|
||||
|
||||
protected ExecutorService executorPool;
|
||||
protected MineSQLCore core;
|
||||
private final HashMap<String, SQLManager> managers = new HashMap<>();
|
||||
|
||||
protected MineSQLRegistry(@NotNull MineSQLCore core) {
|
||||
this.core = core;
|
||||
MineSQLRegistry.instance = this;
|
||||
this.executorPool = Executors.newFixedThreadPool(2, (r) -> {
|
||||
Thread thread = new Thread(r, "EasySQLRegistry");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
|
||||
Map<String, Properties> dbProperties = core.readProperties();
|
||||
Map<String, SQLSourceConfig> dbConfigurations = core.readConfigurations();
|
||||
|
||||
if (dbProperties.isEmpty() && dbConfigurations.isEmpty()) {
|
||||
core.getLogger().warning("未检测到任何数据库配置,将不会预创建任何SQLManager。");
|
||||
return;
|
||||
}
|
||||
|
||||
dbProperties.forEach((id, properties) -> {
|
||||
try {
|
||||
core.getLogger().info("正在初始化数据库 #" + id + " ...");
|
||||
SQLManagerImpl sqlManager = this.core.create(id, properties);
|
||||
this.managers.put(id, sqlManager);
|
||||
core.getLogger().info("完成成初始化数据库 #" + id + " 。");
|
||||
} catch (Exception ex) {
|
||||
core.getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
dbConfigurations.forEach((id, configuration) -> {
|
||||
try {
|
||||
core.getLogger().info("正在初始化数据库 #" + id + " ...");
|
||||
SQLManagerImpl sqlManager = this.core.create(id, configuration);
|
||||
this.managers.put(id, sqlManager);
|
||||
core.getLogger().info("完成初始化数据库 #" + id + " 。");
|
||||
} catch (Exception ex) {
|
||||
core.getLogger().severe("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected HashMap<String, SQLManager> getManagers() {
|
||||
return managers;
|
||||
}
|
||||
|
||||
public ExecutorService getExecutor() {
|
||||
return executorPool;
|
||||
}
|
||||
|
||||
public static MineSQLRegistry getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public MineSQLCore getCore() {
|
||||
return core;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Optional<@Nullable SQLManager> getOptional(@Nullable String id) {
|
||||
return Optional.of(this.managers.get(id));
|
||||
@ -104,5 +44,5 @@ public class MineSQLRegistry implements SQLRegistry {
|
||||
if (manager == null) throw new NullPointerException("不存在ID为 " + name + " 的SQLManager实例。");
|
||||
return manager;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package cc.carm.plugin.minesql.command;
|
||||
import cc.carm.lib.easysql.api.SQLManager;
|
||||
import cc.carm.lib.easysql.api.SQLQuery;
|
||||
import cc.carm.plugin.minesql.MineSQLCore;
|
||||
import cc.carm.plugin.minesql.MineSQLRegistry;
|
||||
import cc.carm.plugin.minesql.util.VersionReader;
|
||||
import co.aikar.commands.BaseCommand;
|
||||
import co.aikar.commands.CommandHelp;
|
||||
@ -66,7 +65,7 @@ public class MineSQLCommand extends BaseCommand {
|
||||
issuer.sendMessage("§c只有后台执行才能使用此命令。");
|
||||
return;
|
||||
}
|
||||
Map<String, ? extends SQLManager> runningManagers = MineSQLRegistry.getInstance().list();
|
||||
Map<String, ? extends SQLManager> runningManagers = MineSQLCore.getInstance().getRegistry().list();
|
||||
if (runningManagers.isEmpty()) {
|
||||
issuer.sendMessage("§r当前无正在运行的数据库管理器。");
|
||||
} else {
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>minesql-parent</artifactId>
|
||||
<groupId>cc.carm.plugin</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<properties>
|
||||
|
Loading…
Reference in New Issue
Block a user