From 98c090d5b21b8b642fc88981fce3d5d354a24121 Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 22 Oct 2026 09:34:46 +0200 Subject: [PATCH] A host can bundle module jars that load with every pack ModuleHost.bundlingJars(...) names jars the host ships itself. Every pack load copies and loads them as if they were in the pack's modules folder: first, in the given order, in the pack's class loader, so pack jars can still require their stages. A pack jar with a bundled jar's file name, such as an old copy, is left out, so a pack never loads a stale copy next to the host's; LoadedPack.replacedJars() names it for the host to tell the owner. Messages name a bundled jar " (bundled with the host)". JarSnapshot now takes each jar's origin from its caller, since a bundled jar does not lie under the pack. --- .../com/volmit/host/DiscoveredModule.java | 2 +- .../java/com/volmit/host/JarSnapshot.java | 31 +++++---- src/main/java/com/volmit/host/LoadedPack.java | 12 +++- src/main/java/com/volmit/host/ModuleHost.java | 45 ++++++++++--- src/main/java/com/volmit/host/ModuleJar.java | 4 +- .../java/com/volmit/host/PackModules.java | 63 ++++++++++++++++--- .../java/com/volmit/host/LoadedPackTest.java | 41 ++++++++++++ 7 files changed, 164 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/volmit/host/DiscoveredModule.java b/src/main/java/com/volmit/host/DiscoveredModule.java index 1097443e9..4e3eaad62 100644 --- a/src/main/java/com/volmit/host/DiscoveredModule.java +++ b/src/main/java/com/volmit/host/DiscoveredModule.java @@ -78,7 +78,7 @@ public final class DiscoveredModule { return log; } - /** The jar path relative to the pack, or "host classpath" for built-in modules. */ + /** The jar path relative to the pack, a bundled jar's name, or "host classpath" for built-in modules. */ public String origin() { return origin; } diff --git a/src/main/java/com/volmit/host/JarSnapshot.java b/src/main/java/com/volmit/host/JarSnapshot.java index 77334bd29..f8a043463 100644 --- a/src/main/java/com/volmit/host/JarSnapshot.java +++ b/src/main/java/com/volmit/host/JarSnapshot.java @@ -24,7 +24,15 @@ final class JarSnapshot { static final String OWNER_FILE = "owner.txt"; static final String OWNER_HEADER = "Iris pack module jar copies"; - /** A module jar: its path relative to the pack, which every message names, and its copy. */ + /** A module jar to copy: its origin, which every message names, and the file. */ + record SourceJar(String origin, Path file) { + SourceJar { + Objects.requireNonNull(origin, "origin"); + Objects.requireNonNull(file, "file"); + } + } + + /** A module jar: its origin, which every message names, and its copy. */ record CopiedJar(String origin, Path copy) { CopiedJar { Objects.requireNonNull(origin, "origin"); @@ -40,15 +48,15 @@ final class JarSnapshot { this.jars = List.copyOf(jars); } - /** Copies {@code jars}, which lie under {@code packRoot}, into a new directory below - * {@code temporaryRoot}. Leaves nothing behind if a copy fails. + /** Copies {@code jars}, whose file names differ, into a new directory below {@code temporaryRoot}. Leaves nothing + * behind if a copy fails. */ - static JarSnapshot take(Path packRoot, List jars, Path temporaryRoot) { + static JarSnapshot take(List jars, Path temporaryRoot) { var directory = createOwnedDirectory(temporaryRoot); var copies = new ArrayList(jars.size()); try { for (var jar : jars) { - copies.add(copy(packRoot, jar, directory)); + copies.add(copy(jar, directory)); } return new JarSnapshot(directory, copies); } catch (RuntimeException | Error failure) { @@ -61,7 +69,7 @@ final class JarSnapshot { return jars; } - /** The pack-relative origin of a copy, or {@code null} if {@code location} is not one of them. */ + /** The origin of a copy, or {@code null} if {@code location} is not one of them. */ String originOf(Path location) { return jars.stream() .filter(jar -> jar.copy().equals(location)) @@ -109,14 +117,13 @@ final class JarSnapshot { return OWNER_HEADER + "\n" + pid + "\n"; } - private static CopiedJar copy(Path packRoot, Path jar, Path directory) { - var origin = packRoot.relativize(jar).toString(); - var copy = directory.resolve(jar.getFileName().toString()); + private static CopiedJar copy(SourceJar jar, Path directory) { + var copy = directory.resolve(jar.file().getFileName().toString()); try { - Files.copy(jar, copy); - return new CopiedJar(origin, copy); + Files.copy(jar.file(), copy); + return new CopiedJar(jar.origin(), copy); } catch (IOException exception) { - throw new UncheckedIOException("Cannot copy module jar " + origin, exception); + throw new UncheckedIOException("Cannot copy module jar " + jar.origin(), exception); } } } diff --git a/src/main/java/com/volmit/host/LoadedPack.java b/src/main/java/com/volmit/host/LoadedPack.java index ed65a833e..dc5e5c9c2 100644 --- a/src/main/java/com/volmit/host/LoadedPack.java +++ b/src/main/java/com/volmit/host/LoadedPack.java @@ -34,7 +34,8 @@ 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(), host.jarCopyRoot(), host.logger()); + var packModules = PackModules.load(packRoot, host.exposedHostPackages(), host.bundledJars(), host.jarCopyRoot(), + host.logger()); try { var loaded = LoadedModules.load(host.withPackModules(packModules.modules()), host.reservedHostFields(), host.hostTypePackages(), host.reservedFolders()); @@ -45,11 +46,18 @@ public final class LoadedPack implements AutoCloseable { } } - /** The pack's own modules, one per jar, in jar file name order. */ + /** The modules of the host's bundled jars, in the order the host lists them, then the pack's own, one per jar, in jar + * file name order. + */ public List packModules() { return packModules.modules(); } + /** The pack's own jars that a jar the host bundles replaced; see {@link PackModules#replacedJars()}. */ + public List replacedJars() { + return packModules.replacedJars(); + } + /** Classes the pack's jars use that nobody has, per jar; see {@link PackModules#missingClasses()}. */ public List missingClasses() { return packModules.missingClasses(); diff --git a/src/main/java/com/volmit/host/ModuleHost.java b/src/main/java/com/volmit/host/ModuleHost.java index 74766571e..de7f5882c 100644 --- a/src/main/java/com/volmit/host/ModuleHost.java +++ b/src/main/java/com/volmit/host/ModuleHost.java @@ -13,7 +13,7 @@ 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 +/** The host's side of every pack load, the same for all packs: its built-in modules, the module jars it bundles, the host * packages pack modules may use besides the Iris API, the JSON fields the host itself uses (which no * settings key may take), the packages of the classes the host describes itself and the pack folders it * reads itself (which no resource kind may read). Build it once and pass it to every @@ -37,6 +37,7 @@ public record ModuleHost( Map> reservedHostFields, Set hostTypePackages, Set reservedFolders, + List bundledJars, Path jarCopyRoot, System.Logger logger) { private static final String TEMPORARY_DIRECTORY_PROPERTY = "java.io.tmpdir"; @@ -47,12 +48,14 @@ public record ModuleHost( reservedHostFields = copyOf(reservedHostFields); hostTypePackages = Set.copyOf(hostTypePackages); reservedFolders = Set.copyOf(reservedFolders); + bundledJars = List.copyOf(bundledJars); Objects.requireNonNull(jarCopyRoot, "jarCopyRoot"); Objects.requireNonNull(logger, "logger"); ExposedPackages.apiAnd(exposedHostPackages); HostTypes.of(hostTypePackages); requireFieldNames(reservedHostFields); requireFolderNames(reservedFolders); + requireJarFileNames(bundledJars); } /** The given built-in modules, exposing only the Iris API and reserving nothing. Jar copies go to @@ -60,7 +63,7 @@ public record ModuleHost( */ public static ModuleHost of(List builtInModules) { var builtIns = builtInModules.stream().map(DiscoveredModule::builtIn).toList(); - return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), Set.of(), systemTemporaryDirectory(), + return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), Set.of(), List.of(), systemTemporaryDirectory(), PackModules.DEFAULT_LOGGER); } @@ -69,7 +72,7 @@ public record ModuleHost( */ public ModuleHost exposing(String... packages) { return new ModuleHost(builtIns, union(exposedHostPackages, packages), reservedHostFields, - hostTypePackages, reservedFolders, jarCopyRoot, logger); + hostTypePackages, reservedFolders, bundledJars, jarCopyRoot, logger); } /** This configuration with {@code fields} also reserved as JSON fields the host itself uses, @@ -77,7 +80,7 @@ public record ModuleHost( */ public ModuleHost reservingFields(Map> fields) { return new ModuleHost(builtIns, exposedHostPackages, merge(reservedHostFields, fields), - hostTypePackages, reservedFolders, jarCopyRoot, logger); + hostTypePackages, reservedFolders, bundledJars, jarCopyRoot, logger); } /** This configuration with the classes in {@code packages}, each with its subpackages, described by @@ -87,7 +90,7 @@ public record ModuleHost( */ public ModuleHost describingHostTypesIn(String... packages) { return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, - union(hostTypePackages, packages), reservedFolders, jarCopyRoot, logger); + union(hostTypePackages, packages), reservedFolders, bundledJars, jarCopyRoot, logger); } /** This configuration with {@code folders}, folder names directly below a pack's root, also read by the host @@ -95,7 +98,20 @@ public record ModuleHost( */ public ModuleHost reservingFolders(String... folders) { return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages, - union(reservedFolders, folders), jarCopyRoot, logger); + union(reservedFolders, folders), bundledJars, jarCopyRoot, logger); + } + + /** This configuration also loading {@code jars}, module jars the host ships, with every pack, as if they were in its + * {@code modules} folder: before the pack's own jars, in the given order, in the same class loader. A pack jar with the + * same file name, such as an old copy, is left out, so a pack can never load a stale copy; {@link LoadedPack#replacedJars()} + * names it, for the host to tell the pack's owner. Their file names must differ and end in {@code .jar}; whether they + * exist is checked when a pack loads. + */ + public ModuleHost bundlingJars(Path... jars) { + var bundled = new ArrayList<>(bundledJars); + bundled.addAll(Arrays.asList(jars)); + return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages, reservedFolders, + bundled, jarCopyRoot, logger); } /** This configuration copying module jars into new directories below {@code directory}, such as a @@ -103,7 +119,7 @@ public record ModuleHost( */ public ModuleHost copyingJarsTo(Path directory) { return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages, - reservedFolders, directory, logger); + reservedFolders, bundledJars, directory, logger); } /** This configuration logging each pack load's module summary and missing-classes warning, and every module's own @@ -114,7 +130,7 @@ public record ModuleHost( Objects.requireNonNull(logger, "logger"); var loggedBuiltIns = builtIns.stream().map(builtIn -> builtIn.loggingTo(logger)).toList(); return new ModuleHost(loggedBuiltIns, exposedHostPackages, reservedHostFields, hostTypePackages, - reservedFolders, jarCopyRoot, logger); + reservedFolders, bundledJars, jarCopyRoot, logger); } /** The built-in modules followed by a pack's modules, in the order {@link LoadedModules} installs them. */ @@ -148,6 +164,19 @@ public record ModuleHost( } } + private static void requireJarFileNames(List jars) { + var names = new TreeSet(); + for (var jar : jars) { + var name = Objects.requireNonNull(jar, "bundled jar").getFileName(); + if (name == null || !name.toString().endsWith(PackModules.JAR_EXTENSION)) { + throw new IllegalArgumentException("Bundled module jar " + jar + " is not a .jar file"); + } + if (!names.add(name.toString())) { + throw new IllegalArgumentException("Two bundled module jars are named " + name); + } + } + } + private static Map> merge( Map> existing, Map> added) { diff --git a/src/main/java/com/volmit/host/ModuleJar.java b/src/main/java/com/volmit/host/ModuleJar.java index 5a1e79c2a..403c77023 100644 --- a/src/main/java/com/volmit/host/ModuleJar.java +++ b/src/main/java/com/volmit/host/ModuleJar.java @@ -24,8 +24,8 @@ import java.util.zip.ZipFile; /** One jar in a pack's modules directory, the entry classes it names and the classes it holds, * each with the classes it links against. {@code path} is the load's copy of the jar - * ({@link JarSnapshot}); {@code origin} is the original's path relative to the pack, which every - * message names. + * ({@link JarSnapshot}); {@code origin}, the original's path relative to the pack or a bundled jar's name, is what + * every message names. * *

The entry points are the jar's own {@code META-INF/services/com.volmit.IrisModule} file, in * the format {@link java.util.ServiceLoader} reads: one module class per line, so a jar holds as many diff --git a/src/main/java/com/volmit/host/PackModules.java b/src/main/java/com/volmit/host/PackModules.java index 7a11d78ad..8a7ad63b5 100644 --- a/src/main/java/com/volmit/host/PackModules.java +++ b/src/main/java/com/volmit/host/PackModules.java @@ -14,6 +14,7 @@ import java.util.List; import java.util.Objects; import java.util.function.Predicate; import java.util.stream.Collectors; +import java.util.stream.Stream; /** The module jars of one pack: every {@code /modules/*.jar}, each with the modules it lists, * ordered by file name so the result never depends on directory listing order. Hosts normally load packs @@ -46,18 +47,21 @@ import java.util.stream.Collectors; public final class PackModules implements AutoCloseable { public static final String MODULES_DIRECTORY = "modules"; static final String JAR_EXTENSION = ".jar"; + static final String BUNDLED_ORIGIN_SUFFIX = " (bundled with the host)"; private static final String TEMPORARY_DIRECTORY_PROPERTY = "java.io.tmpdir"; static final System.Logger DEFAULT_LOGGER = System.getLogger(PackModules.class.getName()); private final List modules; private final List missingClasses; + private final List replacedJars; private final URLClassLoader loader; private final JarSnapshot snapshot; - private PackModules( - List modules, List missingClasses, URLClassLoader loader, JarSnapshot snapshot) { + private PackModules(List modules, List missingClasses, List replacedJars, + URLClassLoader loader, JarSnapshot snapshot) { this.modules = List.copyOf(modules); this.missingClasses = List.copyOf(missingClasses); + this.replacedJars = List.copyOf(replacedJars); this.loader = loader; this.snapshot = snapshot; } @@ -86,13 +90,25 @@ public final class PackModules implements AutoCloseable { /** 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) { + return load(packRoot, exposedHostPackages, List.of(), temporaryRoot, logger); + } + + /** Like {@link #load(Path, Collection, Path, System.Logger)}, also loading the host's {@code bundledJars} as if they were + * in the pack, before its own jars and in the given order. A pack jar with a bundled jar's file name, such as an old copy + * of it, is left out, so the host's copy is the one that loads; {@link #replacedJars()} names it. + */ + static PackModules load(Path packRoot, Collection exposedHostPackages, List bundledJars, 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); + var packJars = findModuleJars(packRoot); + var replacedPackJars = packJarsNamedLike(packJars, bundledJars); + var snapshot = JarSnapshot.take(sourceJars(packRoot, bundledJars, packJars, replacedPackJars), temporaryRoot); + var replacedJars = replacedPackJars.stream().map(jar -> packRoot.relativize(jar).toString()).toList(); try { - var packModules = loadSnapshot(packRoot, snapshot, host, logger); + var packModules = loadSnapshot(packRoot, snapshot, replacedJars, host, logger); logLoad(logger, packName(packRoot), packModules); return packModules; } catch (RuntimeException | Error failure) { @@ -114,6 +130,11 @@ public final class PackModules implements AutoCloseable { return missingClasses; } + /** The pack's own jars, relative to the pack, that a bundled jar of the same file name replaced, in file name order. */ + public List replacedJars() { + return replacedJars; + } + /** Closes the class loader, then deletes the jar copies, which an open loader would keep locked on Windows. */ @Override public void close() { @@ -131,10 +152,11 @@ public final class PackModules implements AutoCloseable { return Path.of(System.getProperty(TEMPORARY_DIRECTORY_PROPERTY)); } - private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, ExposedHostClassLoader host, System.Logger logger) { + private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, List replacedJars, + ExposedHostClassLoader host, System.Logger logger) { var jars = snapshot.jars().stream().map(ModuleJar::read).toList(); var missingClasses = PackClassCheck.requireLinkable(jars, host); - return defineModules(packRoot, jars, missingClasses, host, snapshot, logger); + return defineModules(packRoot, jars, missingClasses, replacedJars, host, snapshot, logger); } private static List findModuleJars(Path packRoot) { @@ -150,17 +172,40 @@ public final class PackModules implements AutoCloseable { } } + private static List packJarsNamedLike(List packJars, List bundledJars) { + var bundledNames = bundledJars.stream().map(PackModules::fileName).collect(Collectors.toSet()); + return packJars.stream().filter(jar -> bundledNames.contains(fileName(jar))).toList(); + } + + private static List sourceJars(Path packRoot, List bundledJars, List packJars, + List replacedPackJars) { + var bundled = bundledJars.stream().map(jar -> new JarSnapshot.SourceJar(bundledOrigin(jar), jar)); + var own = packJars.stream() + .filter(Predicate.not(replacedPackJars::contains)) + .map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar)); + return Stream.concat(bundled, own).toList(); + } + + /** How messages name a bundled jar: its file name, marked as the host's. */ + static String bundledOrigin(Path bundledJar) { + return fileName(bundledJar) + BUNDLED_ORIGIN_SUFFIX; + } + + private static String fileName(Path jar) { + return jar.getFileName().toString(); + } + private static boolean isJar(Path path) { return Files.isRegularFile(path) && path.getFileName().toString().endsWith(JAR_EXTENSION); } private static PackModules defineModules( - Path packRoot, List jars, List missingClasses, ClassLoader host, JarSnapshot snapshot, - System.Logger logger) { + Path packRoot, List jars, List missingClasses, List replacedJars, ClassLoader host, + JarSnapshot snapshot, System.Logger logger) { var loader = newPackClassLoader(packRoot, jars, host); try { var modules = createModules(jars, loader, snapshot, logger); - return new PackModules(modules, withoutDeclaredOptional(missingClasses, modules), loader, snapshot); + return new PackModules(modules, withoutDeclaredOptional(missingClasses, modules), replacedJars, loader, snapshot); } catch (RuntimeException | Error failure) { closeAfterFailure(loader, failure); throw failure; diff --git a/src/test/java/com/volmit/host/LoadedPackTest.java b/src/test/java/com/volmit/host/LoadedPackTest.java index 72b364a15..42588c47c 100644 --- a/src/test/java/com/volmit/host/LoadedPackTest.java +++ b/src/test/java/com/volmit/host/LoadedPackTest.java @@ -121,6 +121,47 @@ class LoadedPackTest { assertEquals(List.of("INFO Pack " + pack.getFileName() + " uses 1 module:"), log.levelsAndFirstLines()); } + @Test + void bundledJarsLoadWithEveryPackBeforeItsOwnJarsInItsClassLoader(@TempDir Path build) { + var bundledJar = module("host.bundled.BundledModule", "bundled:core", + "public static final PlacementStage MARK = Stage.placement(\"bundled:core/mark\");", + "graph.define(MARK).run(context -> {});") + .writeInto(build, "bundled.jar"); + module("pack.user.User", "user:core", NO_MEMBERS, + "graph.define(Stage.placement(\"user:core/after\")).requires(host.bundled.BundledModule.MARK).run(context -> {});") + .compileAgainst(bundledJar) + .writeInto(pack, "user.jar"); + + try (var loaded = LoadedPack.load(pack, HOST.bundlingJars(bundledJar))) { + assertEquals(List.of("bundled.jar" + PackModules.BUNDLED_ORIGIN_SUFFIX, Path.of("modules", "user.jar").toString()), + loaded.packModules().stream().map(DiscoveredModule::origin).toList()); + var order = loaded.placement().stages().stream().map(stage -> stage.stage().id().toString()).toList(); + assertTrue(order.indexOf("bundled:core/mark") < order.indexOf("user:core/after"), order.toString()); + assertTrue(loaded.replacedJars().isEmpty()); + } + } + + @Test + void aPackJarNamedLikeABundledJarIsLeftOutAndNamed(@TempDir Path build) { + var bundledJar = module("host.bundled.BundledModule", "bundled:core", NO_MEMBERS, NO_STAGES).writeInto(build, "bundled.jar"); + module("pack.stale.StaleModule", "bundled:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "bundled.jar"); + + try (var loaded = LoadedPack.load(pack, HOST.bundlingJars(bundledJar))) { + assertEquals(List.of("host.bundled.BundledModule"), + loaded.packModules().stream().map(found -> found.module().getClass().getName()).toList()); + assertEquals(List.of(Path.of("modules", "bundled.jar").toString()), loaded.replacedJars()); + } + } + + @Test + void bundledJarsNeedDistinctJarFileNames() { + var host = ModuleHost.of(List.of()); + + assertThrows(IllegalArgumentException.class, () -> host.bundlingJars(Path.of("notes.txt"))); + assertThrows(IllegalArgumentException.class, () -> host.bundlingJars(Path.of("one", "same.jar"), Path.of("two", "same.jar"))); + assertThrows(IllegalArgumentException.class, () -> host.bundlingJars(Path.of("one", "same.jar")).bundlingJars(Path.of("two", "same.jar"))); + } + /** 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<>();