mirror of
https://github.com/CarmJos/MineSQL.git
synced 2024-09-19 20:25:45 +00:00
refactor(api): 支持独立创建注册池,便于插件内部管理链接
This commit is contained in:
parent
38afb05fc8
commit
2321ed35c2
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.0.0</version>
|
<version>1.1.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ interface IMineSQL {
|
|||||||
|
|
||||||
@NotNull SQLRegistry getRegistry();
|
@NotNull SQLRegistry getRegistry();
|
||||||
|
|
||||||
|
@NotNull SQLRegistry createRegistry();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的 SQLManager 实例
|
* 创建一个新的 SQLManager 实例
|
||||||
*
|
*
|
||||||
|
@ -43,6 +43,15 @@ public class MineSQL {
|
|||||||
return instance.getRegistry();
|
return instance.getRegistry();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个独立的管理器注册池。
|
||||||
|
*
|
||||||
|
* @return {@link SQLRegistry}
|
||||||
|
*/
|
||||||
|
public static @NotNull SQLRegistry createRegistry() {
|
||||||
|
return instance.createRegistry();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的 SQLManager 实例
|
* 创建一个新的 SQLManager 实例
|
||||||
*
|
*
|
||||||
|
@ -13,7 +13,6 @@ import java.util.Optional;
|
|||||||
*/
|
*/
|
||||||
public interface SQLRegistry {
|
public interface SQLRegistry {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取原生注册的指定名称的 SQLManager 实例
|
* 获取原生注册的指定名称的 SQLManager 实例
|
||||||
*
|
*
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.0.0</version>
|
<version>1.1.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -29,6 +29,8 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
public class MineSQLCore implements IMineSQL {
|
public class MineSQLCore implements IMineSQL {
|
||||||
|
|
||||||
|
protected static MineSQLCore instance;
|
||||||
|
|
||||||
public static final String REPO_OWNER = "CarmJos";
|
public static final String REPO_OWNER = "CarmJos";
|
||||||
public static final String REPO_NAME = "MineSQL";
|
public static final String REPO_NAME = "MineSQL";
|
||||||
|
|
||||||
@ -39,6 +41,7 @@ public class MineSQLCore implements IMineSQL {
|
|||||||
protected final PluginConfiguration config;
|
protected final PluginConfiguration config;
|
||||||
|
|
||||||
public MineSQLCore(MineSQLPlatform platform) {
|
public MineSQLCore(MineSQLPlatform platform) {
|
||||||
|
instance = this;
|
||||||
this.platform = platform;
|
this.platform = platform;
|
||||||
|
|
||||||
getLogger().info("加载配置文件...");
|
getLogger().info("加载配置文件...");
|
||||||
@ -50,18 +53,59 @@ public class MineSQLCore implements IMineSQL {
|
|||||||
MineSQL.initializeAPI(this);
|
MineSQL.initializeAPI(this);
|
||||||
|
|
||||||
getLogger().info("初始化注册池...");
|
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() {
|
public MineSQLPlatform getPlatform() {
|
||||||
return platform;
|
return platform;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static MineSQLCore getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull MineSQLRegistry getRegistry() {
|
public @NotNull MineSQLRegistry getRegistry() {
|
||||||
return this.registry;
|
return this.registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull MineSQLRegistry createRegistry() {
|
||||||
|
return new MineSQLRegistry();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull SQLSourceConfig conf) {
|
public @NotNull SQLManagerImpl create(@NotNull String name, @NotNull SQLSourceConfig conf) {
|
||||||
BeeDataSourceConfig config = new BeeDataSourceConfig();
|
BeeDataSourceConfig config = new BeeDataSourceConfig();
|
||||||
|
@ -1,84 +1,24 @@
|
|||||||
package cc.carm.plugin.minesql;
|
package cc.carm.plugin.minesql;
|
||||||
|
|
||||||
import cc.carm.lib.easysql.api.SQLManager;
|
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.SQLRegistry;
|
||||||
import cc.carm.plugin.minesql.api.source.SQLSourceConfig;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.annotations.Unmodifiable;
|
import org.jetbrains.annotations.Unmodifiable;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Collections;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.HashMap;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class MineSQLRegistry implements SQLRegistry {
|
public class MineSQLRegistry implements SQLRegistry {
|
||||||
|
|
||||||
private static MineSQLRegistry instance;
|
|
||||||
|
|
||||||
protected ExecutorService executorPool;
|
|
||||||
protected MineSQLCore core;
|
|
||||||
private final HashMap<String, SQLManager> managers = new HashMap<>();
|
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() {
|
protected HashMap<String, SQLManager> getManagers() {
|
||||||
return managers;
|
return managers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecutorService getExecutor() {
|
|
||||||
return executorPool;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MineSQLRegistry getInstance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MineSQLCore getCore() {
|
|
||||||
return core;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull Optional<@Nullable SQLManager> getOptional(@Nullable String id) {
|
public @NotNull Optional<@Nullable SQLManager> getOptional(@Nullable String id) {
|
||||||
return Optional.of(this.managers.get(id));
|
return Optional.of(this.managers.get(id));
|
||||||
|
@ -3,7 +3,6 @@ package cc.carm.plugin.minesql.command;
|
|||||||
import cc.carm.lib.easysql.api.SQLManager;
|
import cc.carm.lib.easysql.api.SQLManager;
|
||||||
import cc.carm.lib.easysql.api.SQLQuery;
|
import cc.carm.lib.easysql.api.SQLQuery;
|
||||||
import cc.carm.plugin.minesql.MineSQLCore;
|
import cc.carm.plugin.minesql.MineSQLCore;
|
||||||
import cc.carm.plugin.minesql.MineSQLRegistry;
|
|
||||||
import cc.carm.plugin.minesql.util.VersionReader;
|
import cc.carm.plugin.minesql.util.VersionReader;
|
||||||
import co.aikar.commands.BaseCommand;
|
import co.aikar.commands.BaseCommand;
|
||||||
import co.aikar.commands.CommandHelp;
|
import co.aikar.commands.CommandHelp;
|
||||||
@ -66,7 +65,7 @@ public class MineSQLCommand extends BaseCommand {
|
|||||||
issuer.sendMessage("§c只有后台执行才能使用此命令。");
|
issuer.sendMessage("§c只有后台执行才能使用此命令。");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Map<String, ? extends SQLManager> runningManagers = MineSQLRegistry.getInstance().list();
|
Map<String, ? extends SQLManager> runningManagers = MineSQLCore.getInstance().getRegistry().list();
|
||||||
if (runningManagers.isEmpty()) {
|
if (runningManagers.isEmpty()) {
|
||||||
issuer.sendMessage("§r当前无正在运行的数据库管理器。");
|
issuer.sendMessage("§r当前无正在运行的数据库管理器。");
|
||||||
} else {
|
} else {
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.0.0</version>
|
<version>1.1.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.0.0</version>
|
<version>1.1.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.0.0</version>
|
<version>1.1.0</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<version>1.0.0</version>
|
<version>1.1.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<properties>
|
<properties>
|
||||||
|
2
pom.xml
2
pom.xml
@ -24,7 +24,7 @@
|
|||||||
<groupId>cc.carm.plugin</groupId>
|
<groupId>cc.carm.plugin</groupId>
|
||||||
<artifactId>minesql-parent</artifactId>
|
<artifactId>minesql-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.0.0</version>
|
<version>1.1.0</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>api</module>
|
<module>api</module>
|
||||||
<module>core</module>
|
<module>core</module>
|
||||||
|
Loading…
Reference in New Issue
Block a user