mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Bundled jars: no special case for a pack's own copy
Nothing is released, so a pack holding an old copy of a bundled jar
gets no compatibility handling: it fails like any pack whose jars hold
the same class, naming both ("Class ... is in both module jars
iris-builtins.jar (bundled with the host) and
modules/iris-builtins.jar"). Removes LoadedPack.replacedJars() and the
bundled file name checks. Jar copies in a load's snapshot are named by
their place in the load, so same-named jars from the host and the pack
no longer collide before that check can name them.
This commit is contained in:
1 parent
98c090d5b2
commit
d3c0e4d6cc
6 files changed
+28
-79
No files matched your search
@@ -23,6 +23,7 @@ final class JarSnapshot {
|
||||
static final String DIRECTORY_PREFIX = "iris-pack-modules-";
|
||||
static final String OWNER_FILE = "owner.txt";
|
||||
static final String OWNER_HEADER = "Iris pack module jar copies";
|
||||
private static final String COPY_NAME_SEPARATOR = "-";
|
||||
|
||||
/** A module jar to copy: its origin, which every message names, and the file. */
|
||||
record SourceJar(String origin, Path file) {
|
||||
@@ -48,15 +49,15 @@ final class JarSnapshot {
|
||||
this.jars = List.copyOf(jars);
|
||||
}
|
||||
|
||||
/** Copies {@code jars}, whose file names differ, into a new directory below {@code temporaryRoot}. Leaves nothing
|
||||
* behind if a copy fails.
|
||||
/** Copies {@code jars} into a new directory below {@code temporaryRoot}, each copy named by its place in the list so
|
||||
* jars of the same file name from different places do not collide. Leaves nothing behind if a copy fails.
|
||||
*/
|
||||
static JarSnapshot take(List<SourceJar> jars, Path temporaryRoot) {
|
||||
var directory = createOwnedDirectory(temporaryRoot);
|
||||
var copies = new ArrayList<CopiedJar>(jars.size());
|
||||
try {
|
||||
for (var jar : jars) {
|
||||
copies.add(copy(jar, directory));
|
||||
for (int index = 0; index < jars.size(); index++) {
|
||||
copies.add(copy(jars.get(index), directory.resolve(index + COPY_NAME_SEPARATOR + jars.get(index).file().getFileName())));
|
||||
}
|
||||
return new JarSnapshot(directory, copies);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
@@ -117,8 +118,7 @@ final class JarSnapshot {
|
||||
return OWNER_HEADER + "\n" + pid + "\n";
|
||||
}
|
||||
|
||||
private static CopiedJar copy(SourceJar jar, Path directory) {
|
||||
var copy = directory.resolve(jar.file().getFileName().toString());
|
||||
private static CopiedJar copy(SourceJar jar, Path copy) {
|
||||
try {
|
||||
Files.copy(jar.file(), copy);
|
||||
return new CopiedJar(jar.origin(), copy);
|
||||
|
||||
@@ -53,11 +53,6 @@ public final class LoadedPack implements AutoCloseable {
|
||||
return packModules.modules();
|
||||
}
|
||||
|
||||
/** The pack's own jars that a jar the host bundles replaced; see {@link PackModules#replacedJars()}. */
|
||||
public List<String> replacedJars() {
|
||||
return packModules.replacedJars();
|
||||
}
|
||||
|
||||
/** Classes the pack's jars use that nobody has, per jar; see {@link PackModules#missingClasses()}. */
|
||||
public List<MissingClasses> missingClasses() {
|
||||
return packModules.missingClasses();
|
||||
|
||||
@@ -55,7 +55,6 @@ public record ModuleHost(
|
||||
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
|
||||
@@ -102,10 +101,8 @@ public record ModuleHost(
|
||||
}
|
||||
|
||||
/** 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.
|
||||
* {@code modules} folder: before the pack's own jars, in the given order, in the same class loader. Whether they exist
|
||||
* is checked when a pack loads.
|
||||
*/
|
||||
public ModuleHost bundlingJars(Path... jars) {
|
||||
var bundled = new ArrayList<>(bundledJars);
|
||||
@@ -164,19 +161,6 @@ public record ModuleHost(
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireJarFileNames(List<Path> jars) {
|
||||
var names = new TreeSet<String>();
|
||||
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<FeatureAttachment.Target, Set<String>> merge(
|
||||
Map<FeatureAttachment.Target, Set<String>> existing,
|
||||
Map<FeatureAttachment.Target, Set<String>> added) {
|
||||
|
||||
@@ -53,15 +53,13 @@ public final class PackModules implements AutoCloseable {
|
||||
|
||||
private final List<DiscoveredModule> modules;
|
||||
private final List<MissingClasses> missingClasses;
|
||||
private final List<String> replacedJars;
|
||||
private final URLClassLoader loader;
|
||||
private final JarSnapshot snapshot;
|
||||
|
||||
private PackModules(List<DiscoveredModule> modules, List<MissingClasses> missingClasses, List<String> replacedJars,
|
||||
URLClassLoader loader, JarSnapshot snapshot) {
|
||||
private PackModules(
|
||||
List<DiscoveredModule> modules, List<MissingClasses> missingClasses, 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;
|
||||
}
|
||||
@@ -94,8 +92,7 @@ public final class PackModules implements AutoCloseable {
|
||||
}
|
||||
|
||||
/** 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.
|
||||
* in the pack, before its own jars and in the given order.
|
||||
*/
|
||||
static PackModules load(Path packRoot, Collection<String> exposedHostPackages, List<Path> bundledJars, Path temporaryRoot,
|
||||
System.Logger logger) {
|
||||
@@ -103,12 +100,9 @@ public final class PackModules implements AutoCloseable {
|
||||
Objects.requireNonNull(logger, "logger");
|
||||
var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages));
|
||||
StaleJarSnapshots.remove(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();
|
||||
var snapshot = JarSnapshot.take(sourceJars(packRoot, bundledJars, findModuleJars(packRoot)), temporaryRoot);
|
||||
try {
|
||||
var packModules = loadSnapshot(packRoot, snapshot, replacedJars, host, logger);
|
||||
var packModules = loadSnapshot(packRoot, snapshot, host, logger);
|
||||
logLoad(logger, packName(packRoot), packModules);
|
||||
return packModules;
|
||||
} catch (RuntimeException | Error failure) {
|
||||
@@ -130,11 +124,6 @@ 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<String> 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() {
|
||||
@@ -152,11 +141,10 @@ public final class PackModules implements AutoCloseable {
|
||||
return Path.of(System.getProperty(TEMPORARY_DIRECTORY_PROPERTY));
|
||||
}
|
||||
|
||||
private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, List<String> replacedJars,
|
||||
ExposedHostClassLoader host, System.Logger logger) {
|
||||
private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, 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, replacedJars, host, snapshot, logger);
|
||||
return defineModules(packRoot, jars, missingClasses, host, snapshot, logger);
|
||||
}
|
||||
|
||||
private static List<Path> findModuleJars(Path packRoot) {
|
||||
@@ -172,17 +160,9 @@ public final class PackModules implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Path> packJarsNamedLike(List<Path> packJars, List<Path> bundledJars) {
|
||||
var bundledNames = bundledJars.stream().map(PackModules::fileName).collect(Collectors.toSet());
|
||||
return packJars.stream().filter(jar -> bundledNames.contains(fileName(jar))).toList();
|
||||
}
|
||||
|
||||
private static List<JarSnapshot.SourceJar> sourceJars(Path packRoot, List<Path> bundledJars, List<Path> packJars,
|
||||
List<Path> replacedPackJars) {
|
||||
private static List<JarSnapshot.SourceJar> sourceJars(Path packRoot, List<Path> bundledJars, List<Path> packJars) {
|
||||
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));
|
||||
var own = packJars.stream().map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar));
|
||||
return Stream.concat(bundled, own).toList();
|
||||
}
|
||||
|
||||
@@ -200,12 +180,12 @@ public final class PackModules implements AutoCloseable {
|
||||
}
|
||||
|
||||
private static PackModules defineModules(
|
||||
Path packRoot, List<ModuleJar> jars, List<MissingClasses> missingClasses, List<String> replacedJars, ClassLoader host,
|
||||
JarSnapshot snapshot, System.Logger logger) {
|
||||
Path packRoot, List<ModuleJar> jars, List<MissingClasses> missingClasses, 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), replacedJars, loader, snapshot);
|
||||
return new PackModules(modules, withoutDeclaredOptional(missingClasses, modules), loader, snapshot);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
closeAfterFailure(loader, failure);
|
||||
throw failure;
|
||||
|
||||
@@ -137,29 +137,19 @@ class LoadedPackTest {
|
||||
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");
|
||||
void aPackCopyOfABundledJarFailsNamingBothJars(@TempDir Path build) {
|
||||
var bundled = module("host.bundled.BundledModule", "bundled:core", NO_MEMBERS, NO_STAGES);
|
||||
var bundledJar = bundled.writeInto(build, "bundled.jar");
|
||||
bundled.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());
|
||||
}
|
||||
}
|
||||
var error = assertThrows(IllegalArgumentException.class, () -> LoadedPack.load(pack, HOST.bundlingJars(bundledJar)));
|
||||
|
||||
@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")));
|
||||
assertTrue(error.getMessage().contains("Class host.bundled.BundledModule is in both module jars bundled.jar"
|
||||
+ PackModules.BUNDLED_ORIGIN_SUFFIX + " and " + Path.of("modules", "bundled.jar")), error.getMessage());
|
||||
}
|
||||
|
||||
/** Keeps the level and first line of every message, which is enough to see where the summary went. */
|
||||
|
||||
@@ -57,7 +57,7 @@ class PackJarSnapshotTest {
|
||||
var module = packModules.modules().getFirst();
|
||||
var location = Path.of(module.module().getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
|
||||
assertEquals(1, snapshotDirectories().size());
|
||||
assertEquals(snapshotDirectories().getFirst().resolve("gamma.jar"), location);
|
||||
assertEquals(snapshotDirectories().getFirst().resolve("0-gamma.jar"), location);
|
||||
assertEquals(origin("gamma.jar"), module.origin());
|
||||
|
||||
Files.delete(original);
|
||||
|
||||
Reference in new issue
Block a user