Skip to content

Commit

Permalink
将HikariCP改为外部加载
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamVoid committed Jun 16, 2024
1 parent 0424774 commit 940e40a
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions MiraiMC-Base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,16 @@ public static void resolveException(Exception exception, Logger logger, String r
throwable = throwable.getCause();
}
}

public static int getJavaVersion() {
String[] versionElements = System.getProperty("java.version").split("\\.");
int discard = Integer.parseInt(versionElements[0]);
int version;
if (discard == 1) {
version = Integer.parseInt(versionElements[1]);
} else {
version = discard;
}
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import me.dreamvoid.miraimc.LifeCycle;
import me.dreamvoid.miraimc.internal.Utils;
import me.dreamvoid.miraimc.internal.config.PluginConfig;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

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

@Override
public void initialize() throws ClassNotFoundException {
if(!Utils.findClass("com.zaxxer.hikari.HikariDataSource")){
try {
LifeCycle.getPlatform().getLibraryLoader().loadLibraryMaven("com.zaxxer", "HikariCP", Utils.getJavaVersion() >= 11 ? "5.1.0" : "4.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new ClassNotFoundException("Couldn't find HikariCP library both local and remote.");
}
}

String driver;
if (Utils.findClass("com.mysql.cj.jdbc.Driver")){
driver = "com.mysql.cj.jdbc.Driver";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,37 @@
import me.dreamvoid.miraimc.LifeCycle;
import me.dreamvoid.miraimc.internal.Utils;
import me.dreamvoid.miraimc.internal.config.PluginConfig;
import me.dreamvoid.miraimc.internal.loader.LibraryLoader;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

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

@Override
public void initialize() {
public void initialize() throws ClassNotFoundException {
LibraryLoader loader = LifeCycle.getPlatform().getLibraryLoader();
if(!Utils.findClass("com.zaxxer.hikari.HikariDataSource")){
try {
loader.loadLibraryMaven("com.zaxxer", "HikariCP", Utils.getJavaVersion() >= 11 ? "5.1.0" : "4.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new ClassNotFoundException("Couldn't find HikariCP library both local and remote.");
}
}

String driver;
if(Utils.findClass("org.sqlite.JDBC")){
driver = "org.sqlite.JDBC";
} else {
try {
LifeCycle.getPlatform().getLibraryLoader().loadLibraryMaven("org.xerial", "sqlite-jdbc", "3.36.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
loader.loadLibraryMaven("org.xerial", "sqlite-jdbc", "3.36.0.3", PluginConfig.General.MavenRepoUrl, PluginConfig.PluginDir.toPath().resolve("libraries"));
} catch (Exception e) {
throw new RuntimeException(e);
throw new ClassNotFoundException("Couldn't find SQLite library both local and remote.");
}

initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void loadLibraryLocal(File file) {
* @param repo 仓库地址
* @param savePath 保存路径
*/
public void loadLibraryMaven(String groupId, String artifactId, String version, String repo, Path savePath) throws Exception {
public void loadLibraryMaven(String groupId, String artifactId, String version, String repo, Path savePath) throws ParserConfigurationException, IOException, SAXException {
loadLibraryMaven(groupId, artifactId, version, repo, ".jar", savePath);
}

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

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

private static String getSnapshotJarUrl(String baseUrl, String packageName, String packageVersion, String archiveSuffix) throws Exception {
private static String getSnapshotJarUrl(String baseUrl, String packageName, String packageVersion, String archiveSuffix) throws ParserConfigurationException, IOException, SAXException {
String content = Utils.Http.get(baseUrl + "maven-metadata.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Expand Down
4 changes: 0 additions & 4 deletions MiraiMC-Bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@
<configuration>
<minimizeJar>false</minimizeJar>
<relocations>
<relocation>
<pattern>com.zaxxer</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>
Expand Down
4 changes: 0 additions & 4 deletions MiraiMC-Bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
<configuration>
<minimizeJar>false</minimizeJar>
<relocations>
<relocation>
<pattern>com.zaxxer</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>
Expand Down
4 changes: 0 additions & 4 deletions MiraiMC-Nukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
<configuration>
<minimizeJar>false</minimizeJar>
<relocations>
<relocation>
<pattern>com.zaxxer</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>
Expand Down
4 changes: 0 additions & 4 deletions MiraiMC-Velocity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
<configuration>
<minimizeJar>false</minimizeJar>
<relocations>
<relocation>
<pattern>com.zaxxer</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.com.zaxxer</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>me.dreamvoid.miraimc.libraries.org.apache</shadedPattern>
Expand Down

0 comments on commit 940e40a

Please sign in to comment.