From c5a54558fac0fc5ada3e573c6319be61a29ca857 Mon Sep 17 00:00:00 2001 From: piexel Date: Tue, 6 Oct 2026 14:04:11 +0200 Subject: [PATCH] Let the host choose the jar copy directory and the logger of pack loads ModuleHost.copyingJarsTo(directory) and loggingTo(logger) replace the fixed java.io.tmpdir and System.getLogger. A System.LoggerFinder would not work for a plugin, whose classes are not on the system class loader. --- CONTRACTS.md | 5 ++- DESIGN.md | 10 +++-- README.md | 6 ++- .../java/com/volmit/host/JarSnapshot.java | 1 + src/main/java/com/volmit/host/LoadedPack.java | 2 +- src/main/java/com/volmit/host/ModuleHost.java | 43 ++++++++++++++++--- .../java/com/volmit/host/PackModules.java | 21 ++++++--- .../java/com/volmit/host/LoadedPackTest.java | 31 +++++++++++++ 8 files changed, 97 insertions(+), 22 deletions(-) diff --git a/CONTRACTS.md b/CONTRACTS.md index ab90754e3..98450fcee 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -91,8 +91,9 @@ running Iris is the pack publisher's responsibility. fields and `reservingNamespaces("iris")`. Its methods accumulate, so the configuration can be built in steps. Load each pack with `LoadedPack.load(packRoot, host)` and close it only after nothing built from it is in use. -- Route `System.Logger` output (logger `com.volmit.host.PackModules`) to the server log, so the - per-pack module summary and missing-classes warning are visible. +- Pass a `System.Logger` that writes to the server log to `ModuleHost.loggingTo(...)`, so the + per-pack module summary and missing-classes warning are visible, and a cache directory of the host + to `ModuleHost.copyingJarsTo(...)` for the jar copies. - Provide the API at runtime from the plugin's class loader, for example by shading it into the Iris plugin jar. Pack jars must not ship it. - Pass the packages of built-in modules whose stages packs may require to diff --git a/DESIGN.md b/DESIGN.md index 971a05a70..928d241d2 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -102,7 +102,8 @@ only the copies. The originals are closed again as soon as they are copied. This works on one consistent set of jars: a jar rebuilt during the load affects the next load, not half of this one. -Copies live in a new directory `iris-pack-modules-*` under `java.io.tmpdir`, named by their +Copies live in a new directory `iris-pack-modules-*` under `java.io.tmpdir`, or under the directory +the host passes to `ModuleHost.copyingJarsTo(...)`, which is created when missing, named by their original file name, next to an owner file holding a fixed header and the id of the process that made it. Origins and messages always use the original path relative to the pack: the copy's path would mean nothing to the author. When an entry class turns out to come from another jar of the pack, the @@ -273,9 +274,10 @@ namespace. Its stage ids, settings ids and mantle keys start with its id, so thi pack modules out of reserved namespaces everywhere. Built-in modules, discovered with `DiscoveredModule.builtIn`, may use them. -The load summary and the missing-classes warning go through -`System.getLogger(PackModules.class.getName())`, so the host can route them with a -`System.LoggerFinder`. The summary lists display name, id, version and jar for each module in load +The load summary and the missing-classes warning go to the `System.Logger` the host passes to +`ModuleHost.loggingTo(...)`, by default `System.getLogger(PackModules.class.getName())`. A host +argument rather than a `System.LoggerFinder`: the JDK finds a finder through the system class loader, +which a plugin's classes are not on. The summary lists display name, id, version and jar for each module in load order, or says the pack has no module jars. `PackModulesSummary` and `MissingClassesWarning` build the texts and are tested on their own. diff --git a/README.md b/README.md index 2f05423e2..05468b9d7 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,9 @@ Settings com.example.flora.FloraSettings of module example:flora from modules/fl var host = ModuleHost.of(builtInModules) .exposing("art.arcane.iris.engine.modules") // besides the API .reservingNamespaces("iris") - .reservingFields(reservedHostFields); + .reservingFields(reservedHostFields) + .copyingJarsTo(cacheDirectory) // default: java.io.tmpdir + .loggingTo(serverLogger); // a System.Logger; default: System.getLogger // Per pack. try (var pack = LoadedPack.load(packRoot, host)) { @@ -287,7 +289,7 @@ Pack overworld: module jars use classes that neither the pack, Iris nor the JDK modules/stilt.jar: org.apache.logging.log4j.LogManager, org.apache.logging.log4j.Logger, org.slf4j.Logger, org.slf4j.LoggerFactory, org.slf4j.spi.LocationAwareLogger and 3 more ``` -A successful load logs one INFO summary through `System.Logger`: +A successful load logs one INFO summary to the host's `System.Logger`: ``` Pack overworld uses 2 modules: diff --git a/src/main/java/com/volmit/host/JarSnapshot.java b/src/main/java/com/volmit/host/JarSnapshot.java index e34e022a3..77334bd29 100644 --- a/src/main/java/com/volmit/host/JarSnapshot.java +++ b/src/main/java/com/volmit/host/JarSnapshot.java @@ -95,6 +95,7 @@ final class JarSnapshot { private static Path createOwnedDirectory(Path temporaryRoot) { try { + Files.createDirectories(temporaryRoot); var directory = Files.createTempDirectory(temporaryRoot, DIRECTORY_PREFIX); Files.writeString(directory.resolve(OWNER_FILE), ownerRecord(ProcessHandle.current().pid()), StandardCharsets.UTF_8); return directory; diff --git a/src/main/java/com/volmit/host/LoadedPack.java b/src/main/java/com/volmit/host/LoadedPack.java index d57cd9cba..83c005029 100644 --- a/src/main/java/com/volmit/host/LoadedPack.java +++ b/src/main/java/com/volmit/host/LoadedPack.java @@ -34,7 +34,7 @@ public final class LoadedPack implements AutoCloseable { */ public static LoadedPack load(Path packRoot, ModuleHost host) { Objects.requireNonNull(host, "host"); - var packModules = PackModules.load(packRoot, host.exposedHostPackages()); + var packModules = PackModules.load(packRoot, host.exposedHostPackages(), host.jarCopyRoot(), host.logger()); try { var loaded = LoadedModules.load(host.withPackModules(packModules.modules()), host.reservedHostFields(), host.reservedNamespaces()); diff --git a/src/main/java/com/volmit/host/ModuleHost.java b/src/main/java/com/volmit/host/ModuleHost.java index 5e1c3bbb6..439eb0db7 100644 --- a/src/main/java/com/volmit/host/ModuleHost.java +++ b/src/main/java/com/volmit/host/ModuleHost.java @@ -2,6 +2,7 @@ package com.volmit.host; import com.volmit.FeatureAttachment; import com.volmit.IrisModule; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.EnumMap; @@ -32,40 +33,66 @@ public record ModuleHost( List builtIns, Set exposedHostPackages, Map> reservedHostFields, - Set reservedNamespaces) { + Set reservedNamespaces, + Path jarCopyRoot, + System.Logger logger) { + private static final String TEMPORARY_DIRECTORY_PROPERTY = "java.io.tmpdir"; + public ModuleHost { builtIns = List.copyOf(builtIns); exposedHostPackages = Set.copyOf(exposedHostPackages); reservedHostFields = copyOf(reservedHostFields); reservedNamespaces = Set.copyOf(reservedNamespaces); + Objects.requireNonNull(jarCopyRoot, "jarCopyRoot"); + Objects.requireNonNull(logger, "logger"); ExposedPackages.apiAnd(exposedHostPackages); ReservedNamespaces.of(reservedNamespaces); requireFieldNames(reservedHostFields); } - /** The given built-in modules, exposing only the Iris API and reserving nothing. */ + /** The given built-in modules, exposing only the Iris API and reserving nothing. Jar copies go to + * {@code java.io.tmpdir} and the load log to the {@link System.Logger} named after {@link PackModules}. + */ public static ModuleHost of(List builtInModules) { var builtIns = builtInModules.stream().map(DiscoveredModule::builtIn).toList(); - return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of()); + return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), systemTemporaryDirectory(), + System.getLogger(PackModules.class.getName())); } /** 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); + return new ModuleHost(builtIns, union(exposedHostPackages, packages), reservedHostFields, reservedNamespaces, + jarCopyRoot, logger); } /** This configuration with {@code namespaces} reserved for built-in modules. */ public ModuleHost reservingNamespaces(String... namespaces) { - return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, union(reservedNamespaces, namespaces)); + return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, union(reservedNamespaces, namespaces), + jarCopyRoot, logger); } /** This configuration with {@code fields} also reserved as JSON fields the host itself uses, * merged per attachment target with the fields reserved before. */ public ModuleHost reservingFields(Map> fields) { - return new ModuleHost(builtIns, exposedHostPackages, merge(reservedHostFields, fields), reservedNamespaces); + return new ModuleHost(builtIns, exposedHostPackages, merge(reservedHostFields, fields), reservedNamespaces, + jarCopyRoot, logger); + } + + /** This configuration copying module jars into new directories below {@code directory}, such as a + * cache directory of the host, which is created when missing. The last call wins. + */ + public ModuleHost copyingJarsTo(Path directory) { + return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, reservedNamespaces, directory, logger); + } + + /** This configuration logging each pack load's module summary and missing-classes warning to + * {@code logger}, such as an adapter to the host's own log. The last call wins. + */ + public ModuleHost loggingTo(System.Logger logger) { + return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, reservedNamespaces, jarCopyRoot, logger); } /** The built-in modules followed by a pack's modules, in the order {@link LoadedModules} installs them. */ @@ -75,6 +102,10 @@ public record ModuleHost( return modules; } + private static Path systemTemporaryDirectory() { + return Path.of(System.getProperty(TEMPORARY_DIRECTORY_PROPERTY)); + } + private static Map> copyOf(Map> fields) { var copy = new EnumMap>(FeatureAttachment.Target.class); fields.forEach((target, names) -> copy.put(target, Set.copyOf(names))); diff --git a/src/main/java/com/volmit/host/PackModules.java b/src/main/java/com/volmit/host/PackModules.java index d0aa74c87..693699d79 100644 --- a/src/main/java/com/volmit/host/PackModules.java +++ b/src/main/java/com/volmit/host/PackModules.java @@ -36,8 +36,9 @@ import java.util.stream.Collectors; * them runs; they are reported as {@link #missingClasses()}, except those in the optional packages * the jar's module declares in its {@link com.volmit.ModuleInfo}. * - *

