Minor code refactors, clean up logging significantly, and close resources

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax
2021-11-23 17:13:04 -05:00
parent 2d4e46a43f
commit 711451a4b3
4 changed files with 161 additions and 129 deletions
@@ -24,20 +24,21 @@ import com.dfsek.terra.api.inject.annotations.Inject;
public class ManifestAddon implements BaseAddon {
private static final Logger logger = LoggerFactory.getLogger(ManifestAddon.class);
private final AddonManifest manifest;
private final List<AddonInitializer> initializers;
@Inject
private Platform platform;
private static final Logger logger = LoggerFactory.getLogger(ManifestAddon.class);
public ManifestAddon(AddonManifest manifest, List<AddonInitializer> initializers) {
this.manifest = manifest;
this.initializers = initializers;
}
public AddonManifest getManifest() {
return manifest;
}
@Override
public String getID() {
return manifest.getID();
@@ -46,12 +47,12 @@ public class ManifestAddon implements BaseAddon {
public void initialize() {
Injector<BaseAddon> addonInjector = Injector.get(this);
addonInjector.addExplicitTarget(BaseAddon.class);
Injector<Platform> platformInjector = Injector.get(platform);
platformInjector.addExplicitTarget(Platform.class);
logger.info("Initializing addon {}", getID());
logger.debug("Initializing addon {}", getID());
initializers.forEach(initializer -> {
logger.debug("Invoking entry point {}", initializer.getClass());
addonInjector.inject(initializer);
@@ -60,10 +61,6 @@ public class ManifestAddon implements BaseAddon {
});
}
public AddonManifest getManifest() {
return manifest;
}
@Override
public Map<String, VersionRange> getDependencies() {
return manifest.getDependencies();
@@ -13,16 +13,21 @@ import ca.solostudios.strata.version.VersionRange;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.yaml.YamlConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.dfsek.terra.addons.manifest.api.AddonInitializer;
import com.dfsek.terra.addons.manifest.impl.config.AddonManifest;
@@ -32,75 +37,76 @@ import com.dfsek.terra.addons.manifest.impl.config.loaders.VersionRangeLoader;
import com.dfsek.terra.addons.manifest.impl.exception.AddonException;
import com.dfsek.terra.addons.manifest.impl.exception.ManifestException;
import com.dfsek.terra.addons.manifest.impl.exception.ManifestNotPresentException;
import com.dfsek.terra.api.Platform;
import com.dfsek.terra.api.addon.bootstrap.BootstrapBaseAddon;
import com.dfsek.terra.api.inject.annotations.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ManifestAddonLoader implements BootstrapBaseAddon<ManifestAddon> {
private static final Logger LOGGER = LoggerFactory.getLogger(ManifestAddonLoader.class);
private static final Logger logger = LoggerFactory.getLogger(ManifestAddonLoader.class);
private static final Version VERSION = Versions.getVersion(1, 0, 0);
private final ConfigLoader manifestLoader = new ConfigLoader()
.registerLoader(Version.class, new VersionLoader())
.registerLoader(VersionRange.class, new VersionRangeLoader())
.registerLoader(WebsiteConfig.class, WebsiteConfig::new);
public ManifestAddon loadAddon(Path addonPath) {
try(JarFile jar = new JarFile(addonPath.toFile())) {
logger.debug("Loading addon from JAR {}", addonPath);
JarEntry manifestEntry = jar.getJarEntry("terra.addon.yml");
if(manifestEntry == null) {
throw new ManifestNotPresentException("Addon " + addonPath + " does not contain addon manifest.");
}
//noinspection NestedTryStatement
try {
AddonManifest manifest = manifestLoader.load(new AddonManifest(),
new YamlConfiguration(jar.getInputStream(manifestEntry),
"terra.addon.yml"));
logger.debug("Loading addon {}@{}", manifest.getID(), manifest.getVersion());
@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed", "resource" })
ManifestAddonClassLoader loader = new ManifestAddonClassLoader(new URL[]{ addonPath.toUri().toURL() },
getClass().getClassLoader());
List<AddonInitializer> initializers = manifest.getEntryPoints().stream().map(entryPoint -> {
try {
Object in = loader.loadClass(entryPoint).getConstructor().newInstance();
if(!(in instanceof AddonInitializer)) {
throw new AddonException(in.getClass() + " does not extend " + AddonInitializer.class);
}
return (AddonInitializer) in;
} catch(InvocationTargetException e) {
throw new AddonException("Exception occurred while instantiating addon", e);
} catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) {
throw new AddonException(String.format("No valid default constructor found in entry point %s", entryPoint), e);
} catch(ClassNotFoundException e) {
throw new AddonException(String.format("Entry point %s not found in JAR.", entryPoint), e);
}
}).collect(Collectors.toList());
return new ManifestAddon(manifest, initializers);
} catch(LoadException e) {
throw new ManifestException("Failed to load addon manifest", e);
}
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public Iterable<ManifestAddon> loadAddons(Path addonsFolder, ClassLoader parent) {
LOGGER.info("Loading addons...");
logger.debug("Loading addons...");
ConfigLoader manifestLoader = new ConfigLoader();
manifestLoader.registerLoader(Version.class, new VersionLoader())
.registerLoader(VersionRange.class, new VersionRangeLoader())
.registerLoader(WebsiteConfig.class, WebsiteConfig::new);
try {
return Files.walk(addonsFolder, 1)
.filter(path -> path.toFile().isFile() && path.toString().endsWith(".jar"))
.map(path -> {
try {
LOGGER.debug("Loading addon from JAR {}", path);
JarFile jar = new JarFile(path.toFile());
JarEntry manifestEntry = jar.getJarEntry("terra.addon.yml");
if(manifestEntry == null) {
throw new ManifestNotPresentException("Addon " + path + " does not contain addon manifest.");
}
try {
AddonManifest manifest = manifestLoader.load(new AddonManifest(),
new YamlConfiguration(jar.getInputStream(manifestEntry),
"terra.addon.yml"));
LOGGER.info("Loading addon {}", manifest.getID());
ManifestAddonClassLoader loader = new ManifestAddonClassLoader(new URL[]{ path.toUri().toURL() },
getClass().getClassLoader());
return new ManifestAddon(manifest, manifest.getEntryPoints().stream().map(entryPoint -> {
try {
Object in = loader.loadClass(entryPoint).getConstructor().newInstance();
if(!(in instanceof AddonInitializer)) {
throw new AddonException(in.getClass() + " does not extend " + AddonInitializer.class);
}
return (AddonInitializer) in;
} catch(InvocationTargetException e) {
throw new AddonException("Exception occurred while instantiating addon: ", e);
} catch(NoSuchMethodException | IllegalAccessException | InstantiationException e) {
throw new AddonException("No valid default constructor found in entry point " + entryPoint);
} catch(ClassNotFoundException e) {
throw new AddonException("Entry point " + entryPoint + " not found in JAR.");
}
}).collect(Collectors.toList()));
} catch(LoadException e) {
throw new ManifestException("Failed to load addon manifest", e);
}
} catch(IOException e) {
throw new UncheckedIOException(e);
}
}).collect(Collectors.toList());
try(Stream<Path> addons = Files.walk(addonsFolder, 1, FileVisitOption.FOLLOW_LINKS)) {
return addons.filter(path -> path.toFile().isFile())
.filter(path -> path.toFile().canRead())
.filter(path -> path.toString().endsWith(".jar"))
.map(this::loadAddon)
.collect(Collectors.toList());
} catch(IOException e) {
throw new UncheckedIOException(e);
}