11package cc .carm .plugin .minesql ;
22
33import cc .carm .lib .easysql .api .SQLManager ;
4- import cc .carm .lib .easysql .manager .SQLManagerImpl ;
54import cc .carm .plugin .minesql .api .SQLRegistry ;
6- import cc .carm .plugin .minesql .api .source .SQLSourceConfig ;
75import org .jetbrains .annotations .NotNull ;
86import org .jetbrains .annotations .Nullable ;
97import org .jetbrains .annotations .Unmodifiable ;
108
11- import java .util .*;
12- import java .util .concurrent .ExecutorService ;
13- import java .util .concurrent .Executors ;
9+ import java .util .Collections ;
10+ import java .util .HashMap ;
11+ import java .util .Map ;
12+ import java .util .Optional ;
1413
1514public class MineSQLRegistry implements SQLRegistry {
1615
17- private static MineSQLRegistry instance ;
18-
19- protected ExecutorService executorPool ;
20- protected MineSQLCore core ;
2116 private final HashMap <String , SQLManager > managers = new HashMap <>();
2217
23- protected MineSQLRegistry (@ NotNull MineSQLCore core ) {
24- this .core = core ;
25- MineSQLRegistry .instance = this ;
26- this .executorPool = Executors .newFixedThreadPool (2 , (r ) -> {
27- Thread thread = new Thread (r , "EasySQLRegistry" );
28- thread .setDaemon (true );
29- return thread ;
30- });
31-
32- Map <String , Properties > dbProperties = core .readProperties ();
33- Map <String , SQLSourceConfig > dbConfigurations = core .readConfigurations ();
34-
35- if (dbProperties .isEmpty () && dbConfigurations .isEmpty ()) {
36- core .getLogger ().warning ("未检测到任何数据库配置,将不会预创建任何SQLManager。" );
37- return ;
38- }
39-
40- dbProperties .forEach ((id , properties ) -> {
41- try {
42- core .getLogger ().info ("正在初始化数据库 #" + id + " ..." );
43- SQLManagerImpl sqlManager = this .core .create (id , properties );
44- this .managers .put (id , sqlManager );
45- core .getLogger ().info ("完成成初始化数据库 #" + id + " 。" );
46- } catch (Exception ex ) {
47- core .getLogger ().severe ("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex .getMessage ());
48- ex .printStackTrace ();
49- }
50- });
51-
52- dbConfigurations .forEach ((id , configuration ) -> {
53- try {
54- core .getLogger ().info ("正在初始化数据库 #" + id + " ..." );
55- SQLManagerImpl sqlManager = this .core .create (id , configuration );
56- this .managers .put (id , sqlManager );
57- core .getLogger ().info ("完成初始化数据库 #" + id + " 。" );
58- } catch (Exception ex ) {
59- core .getLogger ().severe ("初始化SQLManager(#" + id + ") 出错,请检查配置文件: " + ex .getMessage ());
60- ex .printStackTrace ();
61- }
62- });
63-
64- }
65-
6618 protected HashMap <String , SQLManager > getManagers () {
6719 return managers ;
6820 }
6921
70- public ExecutorService getExecutor () {
71- return executorPool ;
72- }
73-
74- public static MineSQLRegistry getInstance () {
75- return instance ;
76- }
77-
78- public MineSQLCore getCore () {
79- return core ;
80- }
81-
8222 @ Override
8323 public @ NotNull Optional <@ Nullable SQLManager > getOptional (@ Nullable String id ) {
8424 return Optional .of (this .managers .get (id ));
@@ -104,5 +44,5 @@ public void register(@NotNull String name, @NotNull SQLManager sqlManager) throw
10444 if (manager == null ) throw new NullPointerException ("不存在ID为 " + name + " 的SQLManager实例。" );
10545 return manager ;
10646 }
107-
47+
10848}
0 commit comments