mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Add module display names, reject duplicate pack classes, log pack modules
ModuleInfo gains a required, non-blank display name next to id and version: ModuleInfo.of(id, name, version). PackModules now fails when a class is in two jars of one pack, naming the class and both jars. ModuleJar indexes class names while it reads the entry point, so the check runs before the pack class loader exists and nothing is half-loaded. Jars are read with the runtime's multi-release version, like the loader reads them: a versioned class counts once under its base name; module-info.class and anything under META-INF/ are ignored. A successful load logs one INFO summary through System.Logger with the display name, id, version and jar of each module, or a line saying the pack has no module jars. PackModulesSummary builds the text and is tested without a logging backend.
This commit is contained in:
1 parent
4fa6ddcd7a
commit
b137bc6d92
19 files changed
+328
-53
No files matched your search
@@ -13,38 +13,42 @@ import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/** One jar in a pack's modules directory and the entry class it names.
|
||||
/** One jar in a pack's modules directory, the entry class it names and the classes it holds.
|
||||
*
|
||||
* <p>The entry point is the jar's own {@code META-INF/services/com.volmit.IrisModule} file, in
|
||||
* the format {@link java.util.ServiceLoader} reads. The host reads that file from this jar only:
|
||||
* {@code ServiceLoader} would also return providers from parent class loaders and could not tell
|
||||
* which jar a broken entry came from.
|
||||
*/
|
||||
record ModuleJar(Path path, String origin, String entryClass) {
|
||||
record ModuleJar(Path path, String origin, String entryClass, List<String> classNames) {
|
||||
static final String ENTRY_POINT_FILE = "META-INF/services/" + IrisModule.class.getName();
|
||||
private static final String COMMENT_START = "#";
|
||||
private static final String CLASS_FILE_SUFFIX = ".class";
|
||||
private static final String MODULE_DESCRIPTOR = "module-info" + CLASS_FILE_SUFFIX;
|
||||
private static final String META_INF = "META-INF/";
|
||||
|
||||
ModuleJar {
|
||||
Objects.requireNonNull(path, "path");
|
||||
Objects.requireNonNull(origin, "origin");
|
||||
Objects.requireNonNull(entryClass, "entryClass");
|
||||
classNames = List.copyOf(classNames);
|
||||
}
|
||||
|
||||
/** Reads the single entry class a jar names. The origin is the jar path relative to its pack. */
|
||||
/** Reads the single entry class a jar names and the classes it holds. The origin is the jar
|
||||
* path relative to its pack.
|
||||
*/
|
||||
static ModuleJar read(Path packRoot, Path jar) {
|
||||
var origin = packRoot.relativize(jar).toString();
|
||||
var entryClasses = readEntryClasses(jar, origin);
|
||||
if (entryClasses.isEmpty()) {
|
||||
throw new IllegalArgumentException("Module jar " + origin + " has no entry point; name its "
|
||||
+ IrisModule.class.getSimpleName() + " class in " + ENTRY_POINT_FILE);
|
||||
try (var file = openAsClassLoaderSeesIt(jar)) {
|
||||
var entryClass = requireSingleEntryClass(origin, readEntryClasses(file));
|
||||
return new ModuleJar(jar, origin, entryClass, readClassNames(file));
|
||||
} catch (IOException exception) {
|
||||
throw new UncheckedIOException("Module jar " + origin + " is not a readable jar", exception);
|
||||
}
|
||||
if (entryClasses.size() > 1) {
|
||||
throw new IllegalArgumentException("Module jar " + origin + " names " + entryClasses.size()
|
||||
+ " entry points " + entryClasses + "; one jar holds exactly one module");
|
||||
}
|
||||
return new ModuleJar(jar, origin, entryClasses.getFirst());
|
||||
}
|
||||
|
||||
URL url() {
|
||||
@@ -66,21 +70,61 @@ record ModuleJar(Path path, String origin, String entryClass) {
|
||||
return new DiscoveredModule(module, origin);
|
||||
}
|
||||
|
||||
private static List<String> readEntryClasses(Path jar, String origin) {
|
||||
try (var file = new JarFile(jar.toFile())) {
|
||||
var entry = file.getJarEntry(ENTRY_POINT_FILE);
|
||||
if (entry == null) return List.of();
|
||||
try (var reader = new BufferedReader(new InputStreamReader(file.getInputStream(entry), StandardCharsets.UTF_8))) {
|
||||
return reader.lines()
|
||||
.map(ModuleJar::withoutComment)
|
||||
.map(String::strip)
|
||||
.filter(Predicate.not(String::isEmpty))
|
||||
.distinct()
|
||||
.toList();
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
throw new UncheckedIOException("Module jar " + origin + " is not a readable jar", exception);
|
||||
/** Opens the jar with the runtime's multi-release version, like the pack class loader does, so
|
||||
* class names below match the classes that loader can define.
|
||||
*/
|
||||
private static JarFile openAsClassLoaderSeesIt(Path jar) throws IOException {
|
||||
return new JarFile(jar.toFile(), true, ZipFile.OPEN_READ, JarFile.runtimeVersion());
|
||||
}
|
||||
|
||||
private static String requireSingleEntryClass(String origin, List<String> entryClasses) {
|
||||
if (entryClasses.isEmpty()) {
|
||||
throw new IllegalArgumentException("Module jar " + origin + " has no entry point; name its "
|
||||
+ IrisModule.class.getSimpleName() + " class in " + ENTRY_POINT_FILE);
|
||||
}
|
||||
if (entryClasses.size() > 1) {
|
||||
throw new IllegalArgumentException("Module jar " + origin + " names " + entryClasses.size()
|
||||
+ " entry points " + entryClasses + "; one jar holds exactly one module");
|
||||
}
|
||||
return entryClasses.getFirst();
|
||||
}
|
||||
|
||||
private static List<String> readEntryClasses(JarFile file) throws IOException {
|
||||
var entry = file.getJarEntry(ENTRY_POINT_FILE);
|
||||
if (entry == null) return List.of();
|
||||
try (var reader = new BufferedReader(new InputStreamReader(file.getInputStream(entry), StandardCharsets.UTF_8))) {
|
||||
return reader.lines()
|
||||
.map(ModuleJar::withoutComment)
|
||||
.map(String::strip)
|
||||
.filter(Predicate.not(String::isEmpty))
|
||||
.distinct()
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
/** Binary names of the classes in the jar, sorted. A multi-release jar contributes each class
|
||||
* once, under its base name, in the version this runtime would load. Anything left below
|
||||
* {@code META-INF/}, such as versioned entries of a jar that is not multi-release, cannot be
|
||||
* loaded as a class: {@code META-INF} is not a valid package name.
|
||||
*/
|
||||
private static List<String> readClassNames(JarFile file) {
|
||||
return file.versionedStream()
|
||||
.filter(Predicate.not(JarEntry::isDirectory))
|
||||
.map(JarEntry::getName)
|
||||
.filter(ModuleJar::isClassFile)
|
||||
.map(ModuleJar::toBinaryName)
|
||||
.sorted()
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static boolean isClassFile(String entryName) {
|
||||
return entryName.endsWith(CLASS_FILE_SUFFIX)
|
||||
&& !entryName.equals(MODULE_DESCRIPTOR)
|
||||
&& !entryName.startsWith(META_INF);
|
||||
}
|
||||
|
||||
private static String toBinaryName(String entryName) {
|
||||
return entryName.substring(0, entryName.length() - CLASS_FILE_SUFFIX.length()).replace('/', '.');
|
||||
}
|
||||
|
||||
private static String withoutComment(String line) {
|
||||
|
||||
@@ -3,11 +3,13 @@ package com.volmit.host;
|
||||
import com.volmit.IrisModule;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.System.Logger.Level;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -16,7 +18,11 @@ import java.util.Objects;
|
||||
*
|
||||
* <p>All jars of a pack share one class loader whose parent is the loader of the API. A pack jar
|
||||
* can therefore name another pack jar's stage constants in {@code requires(...)}, as well as
|
||||
* those of built-in modules visible to the API. Packs do not see each other's classes.
|
||||
* those of built-in modules visible to the API. Packs do not see each other's classes, and no class
|
||||
* may appear in two jars of one pack.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
@@ -24,6 +30,7 @@ import java.util.Objects;
|
||||
public final class PackModules implements AutoCloseable {
|
||||
public static final String MODULES_DIRECTORY = "modules";
|
||||
static final String JAR_EXTENSION = ".jar";
|
||||
private static final System.Logger LOGGER = System.getLogger(PackModules.class.getName());
|
||||
|
||||
private final List<DiscoveredModule> modules;
|
||||
private final URLClassLoader loader;
|
||||
@@ -41,13 +48,10 @@ public final class PackModules implements AutoCloseable {
|
||||
var jars = findModuleJars(packRoot).stream()
|
||||
.map(jar -> ModuleJar.read(packRoot, jar))
|
||||
.toList();
|
||||
var loader = newPackClassLoader(packRoot, jars);
|
||||
try {
|
||||
return new PackModules(createModules(jars, loader), loader);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
closeAfterFailure(loader, failure);
|
||||
throw failure;
|
||||
}
|
||||
requireDistinctClasses(jars);
|
||||
var packModules = defineModules(packRoot, jars);
|
||||
LOGGER.log(Level.INFO, () -> PackModulesSummary.describe(packName(packRoot), packModules.modules()));
|
||||
return packModules;
|
||||
}
|
||||
|
||||
/** One module per jar, in jar file name order. */
|
||||
@@ -81,9 +85,43 @@ public final class PackModules implements AutoCloseable {
|
||||
return Files.isRegularFile(path) && path.getFileName().toString().endsWith(JAR_EXTENSION);
|
||||
}
|
||||
|
||||
/** Fails before any class is defined when two jars hold the same class. The shared loader would
|
||||
* otherwise silently use whichever copy comes first in file name order.
|
||||
*/
|
||||
private static void requireDistinctClasses(List<ModuleJar> jars) {
|
||||
var owners = new HashMap<String, ModuleJar>();
|
||||
for (var jar : jars) {
|
||||
for (var className : jar.classNames()) {
|
||||
var owner = owners.putIfAbsent(className, jar);
|
||||
if (owner != null) throw duplicateClass(className, owner, jar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IllegalArgumentException duplicateClass(String className, ModuleJar first, ModuleJar second) {
|
||||
return new IllegalArgumentException("Class " + className + " is in both module jars " + first.origin()
|
||||
+ " and " + second.origin() + "; every class of a pack must come from one jar, so relocate"
|
||||
+ " shaded libraries or keep shared classes in one module");
|
||||
}
|
||||
|
||||
private static PackModules defineModules(Path packRoot, List<ModuleJar> jars) {
|
||||
var loader = newPackClassLoader(packRoot, jars);
|
||||
try {
|
||||
return new PackModules(createModules(jars, loader), loader);
|
||||
} catch (RuntimeException | Error failure) {
|
||||
closeAfterFailure(loader, failure);
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private static URLClassLoader newPackClassLoader(Path packRoot, List<ModuleJar> jars) {
|
||||
var urls = jars.stream().map(ModuleJar::url).toArray(URL[]::new);
|
||||
return new URLClassLoader("pack:" + packRoot.getFileName(), urls, IrisModule.class.getClassLoader());
|
||||
return new URLClassLoader("pack:" + packName(packRoot), urls, IrisModule.class.getClassLoader());
|
||||
}
|
||||
|
||||
private static String packName(Path packRoot) {
|
||||
var name = packRoot.getFileName();
|
||||
return name == null ? packRoot.toString() : name.toString();
|
||||
}
|
||||
|
||||
private static List<DiscoveredModule> createModules(List<ModuleJar> jars, ClassLoader loader) {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** The one log message a pack load writes: which modules the pack uses and which jar each comes
|
||||
* from.
|
||||
*/
|
||||
final class PackModulesSummary {
|
||||
private static final String INDENT = " ";
|
||||
|
||||
private PackModulesSummary() {}
|
||||
|
||||
static String describe(String packName, List<DiscoveredModule> modules) {
|
||||
if (modules.isEmpty()) {
|
||||
return "Pack " + packName + " uses no modules: no jars in its " + PackModules.MODULES_DIRECTORY + " directory";
|
||||
}
|
||||
var text = new StringBuilder("Pack ").append(packName).append(" uses ").append(modules.size())
|
||||
.append(modules.size() == 1 ? " module:" : " modules:");
|
||||
for (var module : modules) {
|
||||
text.append('\n').append(INDENT).append(describe(module));
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
private static String describe(DiscoveredModule discovered) {
|
||||
var info = discovered.module().info();
|
||||
return info.name() + " (" + info.id() + " " + info.version() + ") from " + discovered.origin();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user