mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 03:31:32 +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 DIRECTORY_PREFIX = "iris-pack-modules-";
|
||||||
static final String OWNER_FILE = "owner.txt";
|
static final String OWNER_FILE = "owner.txt";
|
||||||
static final String OWNER_HEADER = "Iris pack module jar copies";
|
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. */
|
/** A module jar to copy: its origin, which every message names, and the file. */
|
||||||
record SourceJar(String origin, Path file) {
|
record SourceJar(String origin, Path file) {
|
||||||
@@ -48,15 +49,15 @@ final class JarSnapshot {
|
|||||||
this.jars = List.copyOf(jars);
|
this.jars = List.copyOf(jars);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copies {@code jars}, whose file names differ, into a new directory below {@code temporaryRoot}. Leaves nothing
|
/** Copies {@code jars} into a new directory below {@code temporaryRoot}, each copy named by its place in the list so
|
||||||
* behind if a copy fails.
|
* 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) {
|
static JarSnapshot take(List<SourceJar> jars, Path temporaryRoot) {
|
||||||
var directory = createOwnedDirectory(temporaryRoot);
|
var directory = createOwnedDirectory(temporaryRoot);
|
||||||
var copies = new ArrayList<CopiedJar>(jars.size());
|
var copies = new ArrayList<CopiedJar>(jars.size());
|
||||||
try {
|
try {
|
||||||
for (var jar : jars) {
|
for (int index = 0; index < jars.size(); index++) {
|
||||||
copies.add(copy(jar, directory));
|
copies.add(copy(jars.get(index), directory.resolve(index + COPY_NAME_SEPARATOR + jars.get(index).file().getFileName())));
|
||||||
}
|
}
|
||||||
return new JarSnapshot(directory, copies);
|
return new JarSnapshot(directory, copies);
|
||||||
} catch (RuntimeException | Error failure) {
|
} catch (RuntimeException | Error failure) {
|
||||||
@@ -117,8 +118,7 @@ final class JarSnapshot {
|
|||||||
return OWNER_HEADER + "\n" + pid + "\n";
|
return OWNER_HEADER + "\n" + pid + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CopiedJar copy(SourceJar jar, Path directory) {
|
private static CopiedJar copy(SourceJar jar, Path copy) {
|
||||||
var copy = directory.resolve(jar.file().getFileName().toString());
|
|
||||||
try {
|
try {
|
||||||
Files.copy(jar.file(), copy);
|
Files.copy(jar.file(), copy);
|
||||||
return new CopiedJar(jar.origin(), copy);
|
return new CopiedJar(jar.origin(), copy);
|
||||||
|
|||||||
@@ -53,11 +53,6 @@ public final class LoadedPack implements AutoCloseable {
|
|||||||
return packModules.modules();
|
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()}. */
|
/** Classes the pack's jars use that nobody has, per jar; see {@link PackModules#missingClasses()}. */
|
||||||
public List<MissingClasses> missingClasses() {
|
public List<MissingClasses> missingClasses() {
|
||||||
return packModules.missingClasses();
|
return packModules.missingClasses();
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ public record ModuleHost(
|
|||||||
HostTypes.of(hostTypePackages);
|
HostTypes.of(hostTypePackages);
|
||||||
requireFieldNames(reservedHostFields);
|
requireFieldNames(reservedHostFields);
|
||||||
requireFolderNames(reservedFolders);
|
requireFolderNames(reservedFolders);
|
||||||
requireJarFileNames(bundledJars);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The given built-in modules, exposing only the Iris API and reserving nothing. Jar copies go to
|
/** 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
|
/** 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
|
* {@code modules} folder: before the pack's own jars, in the given order, in the same class loader. Whether they exist
|
||||||
* same file name, such as an old copy, is left out, so a pack can never load a stale copy; {@link LoadedPack#replacedJars()}
|
* is checked when a pack loads.
|
||||||
* 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) {
|
public ModuleHost bundlingJars(Path... jars) {
|
||||||
var bundled = new ArrayList<>(bundledJars);
|
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(
|
private static Map<FeatureAttachment.Target, Set<String>> merge(
|
||||||
Map<FeatureAttachment.Target, Set<String>> existing,
|
Map<FeatureAttachment.Target, Set<String>> existing,
|
||||||
Map<FeatureAttachment.Target, Set<String>> added) {
|
Map<FeatureAttachment.Target, Set<String>> added) {
|
||||||
|
|||||||
@@ -53,15 +53,13 @@ public final class PackModules implements AutoCloseable {
|
|||||||
|
|
||||||
private final List<DiscoveredModule> modules;
|
private final List<DiscoveredModule> modules;
|
||||||
private final List<MissingClasses> missingClasses;
|
private final List<MissingClasses> missingClasses;
|
||||||
private final List<String> replacedJars;
|
|
||||||
private final URLClassLoader loader;
|
private final URLClassLoader loader;
|
||||||
private final JarSnapshot snapshot;
|
private final JarSnapshot snapshot;
|
||||||
|
|
||||||
private PackModules(List<DiscoveredModule> modules, List<MissingClasses> missingClasses, List<String> replacedJars,
|
private PackModules(
|
||||||
URLClassLoader loader, JarSnapshot snapshot) {
|
List<DiscoveredModule> modules, List<MissingClasses> missingClasses, URLClassLoader loader, JarSnapshot snapshot) {
|
||||||
this.modules = List.copyOf(modules);
|
this.modules = List.copyOf(modules);
|
||||||
this.missingClasses = List.copyOf(missingClasses);
|
this.missingClasses = List.copyOf(missingClasses);
|
||||||
this.replacedJars = List.copyOf(replacedJars);
|
|
||||||
this.loader = loader;
|
this.loader = loader;
|
||||||
this.snapshot = snapshot;
|
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
|
/** 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
|
* in the pack, before its own jars and in the given order.
|
||||||
* 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<String> exposedHostPackages, List<Path> bundledJars, Path temporaryRoot,
|
static PackModules load(Path packRoot, Collection<String> exposedHostPackages, List<Path> bundledJars, Path temporaryRoot,
|
||||||
System.Logger logger) {
|
System.Logger logger) {
|
||||||
@@ -103,12 +100,9 @@ public final class PackModules implements AutoCloseable {
|
|||||||
Objects.requireNonNull(logger, "logger");
|
Objects.requireNonNull(logger, "logger");
|
||||||
var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages));
|
var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages));
|
||||||
StaleJarSnapshots.remove(temporaryRoot);
|
StaleJarSnapshots.remove(temporaryRoot);
|
||||||
var packJars = findModuleJars(packRoot);
|
var snapshot = JarSnapshot.take(sourceJars(packRoot, bundledJars, findModuleJars(packRoot)), temporaryRoot);
|
||||||
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 {
|
try {
|
||||||
var packModules = loadSnapshot(packRoot, snapshot, replacedJars, host, logger);
|
var packModules = loadSnapshot(packRoot, snapshot, host, logger);
|
||||||
logLoad(logger, packName(packRoot), packModules);
|
logLoad(logger, packName(packRoot), packModules);
|
||||||
return packModules;
|
return packModules;
|
||||||
} catch (RuntimeException | Error failure) {
|
} catch (RuntimeException | Error failure) {
|
||||||
@@ -130,11 +124,6 @@ public final class PackModules implements AutoCloseable {
|
|||||||
return missingClasses;
|
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. */
|
/** Closes the class loader, then deletes the jar copies, which an open loader would keep locked on Windows. */
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
@@ -152,11 +141,10 @@ public final class PackModules implements AutoCloseable {
|
|||||||
return Path.of(System.getProperty(TEMPORARY_DIRECTORY_PROPERTY));
|
return Path.of(System.getProperty(TEMPORARY_DIRECTORY_PROPERTY));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, List<String> replacedJars,
|
private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, ExposedHostClassLoader host, System.Logger logger) {
|
||||||
ExposedHostClassLoader host, System.Logger logger) {
|
|
||||||
var jars = snapshot.jars().stream().map(ModuleJar::read).toList();
|
var jars = snapshot.jars().stream().map(ModuleJar::read).toList();
|
||||||
var missingClasses = PackClassCheck.requireLinkable(jars, host);
|
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) {
|
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) {
|
private static List<JarSnapshot.SourceJar> sourceJars(Path packRoot, List<Path> bundledJars, List<Path> packJars) {
|
||||||
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) {
|
|
||||||
var bundled = bundledJars.stream().map(jar -> new JarSnapshot.SourceJar(bundledOrigin(jar), jar));
|
var bundled = bundledJars.stream().map(jar -> new JarSnapshot.SourceJar(bundledOrigin(jar), jar));
|
||||||
var own = packJars.stream()
|
var own = packJars.stream().map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar));
|
||||||
.filter(Predicate.not(replacedPackJars::contains))
|
|
||||||
.map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar));
|
|
||||||
return Stream.concat(bundled, own).toList();
|
return Stream.concat(bundled, own).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,12 +180,12 @@ public final class PackModules implements AutoCloseable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static PackModules defineModules(
|
private static PackModules defineModules(
|
||||||
Path packRoot, List<ModuleJar> jars, List<MissingClasses> missingClasses, List<String> replacedJars, ClassLoader host,
|
Path packRoot, List<ModuleJar> jars, List<MissingClasses> missingClasses, ClassLoader host, JarSnapshot snapshot,
|
||||||
JarSnapshot snapshot, System.Logger logger) {
|
System.Logger logger) {
|
||||||
var loader = newPackClassLoader(packRoot, jars, host);
|
var loader = newPackClassLoader(packRoot, jars, host);
|
||||||
try {
|
try {
|
||||||
var modules = createModules(jars, loader, snapshot, logger);
|
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) {
|
} catch (RuntimeException | Error failure) {
|
||||||
closeAfterFailure(loader, failure);
|
closeAfterFailure(loader, failure);
|
||||||
throw failure;
|
throw failure;
|
||||||
|
|||||||
@@ -137,29 +137,19 @@ class LoadedPackTest {
|
|||||||
loaded.packModules().stream().map(DiscoveredModule::origin).toList());
|
loaded.packModules().stream().map(DiscoveredModule::origin).toList());
|
||||||
var order = loaded.placement().stages().stream().map(stage -> stage.stage().id().toString()).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(order.indexOf("bundled:core/mark") < order.indexOf("user:core/after"), order.toString());
|
||||||
assertTrue(loaded.replacedJars().isEmpty());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void aPackJarNamedLikeABundledJarIsLeftOutAndNamed(@TempDir Path build) {
|
void aPackCopyOfABundledJarFailsNamingBothJars(@TempDir Path build) {
|
||||||
var bundledJar = module("host.bundled.BundledModule", "bundled:core", NO_MEMBERS, NO_STAGES).writeInto(build, "bundled.jar");
|
var bundled = module("host.bundled.BundledModule", "bundled:core", NO_MEMBERS, NO_STAGES);
|
||||||
module("pack.stale.StaleModule", "bundled:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "bundled.jar");
|
var bundledJar = bundled.writeInto(build, "bundled.jar");
|
||||||
|
bundled.writeInto(pack, "bundled.jar");
|
||||||
|
|
||||||
try (var loaded = LoadedPack.load(pack, HOST.bundlingJars(bundledJar))) {
|
var error = assertThrows(IllegalArgumentException.class, () -> 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
|
assertTrue(error.getMessage().contains("Class host.bundled.BundledModule is in both module jars bundled.jar"
|
||||||
void bundledJarsNeedDistinctJarFileNames() {
|
+ PackModules.BUNDLED_ORIGIN_SUFFIX + " and " + Path.of("modules", "bundled.jar")), error.getMessage());
|
||||||
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. */
|
/** 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 module = packModules.modules().getFirst();
|
||||||
var location = Path.of(module.module().getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
|
var location = Path.of(module.module().getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
|
||||||
assertEquals(1, snapshotDirectories().size());
|
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());
|
assertEquals(origin("gamma.jar"), module.origin());
|
||||||
|
|
||||||
Files.delete(original);
|
Files.delete(original);
|
||||||
|
|||||||
Reference in new issue
Block a user