Add LoadedPack and ModuleHost as the host's single entry point for a pack

ModuleHost holds what the host passes to every pack load: built-in
modules (info read once), exposed host packages, reserved JSON fields and
reserved namespaces, validated when built. LoadedPack.load(packRoot, host)
loads the pack's jars, installs them with the built-ins and resolves both
pipelines; on any failure it closes the pack before rethrowing, and
close() releases the pack's class loader. PackModules and LoadedModules
stay available as the lower-level pieces.
This commit is contained in:
piexel committed 2026-10-05 21:35:52 +02:00
1 parent 307e488d1a
commit fe51496ab0
3 files changed
+253

No files matched your search

@@ -0,0 +1,80 @@
package com.volmit.host;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
/** One loaded pack: its module jars and everything derived from them together with the host's
* built-in modules. This is the host's entry point for loading and unloading a pack's modules.
*
* <pre>{@code
* try (var pack = LoadedPack.load(packRoot, host)) {
* var placer = new PlacementRunner(pack.placement());
* ...
* }
* }</pre>
*
* A load either returns a complete pack or throws with nothing left open. Close the pack when it
* unloads, after the host has stopped using its graphs, catalog and runners: closing releases the
* pack's class loader.
*/
public final class LoadedPack implements AutoCloseable {
private final PackModules packModules;
private final LoadedModules loaded;
private LoadedPack(PackModules packModules, LoadedModules loaded) {
this.packModules = packModules;
this.loaded = loaded;
}
/** Loads the pack's module jars, installs them with the host's built-in modules and resolves both
* pipelines. Fails with the first load error, naming the jar or module at fault.
*/
public static LoadedPack load(Path packRoot, ModuleHost host) {
Objects.requireNonNull(host, "host");
var packModules = PackModules.load(packRoot, host.exposedHostPackages());
try {
var loaded = LoadedModules.load(host.withPackModules(packModules.modules()),
host.reservedHostFields(), host.reservedNamespaces());
return new LoadedPack(packModules, loaded);
} catch (RuntimeException | Error failure) {
closeAfterFailure(packModules, failure);
throw failure;
}
}
/** The pack's own modules, one per jar, in jar file name order. */
public List<DiscoveredModule> packModules() {
return packModules.modules();
}
/** Classes the pack's jars use that nobody has, per jar; see {@link PackModules#missingClasses()}. */
public List<MissingClasses> missingClasses() {
return packModules.missingClasses();
}
public FeatureCatalog catalog() {
return loaded.catalog();
}
public ResolvedGraph placement() {
return loaded.placement();
}
public ResolvedGraph chunk() {
return loaded.chunk();
}
@Override
public void close() {
packModules.close();
}
private static void closeAfterFailure(PackModules packModules, Throwable failure) {
try {
packModules.close();
} catch (RuntimeException exception) {
failure.addSuppressed(exception);
}
}
}