mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
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:
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisModule;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/** The host's side of every pack load, the same for all packs: its built-in modules, the host
|
||||
* packages pack modules may use besides the Iris API, the JSON fields the host itself uses, and the
|
||||
* namespaces only built-in modules may use. Build it once and pass it to every
|
||||
* {@link LoadedPack#load(java.nio.file.Path, ModuleHost)}.
|
||||
*
|
||||
* <pre>{@code
|
||||
* var host = ModuleHost.of(builtInModules)
|
||||
* .exposing("art.arcane.iris.engine.modules")
|
||||
* .reservingNamespaces("iris")
|
||||
* .reservingFields(reservedHostFields);
|
||||
* }</pre>
|
||||
*
|
||||
* Built-in modules' {@code info()} is read here, once. Exposed package names and reserved namespaces
|
||||
* are checked here, so a mistake fails when the host starts rather than at the first pack load.
|
||||
*/
|
||||
public record ModuleHost(
|
||||
List<DiscoveredModule> builtIns,
|
||||
Set<String> exposedHostPackages,
|
||||
Map<FeatureAttachment.Target, Set<String>> reservedHostFields,
|
||||
Set<String> reservedNamespaces) {
|
||||
public ModuleHost {
|
||||
builtIns = List.copyOf(builtIns);
|
||||
exposedHostPackages = Set.copyOf(exposedHostPackages);
|
||||
reservedHostFields = copyOf(reservedHostFields);
|
||||
reservedNamespaces = Set.copyOf(reservedNamespaces);
|
||||
ExposedPackages.apiAnd(exposedHostPackages);
|
||||
ReservedNamespaces.of(reservedNamespaces);
|
||||
}
|
||||
|
||||
/** The given built-in modules, exposing only the Iris API and reserving nothing. */
|
||||
public static ModuleHost of(List<? extends IrisModule> builtInModules) {
|
||||
var builtIns = builtInModules.stream().map(DiscoveredModule::builtIn).toList();
|
||||
return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of());
|
||||
}
|
||||
|
||||
/** This configuration with {@code packages}, each with its subpackages, also exposed to pack
|
||||
* modules, such as the packages that hold built-in modules' stage constants.
|
||||
*/
|
||||
public ModuleHost exposing(String... packages) {
|
||||
return new ModuleHost(builtIns, union(exposedHostPackages, packages), reservedHostFields, reservedNamespaces);
|
||||
}
|
||||
|
||||
/** This configuration with {@code namespaces} reserved for built-in modules. */
|
||||
public ModuleHost reservingNamespaces(String... namespaces) {
|
||||
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, union(reservedNamespaces, namespaces));
|
||||
}
|
||||
|
||||
/** This configuration with {@code fields} as the JSON fields the host itself uses, per target. */
|
||||
public ModuleHost reservingFields(Map<FeatureAttachment.Target, Set<String>> fields) {
|
||||
return new ModuleHost(builtIns, exposedHostPackages, fields, reservedNamespaces);
|
||||
}
|
||||
|
||||
/** The built-in modules followed by a pack's modules, in the order {@link LoadedModules} installs them. */
|
||||
List<DiscoveredModule> withPackModules(List<DiscoveredModule> packModules) {
|
||||
var modules = new ArrayList<>(builtIns);
|
||||
modules.addAll(Objects.requireNonNull(packModules, "packModules"));
|
||||
return modules;
|
||||
}
|
||||
|
||||
private static Map<FeatureAttachment.Target, Set<String>> copyOf(Map<FeatureAttachment.Target, Set<String>> fields) {
|
||||
var copy = new EnumMap<FeatureAttachment.Target, Set<String>>(FeatureAttachment.Target.class);
|
||||
fields.forEach((target, names) -> copy.put(target, Set.copyOf(names)));
|
||||
return Map.copyOf(copy);
|
||||
}
|
||||
|
||||
private static Set<String> union(Set<String> existing, String... added) {
|
||||
var names = new TreeSet<>(existing);
|
||||
names.addAll(Arrays.asList(added));
|
||||
return names;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user