mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-14 11:46:06 +00:00
Minor code refactors, clean up logging significantly, and close resources
Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
@@ -26,10 +26,12 @@ import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.dfsek.terra.addon.exception.AddonLoadException;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
@@ -44,44 +46,54 @@ public class BootstrapAddonLoader implements BootstrapBaseAddon<BootstrapBaseAdd
|
||||
|
||||
public BootstrapAddonLoader(Platform platform) { this.platform = platform; }
|
||||
|
||||
private BootstrapBaseAddon<?> loadAddon(Path addonPath, ClassLoader parent) {
|
||||
logger.debug("Loading bootstrap addon from JAR {}", addonPath);
|
||||
try(JarFile jar = new JarFile(addonPath.toFile())) {
|
||||
String entry = jar.getManifest().getMainAttributes().getValue("Bootstrap-Addon-Entry-Point");
|
||||
|
||||
if(entry == null) {
|
||||
throw new AddonLoadException("No Bootstrap-Addon-Entry-Point attribute defined in addon manifest.");
|
||||
}
|
||||
|
||||
//noinspection NestedTryStatement
|
||||
try {
|
||||
@SuppressWarnings({ "resource", "IOResourceOpenedButNotSafelyClosed" })
|
||||
AddonClassLoader loader = new AddonClassLoader(new URL[]{ addonPath.toUri().toURL() }, parent);
|
||||
|
||||
Object addonObject = loader.loadClass(entry).getConstructor().newInstance();
|
||||
|
||||
if(!(addonObject instanceof BootstrapBaseAddon<?> addon)) {
|
||||
throw new AddonLoadException(
|
||||
addonObject.getClass() + " does not extend " + BootstrapBaseAddon.class);
|
||||
}
|
||||
|
||||
logger.debug("Loaded bootstrap addon {}@{} with entry point {}",
|
||||
addon.getID(), addon.getVersion().getFormatted(), addonObject.getClass());
|
||||
return addon;
|
||||
} catch(InvocationTargetException e) {
|
||||
throw new AddonLoadException("Exception occurred while instantiating addon", e);
|
||||
} catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) {
|
||||
throw new AddonLoadException(String.format("No valid default constructor found in entry point %s", entry), e);
|
||||
} catch(ClassNotFoundException | NoClassDefFoundError e) {
|
||||
throw new AddonLoadException(String.format("Entry point %s not found in JAR.", entry), e);
|
||||
}
|
||||
|
||||
} catch(IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<BootstrapBaseAddon<?>> loadAddons(Path addonsFolder, ClassLoader parent) {
|
||||
Path bootstrapAddons = addonsFolder.resolve("bootstrap");
|
||||
logger.info("Loading bootstrap addons from {}", bootstrapAddons);
|
||||
try {
|
||||
return Files.walk(bootstrapAddons, 1)
|
||||
.filter(path -> path.toFile().isFile() && path.toString().endsWith(".jar"))
|
||||
.map(path -> {
|
||||
try {
|
||||
logger.info("Loading bootstrap addon from JAR {}", path);
|
||||
JarFile jar = new JarFile(path.toFile());
|
||||
String entry = jar.getManifest().getMainAttributes().getValue("Bootstrap-Addon-Entry-Point");
|
||||
|
||||
if(entry == null) {
|
||||
throw new AddonLoadException("No Bootstrap-Addon-Entry-Point attribute defined in addon manifest.");
|
||||
}
|
||||
|
||||
AddonClassLoader loader = new AddonClassLoader(new URL[]{ path.toUri().toURL() }, parent);
|
||||
|
||||
try {
|
||||
Object in = loader.loadClass(entry).getConstructor().newInstance();
|
||||
if(!(in instanceof BootstrapBaseAddon)) {
|
||||
throw new AddonLoadException(in.getClass() + " does not extend " + BootstrapBaseAddon.class);
|
||||
}
|
||||
logger.info("Loaded bootstrap addon {}", ((BootstrapBaseAddon<?>) in).getID());
|
||||
return (BootstrapBaseAddon<?>) in;
|
||||
} catch(InvocationTargetException e) {
|
||||
throw new AddonLoadException("Exception occurred while instantiating addon: ", e);
|
||||
} catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) {
|
||||
throw new AddonLoadException("No valid default constructor found in entry point " + entry);
|
||||
} catch(ClassNotFoundException e) {
|
||||
throw new AddonLoadException("Entry point " + entry + " not found in JAR.");
|
||||
}
|
||||
|
||||
} catch(IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
Path bootstrapFolder = addonsFolder.resolve("bootstrap");
|
||||
logger.debug("Loading bootstrap addons from {}", bootstrapFolder);
|
||||
|
||||
try(Stream<Path> bootstrapAddons = Files.walk(bootstrapFolder, 1, FileVisitOption.FOLLOW_LINKS)) {
|
||||
return bootstrapAddons.filter(path -> path.toFile().isFile())
|
||||
.filter(path -> path.toFile().canRead())
|
||||
.filter(path -> path.toString().endsWith(".jar"))
|
||||
.map(path -> loadAddon(path, parent))
|
||||
.collect(Collectors.toList());
|
||||
} catch(IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user