Skip to content

Commit 940e40a

Browse files
committed
将HikariCP改为外部加载
1 parent 0424774 commit 940e40a

File tree

9 files changed

+45
-23
lines changed

9 files changed

+45
-23
lines changed

MiraiMC-Base/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<groupId>com.zaxxer</groupId>
2424
<artifactId>HikariCP</artifactId>
2525
<version>4.0.3</version>
26+
<scope>provided</scope>
2627
</dependency>
2728
<dependency>
2829
<groupId>com.google.guava</groupId>

MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/internal/Utils.java

+12
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,16 @@ public static void resolveException(Exception exception, Logger logger, String r
206206
throwable = throwable.getCause();
207207
}
208208
}
209+
210+
public static int getJavaVersion() {
211+
String[] versionElements = System.getProperty("java.version").split("\\.");
212+
int discard = Integer.parseInt(versionElements[0]);
213+
int version;
214+
if (discard == 1) {
215+
version = Integer.parseInt(versionElements[1]);
216+
} else {
217+
version = discard;
218+
}
219+
return version;
220+
}
209221
}

MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/internal/database/MySQL.java

+12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import com.zaxxer.hikari.HikariConfig;
44
import com.zaxxer.hikari.HikariDataSource;
5+
import me.dreamvoid.miraimc.LifeCycle;
56
import me.dreamvoid.miraimc.internal.Utils;
67
import me.dreamvoid.miraimc.internal.config.PluginConfig;
8+
import org.xml.sax.SAXException;
79

10+
import javax.xml.parsers.ParserConfigurationException;
11+
import java.io.IOException;
812
import java.sql.Connection;
913
import java.sql.SQLException;
1014

