fixed bug with resource handling
This commit is contained in:
6
launcher/src/main/java11/module-info.java
Normal file
6
launcher/src/main/java11/module-info.java
Normal file
@@ -0,0 +1,6 @@
|
||||
module net.woggioni.envelope {
|
||||
requires java.logging;
|
||||
requires static lombok;
|
||||
requires net.woggioni.xclassloader;
|
||||
requires java.instrument;
|
||||
}
|
@@ -12,10 +12,11 @@ import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import java.util.Enumeration;
|
||||
import java.util.function.Consumer;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import net.woggioni.xclassloader.ModuleClassLoader;
|
||||
@@ -24,6 +25,10 @@ import net.woggioni.xclassloader.jar.JarFile;
|
||||
import java.util.jar.JarEntry;
|
||||
|
||||
class MainRunner {
|
||||
@SneakyThrows
|
||||
private static final URL uri2url(URI uri, URLStreamHandler streamHandler) {
|
||||
return new URL(null, uri.toString(), streamHandler);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
static void run(JarFile currentJarFile,
|
||||
@@ -57,30 +62,33 @@ class MainRunner {
|
||||
}
|
||||
ModuleLayer bootLayer = ModuleLayer.boot();
|
||||
Configuration bootConfiguration = bootLayer.configuration();
|
||||
Configuration cfg = bootConfiguration.resolve(new JarFileModuleFinder(jarList), ModuleFinder.of(), Collections.singletonList(mainModuleName));
|
||||
JarFileModuleFinder jarFileModuleFinder = new JarFileModuleFinder(jarList);
|
||||
Configuration cfg = bootConfiguration.resolve(jarFileModuleFinder, ModuleFinder.of(), Collections.singletonList(mainModuleName));
|
||||
Map<String, ClassLoader> packageMap = new TreeMap<>();
|
||||
ModuleLayer.Controller controller =
|
||||
ModuleLayer.defineModules(cfg, Collections.singletonList(ModuleLayer.boot()), moduleName -> {
|
||||
ModuleReference modRef = cfg.findModule(moduleName)
|
||||
.map(ResolvedModule::reference)
|
||||
.orElseThrow();
|
||||
ClassLoader cl = new ModuleClassLoader(
|
||||
Collections.unmodifiableMap(packageMap),
|
||||
modRef
|
||||
);
|
||||
for(String packageName : modRef.descriptor().packages()) {
|
||||
packageMap.put(packageName, cl);
|
||||
}
|
||||
return cl;
|
||||
});
|
||||
ModuleLayer.defineModules(cfg, Collections.singletonList(ModuleLayer.boot()), moduleName -> {
|
||||
ModuleReference modRef = cfg.findModule(moduleName)
|
||||
.map(ResolvedModule::reference)
|
||||
.orElseThrow();
|
||||
URLStreamHandler streamHandler = jarFileModuleFinder.getStreamHandlerForModule(moduleName);
|
||||
ClassLoader cl = new ModuleClassLoader(
|
||||
Collections.unmodifiableMap(packageMap),
|
||||
modRef,
|
||||
(URI uri) -> uri2url(uri, streamHandler)
|
||||
);
|
||||
for(String packageName : modRef.descriptor().packages()) {
|
||||
packageMap.put(packageName, cl);
|
||||
}
|
||||
return cl;
|
||||
});
|
||||
ModuleLayer layer = controller.layer();
|
||||
Module mainModule = layer.findModule(mainModuleName).orElseThrow(
|
||||
() -> new IllegalStateException(String.format("Main module '%s' not found", mainModuleName)));
|
||||
runner.accept(Optional.ofNullable(mainClassName)
|
||||
.or(() -> mainModule.getDescriptor().mainClass())
|
||||
.map(className -> Class.forName(mainModule, className))
|
||||
.orElseThrow(() -> new IllegalStateException(
|
||||
String.format("Unable to determine main class name for module '%s'", mainModule.getName()))));
|
||||
.or(() -> mainModule.getDescriptor().mainClass())
|
||||
.map(className -> Class.forName(mainModule, className))
|
||||
.orElseThrow(() -> new IllegalStateException(
|
||||
String.format("Unable to determine main class name for module '%s'", mainModule.getName()))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user