added old executable-jar plugin prototype (excluded from build)

This commit is contained in:
2021-09-25 21:33:03 +02:00
parent 87d040d125
commit f45e25bcd3
12 changed files with 225 additions and 53 deletions

View File

@@ -0,0 +1,37 @@
package net.woggioni.executable.jar;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import lombok.SneakyThrows;
class JavaAgentLauncher {
@SneakyThrows
static void premain(String agentArguments, Instrumentation instrumentation) {
ClassLoader cl = JavaAgentLauncher.class.getClassLoader();
Enumeration<URL> it = cl.getResources(Constants.JAVA_AGENTS_FILE);
while (it.hasMoreElements()) {
URL url = it.nextElement();
Properties properties = new Properties();
try (InputStream is = url.openStream()) {
properties.load(is);
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String agentClassName = (String) entry.getKey();
String agentArgs = (String) entry.getValue();
Class<?> agentClass = cl.loadClass(agentClassName);
Method premainMethod = agentClass.getMethod("premain", String.class, Instrumentation.class);
premainMethod.invoke(null, agentArgs, instrumentation);
}
}
}
static void agentmain(String agentArguments, Instrumentation instrumentation) {
premain(agentArguments, instrumentation);
}
}

View File

@@ -1,8 +1,6 @@
package net.woggioni.executable.jar;
import java.io.InputStream;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
@@ -10,22 +8,23 @@ import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import lombok.SneakyThrows;
import lombok.extern.java.Log;
@Log
public class Launcher {
@@ -50,9 +49,14 @@ public class Launcher {
try(InputStream is = manifestResource.openStream()) {
mf.read(is);
}
try(FileSystem fs = FileSystems.newFileSystem(Path.of(currentJar.getPath()), null)) {
try(FileSystem fs = FileSystems.newFileSystem(Paths.get(currentJar), null)) {
Attributes mainAttributes = mf.getMainAttributes();
Collector<Path, ArrayList<Path>, List<Path>> immutableListCollector = Collector.of(
ArrayList::new,
List::add,
(l1, l2) -> { l1.addAll(l2); return l1; },
Collections::unmodifiableList);
List<Path> jarList = StreamSupport.stream(fs.getRootDirectories().spliterator(), false).flatMap(new Function<Path, Stream<Path>>() {
@Override
@SneakyThrows
@@ -67,32 +71,11 @@ public class Launcher {
public Stream<Path> apply(Path path) {
return StreamSupport.stream(FileSystems.newFileSystem(path, null).getRootDirectories().spliterator(), false);
}
}).collect(Collectors.toUnmodifiableList());
}).collect(immutableListCollector);
// for (Map.Entry<String, Attributes> entry : mf.getEntries().entrySet()) {
// String jarEntryName = entry.getKey();
// Attributes attributes = entry.getValue();
// if (jarEntryName.startsWith(Constants.LIBRARIES_FOLDER + '/') && attributes.getValue(Constants.ManifestAttributes.ENTRY_HASH) != null) {
// jarList.add(fs.getPath(jarEntryName));
// }
// }
Path[] jars = jarList.toArray(new Path[jarList.size()]);
ClassLoader pathClassLoader = new PathClassLoader(jars);
String mainClassName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_CLASS);
String mainModuleName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_MODULE);
Class<?> mainClass;
if (mainModuleName == null) {
mainClass = pathClassLoader.loadClass(mainClassName);
} else {
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration bootConfiguration = ModuleLayer.boot().configuration();
Configuration cfg = bootConfiguration.resolve(ModuleFinder.of(jars), ModuleFinder.of(), Arrays.asList(mainModuleName));
ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cfg, pathClassLoader);
Module mainModule = layer.findModule(mainModuleName).orElseThrow(
() -> new IllegalStateException(String.format("Main module '%s' not found", mainModuleName)));
mainClass = Class.forName(mainModule, mainClassName);
}
Class<?> mainClass = MainClassLoader.loadMainClass(jarList, mainModuleName, mainClassName);
try {
Method mainMethod = mainClass.getMethod("main", String[].class);
Class<?> returnType = mainMethod.getReturnType();

View File

@@ -0,0 +1,13 @@
package net.woggioni.executable.jar;
import java.nio.file.Path;
import lombok.SneakyThrows;
class MainClassLoader {
@SneakyThrows
static Class<?> loadMainClass(Iterable<Path> roots, String mainModuleName, String mainClassName) {
ClassLoader pathClassLoader = new net.woggioni.xclassloader.PathClassLoader(roots);
return pathClassLoader.loadClass(mainClassName);
}
}

View File

@@ -1,19 +1,26 @@
package net.woggioni.executable.jar;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.nio.file.*;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
/**
* A classloader that loads classes from a {@link Path} instance
@@ -153,8 +160,30 @@ public final class PathClassLoader extends ClassLoader {
@Override
@SneakyThrows
protected URLConnection openConnection(URL url) {
URI uri = url.toURI();
Path path = Paths.get(uri);
List<String> stack = new ArrayList<>();
URL currentURL = url;
while(true) {
String file = currentURL.getFile();
int exclamationMark = file.lastIndexOf('!');
if(exclamationMark != -1) {
stack.add(file.substring(exclamationMark + 1));
currentURL = new URL(file.substring(0, exclamationMark));
} else {
stack.add(file);
break;
}
}
Path path;
FileSystem fs = FileSystems.getDefault();
while(true) {
String pathString = stack.remove(stack.size() - 1);
path = fs.getPath(pathString);
if(stack.isEmpty()) break;
else {
fs = FileSystems.newFileSystem(path, null);
}
}
return new PathURLConnection(url, path);
}
}

View File

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

View File

@@ -0,0 +1,39 @@
package net.woggioni.executable.jar;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Optional;
import lombok.SneakyThrows;
import net.woggioni.xclassloader.PathClassLoader;
import net.woggioni.xclassloader.PathModuleFinder;
class MainClassLoader {
@SneakyThrows
static Class<?> loadMainClass(Iterable<Path> roots, String mainModuleName, String mainClassName) {
if (mainModuleName == null) {
ClassLoader pathClassLoader = new net.woggioni.xclassloader.PathClassLoader(roots);
return pathClassLoader.loadClass(mainClassName);
} else {
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration bootConfiguration = bootLayer.configuration();
Configuration cfg = bootConfiguration.resolve(new PathModuleFinder(roots), ModuleFinder.of(), Collections.singletonList(mainModuleName));
ClassLoader pathClassLoader = new PathClassLoader(roots, cfg, null);
ModuleLayer.Controller controller =
ModuleLayer.defineModules(cfg, Collections.singletonList(ModuleLayer.boot()), moduleName -> pathClassLoader);
ModuleLayer layer = controller.layer();
for(Module module : layer.modules()) {
controller.addReads(module, pathClassLoader.getUnnamedModule());
}
Module mainModule = layer.findModule(mainModuleName).orElseThrow(
() -> new IllegalStateException(String.format("Main module '%s' not found", mainModuleName)));
return 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

@@ -22,6 +22,7 @@ import java.util.stream.StreamSupport;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.gradle.internal.impldep.org.junit.Ignore;
import org.junit.jupiter.api.Test;
public class FooTest {
@@ -78,6 +79,7 @@ public class FooTest {
}
@Test
@Ignore
@SneakyThrows
void test2() {
FileSystem fs = FileSystems.newFileSystem(new URI("jar:file:/home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar"), new HashMap<>());