A successful load logs one INFO summary of the pack's modules through {@link System.Logger}, - * which the host can route to its own logging, and one WARNING listing the missing classes, if any. + *

A successful load logs one INFO summary of the pack's modules through a {@link System.Logger}, by + * default the one named after this class, or the host's from {@link ModuleHost#loggingTo}, and one + * WARNING listing the missing classes, if any. * *

Close this when the pack unloads, after dropping everything built from its modules, such as * {@link LoadedModules}. Classes not loaded before then can no longer be loaded. @@ -46,7 +47,7 @@ public final class PackModules implements AutoCloseable { public static final String MODULES_DIRECTORY = "modules"; static final String JAR_EXTENSION = ".jar"; private static final String TEMPORARY_DIRECTORY_PROPERTY = "java.io.tmpdir"; - private static final System.Logger LOGGER = System.getLogger(PackModules.class.getName()); + private static final System.Logger DEFAULT_LOGGER = System.getLogger(PackModules.class.getName()); private final List modules; private final List missingClasses; @@ -80,13 +81,19 @@ public final class PackModules implements AutoCloseable { /** Like {@link #load(Path, Collection)}, copying the jars below {@code temporaryRoot}. */ static PackModules load(Path packRoot, Collection exposedHostPackages, Path temporaryRoot) { + return load(packRoot, exposedHostPackages, temporaryRoot, DEFAULT_LOGGER); + } + + /** Like {@link #load(Path, Collection)}, copying the jars below {@code temporaryRoot} and logging to {@code logger}. */ + static PackModules load(Path packRoot, Collection exposedHostPackages, Path temporaryRoot, System.Logger logger) { Objects.requireNonNull(packRoot, "packRoot"); + Objects.requireNonNull(logger, "logger"); var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages)); StaleJarSnapshots.remove(temporaryRoot); var snapshot = JarSnapshot.take(packRoot, findModuleJars(packRoot), temporaryRoot); try { var packModules = loadSnapshot(packRoot, snapshot, host); - logLoad(packName(packRoot), packModules); + logLoad(logger, packName(packRoot), packModules); return packModules; } catch (RuntimeException | Error failure) { snapshot.deleteAfterFailure(failure); @@ -164,10 +171,10 @@ public final class PackModules implements AutoCloseable { return new URLClassLoader("pack:" + packName(packRoot), urls, host); } - private static void logLoad(String packName, PackModules packModules) { - LOGGER.log(Level.INFO, () -> PackModulesSummary.describe(packName, packModules.modules())); + private static void logLoad(System.Logger logger, String packName, PackModules packModules) { + logger.log(Level.INFO, () -> PackModulesSummary.describe(packName, packModules.modules())); if (packModules.missingClasses().isEmpty()) return; - LOGGER.log(Level.WARNING, () -> MissingClassesWarning.describe(packName, packModules.missingClasses())); + logger.log(Level.WARNING, () -> MissingClassesWarning.describe(packName, packModules.missingClasses())); } private static String packName(Path packRoot) { diff --git a/src/test/java/com/volmit/host/LoadedPackTest.java b/src/test/java/com/volmit/host/LoadedPackTest.java index 7f689448b..3ed0c75d9 100644 --- a/src/test/java/com/volmit/host/LoadedPackTest.java +++ b/src/test/java/com/volmit/host/LoadedPackTest.java @@ -105,4 +105,35 @@ class LoadedPackTest { assertEquals(Set.of("iris", "vanilla"), host.reservedNamespaces()); assertEquals(Map.of(Target.BIOME, Set.of("carving", "objects"), Target.REGION, Set.of("spawns")), host.reservedHostFields()); } + + @Test + void copiesJarsWhereTheHostSaysAndLogsToTheHostsLogger(@TempDir Path cache) throws java.io.IOException { + writeModule("user:core", NO_STAGES); + var copies = cache.resolve("module-jars"); + var log = new RecordingLogger(); + + try (var ignored = LoadedPack.load(pack, HOST.copyingJarsTo(copies).loggingTo(log))) { + assertTrue(java.nio.file.Files.exists(copies), "a missing copy directory is created"); + try (var entries = java.nio.file.Files.list(copies)) { + assertEquals(1, entries.filter(entry -> entry.getFileName().toString().startsWith(JarSnapshot.DIRECTORY_PREFIX)).count()); + } + } + + assertEquals(List.of("INFO Pack " + pack.getFileName() + " uses 1 module:"), log.levelsAndFirstLines()); + } + + /** Keeps the level and first line of every message, which is enough to see where the summary went. */ + private static final class RecordingLogger implements System.Logger { + private final List lines = new java.util.ArrayList<>(); + + List levelsAndFirstLines() { return lines; } + public String getName() { return "recording"; } + public boolean isLoggable(Level level) { return true; } + public void log(Level level, java.util.ResourceBundle bundle, String message, Throwable thrown) { record(level, message); } + public void log(Level level, java.util.ResourceBundle bundle, String format, Object... parameters) { record(level, format); } + + private void record(Level level, String message) { + lines.add(level + " " + message.lines().findFirst().orElse("")); + } + } }