@@ -13,6 +17,14 @@ public class MySQL implements Database {
1317

1418
@Override
1519
public void initialize() throws ClassNotFoundException {
20+
if(!Utils.findClass("com.zaxxer.hikari.HikariDataSource")){
21+
try {
22+
LifeCycle.getPlatform().getLibraryLoader().loadLibraryMaven("com.zaxxer", "HikariCP", Utils.getJavaVersion() >= 11 ? "5.1.0" : "4.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
23+
} catch (ParserConfigurationException | SAXException | IOException e) {
24+
throw new ClassNotFoundException("Couldn't find HikariCP library both local and remote.");
25+
}
26+
}
27+
1628
String driver;
1729
if (Utils.findClass("com.mysql.cj.jdbc.Driver")){
1830
driver = "com.mysql.cj.jdbc.Driver";

MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/internal/database/SQLite.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,37 @@
55
import me.dreamvoid.miraimc.LifeCycle;
66
import me.dreamvoid.miraimc.internal.Utils;
77
import me.dreamvoid.miraimc.internal.config.PluginConfig;
8+
import me.dreamvoid.miraimc.internal.loader.LibraryLoader;
9+
import org.xml.sax.SAXException;
810

11+
import javax.xml.parsers.ParserConfigurationException;
912
import java.io.File;
13+
import java.io.IOException;
1014
import java.sql.Connection;
1115
import java.sql.SQLException;
1216

1317
public class SQLite implements Database {
1418
private static HikariDataSource ds; // SQLite
1519

1620
@Override
17-
public void initialize() {
21+
public void initialize() throws ClassNotFoundException {
22+
LibraryLoader loader = LifeCycle.getPlatform().getLibraryLoader();
23+
if(!Utils.findClass("com.zaxxer.hikari.HikariDataSource")){
24+
try {
25+
loader.loadLibraryMaven("com.zaxxer", "HikariCP", Utils.getJavaVersion() >= 11 ? "5.1.0" : "4.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
26+
} catch (ParserConfigurationException | SAXException | IOException e) {
27+
throw new ClassNotFoundException("Couldn't find HikariCP library both local and remote.");
28+
}
29+
}
30+
1831
String driver;
1932
if(Utils.findClass("org.sqlite.JDBC")){
2033
driver = "org.sqlite.JDBC";
2134
} else {
2235
try {
23-
LifeCycle.getPlatform().getLibraryLoader().loadLibraryMaven("org.xerial", "sqlite-jdbc", "3.36.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
36+
loader.loadLibraryMaven("org.xerial", "sqlite-jdbc", "3.36.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
2437
} catch (Exception e) {
25-
throw new RuntimeException(e);
38+
throw new ClassNotFoundException("Couldn't find SQLite library both local and remote.");
2639
}
2740

2841
initialize();

MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/internal/loader/LibraryLoader.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void loadLibraryLocal(File file) {
6565
* @param repo 仓库地址
6666
* @param savePath 保存路径
6767
*/
68-
public void loadLibraryMaven(String groupId, String artifactId, String version, String repo, Path savePath) throws Exception {
68+
public void loadLibraryMaven(String groupId, String artifactId, String version, String repo, Path savePath) throws ParserConfigurationException, IOException, SAXException {
6969
loadLibraryMaven(groupId, artifactId, version, repo, ".jar", savePath);
7070
}
7171

@@ -79,7 +79,7 @@ public void loadLibraryMaven(String groupId, String artifactId, String version,
7979
* @param archiveSuffix 文件后缀(通常为.jar)
8080
* @param savePath 保存路径
8181
*/
82-
public void loadLibraryMaven(String groupId, String artifactId, String version, String repo, String archiveSuffix, Path savePath) throws Exception {
82+
public void loadLibraryMaven(String groupId, String artifactId, String version, String repo, String archiveSuffix, Path savePath) throws ParserConfigurationException, IOException, SAXException {
8383
String filename = artifactId + "-" + version + archiveSuffix; // 文件名
8484
String sha1Filename = filename + ".sha1"; // sha1文件名
8585
File file = savePath.resolve(filename).toFile();
@@ -158,7 +158,7 @@ private static Document fetchMavenMetadata(String groupId, String artifactId, St
158158
}
159159
}
160160

161-
private static String getJarUrl(String groupId, String artifactId, String version, String repo, String archiveSuffix) throws Exception {
161+
private static String getJarUrl(String groupId, String artifactId, String version, String repo, String archiveSuffix) throws ParserConfigurationException, IOException, SAXException {
162162
if(version.endsWith("-SNAPSHOT")){
163163
String base = repo + (repo.endsWith("/") ? "" : "/") + groupId.replace(".", "/") + "/" + artifactId + "/" + version + "/";
164164
return getSnapshotJarUrl(base, artifactId, version, archiveSuffix);
@@ -168,7 +168,7 @@ private static String getJarUrl(String groupId, String artifactId, String versio
168168
}
169169
}
170170

171-
private static String getSnapshotJarUrl(String baseUrl, String packageName, String packageVersion, String archiveSuffix) throws Exception {
171+
private static String getSnapshotJarUrl(String baseUrl, String packageName, String packageVersion, String archiveSuffix) throws ParserConfigurationException, IOException, SAXException {
172172
String content = Utils.Http.get(baseUrl + "maven-metadata.xml");
173173
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
174174
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

MiraiMC-Bukkit/pom.xml

-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@
6060
<configuration>
6161
<minimizeJar>false</minimizeJar>
6262
<relocations>
63-
<relocation>
64-
<pattern>com.zaxxer</pattern>
65-
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
66-
</relocation>
6763
<relocation>
6864
<pattern>org.apache</pattern>
6965
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>

MiraiMC-Bungee/pom.xml

-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@
5757
<configuration>
5858
<minimizeJar>false</minimizeJar>
5959
<relocations>
60-
<relocation>
61-
<pattern>com.zaxxer</pattern>
62-
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
63-
</relocation>
6460
<relocation>
6561
<pattern>org.apache</pattern>
6662
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>

MiraiMC-Nukkit/pom.xml

-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@
5252
<configuration>
5353
<minimizeJar>false</minimizeJar>
5454
<relocations>
55-
<relocation>
56-
<pattern>com.zaxxer</pattern>
57-
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
58-
</relocation>
5955
<relocation>
6056
<pattern>org.apache</pattern>
6157
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>

MiraiMC-Velocity/pom.xml

-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
<configuration>
2222
<minimizeJar>false</minimizeJar>
2323
<relocations>
24-
<relocation>
25-
<pattern>com.zaxxer</pattern>
26-
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
27-
</relocation>
2824
<relocation>
2925
<pattern>org.apache</pattern>
3026
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>

0 commit comments

Comments
 (0)