fixed bug with resource handling

This commit is contained in:
2022-06-27 17:21:23 +08:00
parent 01ff2bae71
commit 6f294d4dd7
9 changed files with 124 additions and 112 deletions

View File

@@ -105,11 +105,6 @@ publishing {
url = publishMavenRepositoryUrl
}
}
publications {
maven(MavenPublication) {
from(components["java"])
}
}
}
wrapper {

View File

@@ -1,86 +0,0 @@
package net.woggioni.envelope;
import java.util.Map;
import java.util.TreeMap;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.lang.module.ResolvedModule;
import java.lang.module.ModuleReference;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Enumeration;
import java.util.function.Consumer;
import java.net.URL;
import java.net.URLClassLoader;
import lombok.SneakyThrows;
import net.woggioni.xclassloader.ModuleClassLoader;
import net.woggioni.xclassloader.JarFileModuleFinder;
import net.woggioni.xclassloader.jar.JarFile;
import java.util.jar.JarEntry;
class MainRunner {
@SneakyThrows
static void run(JarFile currentJarFile,
String mainModuleName,
String mainClassName,
String librariesFolder,
Consumer<Class<?>> runner) {
if(mainModuleName == null) {
List<URL> jarList = new ArrayList<>();
Enumeration<JarEntry> entries = currentJarFile.entries();
while(entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if(!entry.isDirectory() && name.startsWith(librariesFolder) && name.endsWith(".jar")) {
jarList.add(currentJarFile.getNestedJarFile(entry).getUrl());
}
}
try (URLClassLoader cl = new URLClassLoader(jarList.toArray(new URL[0]), ClassLoader.getSystemClassLoader().getParent())) {
Thread.currentThread().setContextClassLoader(cl);
runner.accept(cl.loadClass(mainClassName));
}
} else {
List<JarFile> jarList = new ArrayList<>();
Enumeration<JarEntry> entries = currentJarFile.entries();
while(entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if(!entry.isDirectory() && name.startsWith("LIB-INF") && name.endsWith(".jar")) {
jarList.add(currentJarFile.getNestedJarFile(entry));
}
}
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration bootConfiguration = bootLayer.configuration();
Configuration cfg = bootConfiguration.resolve(new JarFileModuleFinder(jarList), 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 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()))));
}
}
}

View File

@@ -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()))));
}
}
}

View File

@@ -1,10 +1,18 @@
package envelope.test.jpms;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) throws MalformedURLException {
public static void main(String[] args) throws Exception {
new URL("https://www.google.com");
System.out.println("Hello World!!");
URL url = Main.class.getResource("/envelope/test/jpms/someResource.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
try(InputStream is = url.openStream()) {
builder.parse(is);
}
}
}

View File

@@ -1,3 +1,4 @@
module envelope.test.jpms {
requires java.xml;
exports envelope.test.jpms;
}

View File

@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
<Test TestId="0006" TestType="GUI">
<Name>Another Test</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>Test Input</Input>
<Output>10</Output>
</Test>
</Tests>

View File

@@ -1,10 +1,18 @@
package envelope.test.legacy;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) throws MalformedURLException {
public static void main(String[] args) throws Exception {
new URL("https://www.google.com");
System.out.println("Hello World!!");
URL url = Main.class.getResource("someResource.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
try(InputStream is = url.openStream()) {
builder.parse(is);
}
}
}

View File

@@ -0,0 +1,39 @@
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
<Test TestId="0006" TestType="GUI">
<Name>Another Test</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>Test Input</Input>
<Output>10</Output>
</Test>
</Tests>