mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
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.
This commit is contained in:
1 parent
a325640d5d
commit
c5a54558fa
8 files changed
+97
-22
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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<DiscoveredModule> builtIns,
|
||||
Set<String> exposedHostPackages,
|
||||
Map<FeatureAttachment.Target, Set<String>> reservedHostFields,
|
||||
Set<String> reservedNamespaces) {
|
||||
Set<String> 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<? extends IrisModule> 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<FeatureAttachment.Target, Set<String>> 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<FeatureAttachment.Target, Set<String>> copyOf(Map<FeatureAttachment.Target, Set<String>> fields) {
|
||||
var copy = new EnumMap<FeatureAttachment.Target, Set<String>>(FeatureAttachment.Target.class);
|
||||
fields.forEach((target, names) -> copy.put(target, Set.copyOf(names)));
|
||||
|
||||
@@ -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}.
|
||||
*
|
||||
* <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, and one WARNING listing the missing classes, if any.
|
||||
* <p>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.
|
||||
*
|
||||
* <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.
|
||||
@@ -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<DiscoveredModule> modules;
|
||||
private final List<MissingClasses> 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<String> 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<String> 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) {
|
||||
|
||||
Reference in new issue
Block a user