mirror of
https://github.com/songoda/SongodaCore.git
synced 2025-01-10 09:47:34 +01:00
refactor: Slight refactoring of DependencyLoader class
I am too scared on touching too much logic and breaking something as I didn't work on this class or with that library much yet. So I'm just refactoring it a bit not changing a lot of logic
This commit is contained in:
parent
193af915e9
commit
fcb613e3d6
@ -1,13 +1,8 @@
|
||||
package com.craftaro.core.dependency;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Dependency {
|
||||
private final String repositoryUrl;
|
||||
@ -18,7 +13,6 @@ public class Dependency {
|
||||
private final List<Relocation> relocations;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param repositoryUrl The repository url to download the dependency from.
|
||||
* @param groupId The groupId of the dependency.
|
||||
* @param artifactId The artifactId of the dependency.
|
||||
@ -29,7 +23,6 @@ public class Dependency {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param repositoryUrl The repository url to download the dependency from.
|
||||
* @param groupId The groupId of the dependency.
|
||||
* @param artifactId The artifactId of the dependency.
|
||||
@ -41,7 +34,6 @@ public class Dependency {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param repositoryUrl The repository url to download the dependency from.
|
||||
* @param groupId The groupId of the dependency.
|
||||
* @param artifactId The artifactId of the dependency.
|
||||
@ -85,6 +77,14 @@ public class Dependency {
|
||||
}
|
||||
|
||||
public boolean shouldRelocate() {
|
||||
return this.relocate || !relocations.isEmpty();
|
||||
return this.relocate || !this.relocations.isEmpty();
|
||||
}
|
||||
|
||||
public String buildArtifactUrl() {
|
||||
return this.repositoryUrl + "/" +
|
||||
this.groupId.replace('.', '/') + "/" +
|
||||
this.artifactId + "/" +
|
||||
this.version + "/" +
|
||||
this.artifactId + "-" + this.version + ".jar";
|
||||
}
|
||||
}
|
||||
|
@ -10,11 +10,10 @@ import me.lucko.jarrelocator.Relocation;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
@ -42,57 +41,24 @@ public class DependencyLoader {
|
||||
}
|
||||
|
||||
public void loadDependency(Dependency dependency) throws IOException {
|
||||
String repositoryUrl = dependency.getRepositoryUrl();
|
||||
String groupId = dependency.getGroupId();
|
||||
String artifactId = dependency.getArtifactId();
|
||||
String version = dependency.getVersion();
|
||||
//Download dependency from the repositoryUrl
|
||||
//Check if we have the dependency downloaded already
|
||||
String name = dependency.getArtifactId() + "-" + dependency.getVersion();
|
||||
|
||||
File outputFile = new File(this.libraryLoader.getLibFolder(), dependency.getGroupId().replace(".", File.separator) + File.separator + dependency.getArtifactId().replace(".", File.separator) + File.separator + dependency.getVersion() + File.separator + "raw-" + name + ".jar");
|
||||
File relocatedFile = new File(outputFile.getParentFile(), name.replace("raw-", "") + ".jar");
|
||||
if (relocatedFile.exists()) {
|
||||
//Check if the file is already loaded to the classpath
|
||||
if (isLoaded(relocatedFile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Load dependency into the classpath
|
||||
loadJarIntoClasspath(relocatedFile, dependency);
|
||||
return;
|
||||
}
|
||||
|
||||
SongodaCore.getLogger().info("Downloading dependency " + groupId + ":" + artifactId + ":" + version + " from " + repositoryUrl);
|
||||
// Construct the URL for the artifact in the Maven repository
|
||||
String artifactUrl = repositoryUrl + "/" +
|
||||
groupId.replace('.', '/') + "/" +
|
||||
artifactId + "/" +
|
||||
version + "/" +
|
||||
artifactId + "-" + version + ".jar";
|
||||
|
||||
URL url = new URL(artifactUrl);
|
||||
URLConnection connection = url.openConnection();
|
||||
InputStream in = connection.getInputStream();
|
||||
|
||||
// Define the output file
|
||||
|
||||
outputFile.getParentFile().mkdirs();
|
||||
FileOutputStream out = new FileOutputStream(outputFile);
|
||||
|
||||
// Read from the input stream and write to the output stream
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, length);
|
||||
SongodaCore.getLogger().info(String.format("Downloading dependency %s:%s:%s from %s", dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getRepositoryUrl()));
|
||||
Files.createDirectories(outputFile.getParentFile().toPath());
|
||||
try (InputStream is = new URL(dependency.buildArtifactUrl()).openStream()) {
|
||||
Files.copy(is, outputFile.toPath());
|
||||
}
|
||||
|
||||
// Close both streams
|
||||
in.close();
|
||||
out.close();
|
||||
|
||||
//Load dependency into the classpath
|
||||
SongodaCore.getLogger().info("Downloaded dependency " + groupId + ":" + artifactId + ":" + version);
|
||||
loadJarIntoClasspath(outputFile, dependency);
|
||||
}
|
||||
|
||||
@ -112,65 +78,72 @@ public class DependencyLoader {
|
||||
try {
|
||||
relocator.run();
|
||||
SongodaCore.getLogger().info("Relocated dependency " + file);
|
||||
//Delete the old jar
|
||||
file.delete();
|
||||
} catch (Exception e) {
|
||||
SongodaCore.getLogger().severe("Failed to relocate dependency1 " + file);
|
||||
if (e.getMessage().contains("zip file is empty")) {
|
||||
SongodaCore.getLogger().severe("Try deleting the 'server root/craftaro' folder and restarting the server");
|
||||
}
|
||||
//Delete the new jar cuz it's probably corrupted
|
||||
finalJar.delete();
|
||||
|
||||
// Delete the old jar
|
||||
Files.deleteIfExists(file.toPath());
|
||||
} catch (Exception e) {
|
||||
SongodaCore.getLogger().severe("Failed to relocate dependency " + file);
|
||||
if (e.getMessage().contains("zip file is empty")) {
|
||||
SongodaCore.getLogger().severe("Try deleting '" + this.libraryLoader.getLibFolder().getParent() + "' and restarting the server");
|
||||
}
|
||||
|
||||
// Delete the new jar cuz it's probably corrupted
|
||||
Files.deleteIfExists(finalJar.toPath());
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
this.libraryLoader.load(new LibraryLoader.Dependency(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getRepositoryUrl()), true);
|
||||
} catch (InvalidDependencyException ignored) {
|
||||
//already loaded
|
||||
// Already loaded
|
||||
} catch (UnknownDependencyException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
SongodaCore.getLogger().info("----------------------------");
|
||||
}
|
||||
|
||||
private boolean isRelocated(File file) throws IOException {
|
||||
try (ZipFile zipFile = new ZipFile(file)) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
|
||||
private boolean isRelocated(File jarFile) throws IOException {
|
||||
try (ZipFile zipFile = new ZipFile(jarFile)) {
|
||||
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
|
||||
while (zipEntries.hasMoreElements()) {
|
||||
ZipEntry entry = zipEntries.nextElement();
|
||||
if (entry.getName().startsWith("com/craftaro/third_party")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isLoaded(File file) throws IOException {
|
||||
//Find the first class file in the jar and try Class.forName
|
||||
try (ZipFile zipFile = new ZipFile(file)) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
if (entry.getName().startsWith("META-INF")) {
|
||||
/**
|
||||
* Finds the first .class file in the jar and check if it's loaded
|
||||
*/
|
||||
private boolean isLoaded(File jarFile) throws IOException {
|
||||
try (ZipFile zipFile = new ZipFile(jarFile)) {
|
||||
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
|
||||
while (zipEntries.hasMoreElements()) {
|
||||
ZipEntry zipEntry = zipEntries.nextElement();
|
||||
if (zipEntry.getName().startsWith("META-INF")) {
|
||||
continue;
|
||||
}
|
||||
if (!zipEntry.getName().endsWith(".class")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entry.getName().endsWith(".class")) {
|
||||
String className = entry.getName().replace("/", ".").replace(".class", "");
|
||||
String className = zipEntry.getName()
|
||||
.substring(0, zipEntry.getName().length() - ".class".length())
|
||||
.replace("/", ".");
|
||||
try {
|
||||
Class.forName(className);
|
||||
return true;
|
||||
} catch (Exception | Error e) {
|
||||
} catch (Throwable th) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1,26 +1,26 @@
|
||||
package com.craftaro.core.dependency;
|
||||
|
||||
public class Relocation {
|
||||
|
||||
private final String from;
|
||||
private final String to;
|
||||
|
||||
public Relocation(String from, String to) {
|
||||
if (from == null) {
|
||||
throw new IllegalArgumentException("from cannot be null");
|
||||
throw new IllegalArgumentException("'from' cannot be null");
|
||||
}
|
||||
if (to == null) {
|
||||
throw new IllegalArgumentException("to cannot be null");
|
||||
throw new IllegalArgumentException("'to' cannot be null");
|
||||
}
|
||||
|
||||
this.from = from.replaceAll(";", ".");
|
||||
this.to = to.replaceAll(";", ".");
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
return this.from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
return this.to;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user