added library toc file

This commit is contained in:
2022-06-30 20:41:47 +08:00
parent 6f294d4dd7
commit 2b8e150949
34 changed files with 4249 additions and 59 deletions

View File

@@ -1,26 +1,36 @@
package net.woggioni.envelope;
import lombok.SneakyThrows;
import net.woggioni.xclassloader.jar.JarFile;
import net.woggioni.envelope.loader.JarFile;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.Manifest;
import static java.util.jar.JarFile.MANIFEST_NAME;
public class Launcher {
@SneakyThrows
static URL getURL(JarFile jarFile) {
return jarFile.getUrl();
}
@SneakyThrows
private static JarFile findCurrentJar() {
String launcherClassName = Launcher.class.getName();
@@ -86,7 +96,25 @@ public class Launcher {
String mainClassName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_CLASS);
String mainModuleName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_MODULE);
StringBuilder sb = new StringBuilder();
List<JarFile> classpath = new ArrayList<>();
URL libraryTocResource = Launcher.class.getClassLoader().getResource(Constants.LIBRARIES_TOC);
if(libraryTocResource == null) throw new RuntimeException(
Constants.LIBRARIES_TOC + " not found");
try(Reader reader = new InputStreamReader(libraryTocResource.openStream())) {
while(true) {
int c = reader.read();
boolean entryEnd = c == '/' || c < 0;
if(entryEnd) {
String entryName = Constants.LIBRARIES_FOLDER + '/' + sb;
JarEntry entry = currentJar.getJarEntry(entryName);
classpath.add(currentJar.getNestedJarFile(entry));
sb.setLength(0);
if(c < 0) break;
}
else sb.append((char) c);
}
}
Consumer<Class<?>> runner = new Consumer<Class<?>>() {
@Override
@SneakyThrows
@@ -108,7 +136,7 @@ public class Launcher {
currentJar,
mainModuleName,
mainClassName,
Constants.LIBRARIES_FOLDER,
classpath,
runner);
}

View File

@@ -1,33 +1,23 @@
package net.woggioni.envelope;
import lombok.SneakyThrows;
import net.woggioni.xclassloader.jar.JarFile;
import net.woggioni.envelope.loader.JarFile;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.function.Consumer;
import java.util.jar.JarEntry;
import java.util.stream.Collectors;
class MainRunner {
@SneakyThrows
static void run(JarFile currentJarFile,
String mainModuleName,
String mainClassName,
String librariesFolder,
Consumer<Class<?>> runner) {
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())) {
String mainModuleName,
String mainClassName,
List<JarFile> classpath,
Consumer<Class<?>> runner) {
URL[] urls = classpath.stream().map(Launcher::getURL).toArray(URL[]::new);
try (URLClassLoader cl = new URLClassLoader(urls, ClassLoader.getSystemClassLoader().getParent())) {
Thread.currentThread().setContextClassLoader(cl);
runner.accept(cl.loadClass(mainClassName));
}

View File

@@ -1,6 +1,6 @@
module net.woggioni.envelope {
requires java.logging;
requires static lombok;
requires net.woggioni.xclassloader;
requires net.woggioni.envelope.loader;
requires java.instrument;
}

View File

@@ -19,9 +19,9 @@ import java.net.URLClassLoader;
import lombok.SneakyThrows;
import net.woggioni.xclassloader.ModuleClassLoader;
import net.woggioni.xclassloader.JarFileModuleFinder;
import net.woggioni.xclassloader.jar.JarFile;
import net.woggioni.envelope.loader.ModuleClassLoader;
import net.woggioni.envelope.loader.JarFileModuleFinder;
import net.woggioni.envelope.loader.JarFile;
import java.util.jar.JarEntry;
class MainRunner {
@@ -34,35 +34,18 @@ class MainRunner {
static void run(JarFile currentJarFile,
String mainModuleName,
String mainClassName,
String librariesFolder,
List<JarFile> classpath,
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())) {
URL[] urls = classpath.stream().map(Launcher::getURL).toArray(URL[]::new);
try (URLClassLoader cl = new URLClassLoader(urls, 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();
JarFileModuleFinder jarFileModuleFinder = new JarFileModuleFinder(jarList);
JarFileModuleFinder jarFileModuleFinder = new JarFileModuleFinder(classpath);
Configuration cfg = bootConfiguration.resolve(jarFileModuleFinder, ModuleFinder.of(), Collections.singletonList(mainModuleName));
Map<String, ClassLoader> packageMap = new TreeMap<>();
ModuleLayer.Controller controller =