initial commit
This commit is contained in:
62
launcher/build.gradle
Normal file
62
launcher/build.gradle
Normal file
@@ -0,0 +1,62 @@
|
||||
import java.util.jar.Attributes
|
||||
|
||||
plugins {
|
||||
id "net.woggioni.gradle.multi-release-jar"
|
||||
}
|
||||
|
||||
ext.setProperty("jpms.module.name", "net.woggioni.executable.jar")
|
||||
|
||||
configurations {
|
||||
embedded
|
||||
compileOnly.extendsFrom(embedded)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
embedded project(path: ":common", configuration: 'archives')
|
||||
embedded group: "net.woggioni", name: "xclassloader", version: getProperty("version.xclassloader")
|
||||
}
|
||||
|
||||
java {
|
||||
modularity.inferModulePath = true
|
||||
}
|
||||
|
||||
tasks.withType(JavaCompile).configureEach {
|
||||
javaCompiler = javaToolchains.compilerFor {
|
||||
languageVersion = JavaLanguageVersion.of(16)
|
||||
}
|
||||
options.forkOptions.jvmArgs << "--illegal-access=permit"
|
||||
}
|
||||
|
||||
jar {
|
||||
manifest {
|
||||
attributes([
|
||||
(Attributes.Name.SPECIFICATION_TITLE) : "executable-jar-launcher",
|
||||
(Attributes.Name.SEALED) : true
|
||||
].collectEntries {
|
||||
[it.key.toString(), it.value.toString()]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
tasks.register("tar", Tar) {
|
||||
archiveFileName = "${project.name}.tar"
|
||||
from(project.tasks.named(JavaPlugin.JAR_TASK_NAME)
|
||||
.flatMap(Jar.&getArchiveFile)
|
||||
.map(RegularFile.&getAsFile)
|
||||
.map(project.&zipTree))
|
||||
from(configurations.named('embedded').map {
|
||||
it.collect {
|
||||
it.isDirectory() ? it : zipTree(it)
|
||||
}
|
||||
}) {
|
||||
exclude("**/module-info.class")
|
||||
exclude("META-INF/MANIFEST.MF")
|
||||
}
|
||||
}
|
||||
|
||||
tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
|
||||
doFirst {
|
||||
String path = project(":common").extensions.getByType(JavaPluginExtension).sourceSets.named("main").get().output.asPath
|
||||
options.compilerArgs.addAll(["--patch-module", "net.woggioni.executable.jar=$path"])
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
package net.woggioni.executable.jar;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
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 {
|
||||
|
||||
@SneakyThrows
|
||||
private static URI findCurrentJar() {
|
||||
String launcherClassName = Launcher.class.getName();
|
||||
URL url = Launcher.class.getClassLoader().getResource(launcherClassName.replace('.', '/') + ".class");
|
||||
if (url == null || !"jar".equals(url.getProtocol()))
|
||||
throw new IllegalStateException(String.format("The class %s must be used inside a JAR file", launcherClassName));
|
||||
String path = url.getPath();
|
||||
return new URI(path.substring(0, path.indexOf('!')));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static void main(String[] args) {
|
||||
URI currentJar = findCurrentJar();
|
||||
URL manifestResource = Launcher.class.getResource("/" + JarFile.MANIFEST_NAME);
|
||||
if(Objects.isNull(manifestResource)) {
|
||||
throw new RuntimeException("Launcher manifest not found");
|
||||
}
|
||||
Manifest mf = new Manifest();
|
||||
try(InputStream is = manifestResource.openStream()) {
|
||||
mf.read(is);
|
||||
}
|
||||
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
|
||||
public Stream<Path> apply(Path path) {
|
||||
return Files.list(path.resolve(Constants.LIBRARIES_FOLDER))
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> p.getFileName().toString().endsWith(".jar"));
|
||||
}
|
||||
}).flatMap(new Function<Path, Stream<Path>>() {
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Stream<Path> apply(Path path) {
|
||||
return StreamSupport.stream(FileSystems.newFileSystem(path, null).getRootDirectories().spliterator(), false);
|
||||
}
|
||||
}).collect(immutableListCollector);
|
||||
|
||||
String mainClassName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_CLASS);
|
||||
String mainModuleName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_MODULE);
|
||||
Class<?> mainClass = MainClassLoader.loadMainClass(jarList, mainModuleName, mainClassName);
|
||||
try {
|
||||
Method mainMethod = mainClass.getMethod("main", String[].class);
|
||||
Class<?> returnType = mainMethod.getReturnType();
|
||||
if (mainMethod.getReturnType() != Void.TYPE) {
|
||||
throw new IllegalArgumentException(String.format("Main method in class '%s' " +
|
||||
"has wrong return type, expected '%s', found '%s' instead", mainClass, Void.class.getName(), returnType));
|
||||
}
|
||||
mainMethod.invoke(null, (Object) args);
|
||||
} catch (NoSuchMethodException nsme) {
|
||||
throw new IllegalArgumentException(String.format("No valid main method found in class '%s'", mainClass), nsme);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,190 @@
|
||||
package net.woggioni.executable.jar;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
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.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
|
||||
*/
|
||||
public final class PathClassLoader extends ClassLoader {
|
||||
|
||||
private final Iterable<Path> paths;
|
||||
|
||||
static {
|
||||
registerAsParallelCapable();
|
||||
}
|
||||
|
||||
public PathClassLoader(Path ...path) {
|
||||
this(Arrays.asList(path), null);
|
||||
}
|
||||
|
||||
public PathClassLoader(Iterable<Path> paths) {
|
||||
this(paths, null);
|
||||
}
|
||||
|
||||
public PathClassLoader(Iterable<Path> paths, ClassLoader parent) {
|
||||
super(parent);
|
||||
this.paths = paths;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
protected Class<?> findClass(String name) {
|
||||
String resource = name.replace('.', '/').concat(".class");
|
||||
for(Path path : paths) {
|
||||
Path classPath = path.resolve(resource);
|
||||
if (Files.exists(classPath)) {
|
||||
byte[] byteCode = Files.readAllBytes(classPath);
|
||||
return defineClass(name, byteCode, 0, byteCode.length);
|
||||
}
|
||||
}
|
||||
throw new ClassNotFoundException(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
protected URL findResource(String name) {
|
||||
for(Path path : paths) {
|
||||
Path resolved = path.resolve(name);
|
||||
if (Files.exists(resolved)) {
|
||||
return toURL(resolved);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Enumeration<URL> findResources(final String name) throws IOException {
|
||||
final List<URL> resources = new ArrayList<>(1);
|
||||
for(Path path : paths) {
|
||||
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
if (!name.isEmpty()) {
|
||||
this.addIfMatches(resources, file);
|
||||
}
|
||||
return super.visitFile(file, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||
if (!name.isEmpty() || path.equals(dir)) {
|
||||
this.addIfMatches(resources, dir);
|
||||
}
|
||||
return super.preVisitDirectory(dir, attrs);
|
||||
}
|
||||
|
||||
void addIfMatches(List<URL> resources, Path file) throws IOException {
|
||||
if (path.relativize(file).toString().equals(name)) {
|
||||
resources.add(toURL(file));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return Collections.enumeration(resources);
|
||||
}
|
||||
|
||||
private static URL toURL(Path path) throws IOException {
|
||||
return new URL(null, path.toUri().toString(), PathURLStreamHandler.INSTANCE);
|
||||
}
|
||||
|
||||
private static final class PathURLConnection extends URLConnection {
|
||||
|
||||
private final Path path;
|
||||
|
||||
PathURLConnection(URL url, Path path) {
|
||||
super(url);
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {}
|
||||
|
||||
@Override
|
||||
public long getContentLengthLong() {
|
||||
try {
|
||||
return Files.size(this.path);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("could not get size of: " + this.path, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return Files.newInputStream(this.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return Files.newOutputStream(this.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String getContentType() {
|
||||
return Files.probeContentType(this.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public long getLastModified() {
|
||||
BasicFileAttributes attributes = Files.readAttributes(this.path, BasicFileAttributes.class);
|
||||
return attributes.lastModifiedTime().toMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
private static final class PathURLStreamHandler extends URLStreamHandler {
|
||||
|
||||
static final URLStreamHandler INSTANCE = new PathURLStreamHandler();
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
protected URLConnection openConnection(URL url) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
6
launcher/src/main/java9/module-info.java
Normal file
6
launcher/src/main/java9/module-info.java
Normal file
@@ -0,0 +1,6 @@
|
||||
module net.woggioni.executable.jar {
|
||||
requires java.logging;
|
||||
requires static lombok;
|
||||
requires net.woggioni.xclassloader;
|
||||
requires java.instrument;
|
||||
}
|
@@ -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())));
|
||||
}
|
||||
}
|
||||
}
|
149
launcher/src/test/java/net/woggioni/executable/jar/FooTest.java
Normal file
149
launcher/src/test/java/net/woggioni/executable/jar/FooTest.java
Normal file
@@ -0,0 +1,149 @@
|
||||
package net.woggioni.executable.jar;
|
||||
|
||||
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.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
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 {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void foo() {
|
||||
Path.of(new URI("jar:file:///home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void foo2() {
|
||||
Path p = Path.of(new URI("file:///home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar"));
|
||||
FileSystem fs = FileSystems.newFileSystem(p, null);
|
||||
StreamSupport.stream(fs.getRootDirectories().spliterator(), false).flatMap(new Function<Path, Stream<?>>() {
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Stream<?> apply(Path path) {
|
||||
return Files.list(path);
|
||||
}
|
||||
}).forEach(r -> {
|
||||
System.out.println(r);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void test() {
|
||||
Path fatJar = Path.of("/home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar");
|
||||
List<Path> jars = StreamSupport.stream(FileSystems.newFileSystem(fatJar, null).getRootDirectories().spliterator(), false)
|
||||
.flatMap(new Function<Path, Stream<? extends Path>>() {
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Stream<? extends Path> apply(Path root) {
|
||||
Path libDir = root.resolve("/LIB-INF");
|
||||
if (Files.exists(libDir) && Files.isDirectory(libDir)) {
|
||||
return Files.list(libDir);
|
||||
} else {
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
||||
}).flatMap(new Function<Path, Stream<Path>>() {
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Stream<Path> apply(Path path) {
|
||||
return StreamSupport.stream(FileSystems.newFileSystem(path, null).getRootDirectories().spliterator(), false);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
PathClassLoader p = new PathClassLoader(jars.toArray(new Path[jars.size()]));
|
||||
Class<?> cl = p.loadClass("net.woggioni.wson.serialization.binary.JBONParser");
|
||||
System.out.println(cl);
|
||||
URL resource = p.findResource("citylots.json.xz");
|
||||
resource.openStream();
|
||||
}
|
||||
|
||||
@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<>());
|
||||
String s = "jar:jar:file:///home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar!/LIB-INF/wson-test-utils-1.0.jar!/citylots.json.xz";
|
||||
URI uri = new URI(s);
|
||||
Files.list(Path.of(uri)).forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static final class PathURLConnection extends URLConnection {
|
||||
|
||||
private final Path path;
|
||||
|
||||
PathURLConnection(URL url, Path path) {
|
||||
super(url);
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {}
|
||||
|
||||
@Override
|
||||
public long getContentLengthLong() {
|
||||
try {
|
||||
return Files.size(this.path);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("could not get size of: " + this.path, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return Files.newInputStream(this.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return Files.newOutputStream(this.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String getContentType() {
|
||||
return Files.probeContentType(this.path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public long getLastModified() {
|
||||
BasicFileAttributes attributes = Files.readAttributes(this.path, BasicFileAttributes.class);
|
||||
return attributes.lastModifiedTime().toMillis();
|
||||
}
|
||||
}
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
private static final class PathURLStreamHandler extends URLStreamHandler {
|
||||
|
||||
static final URLStreamHandler INSTANCE = new PathURLStreamHandler();
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
protected URLConnection openConnection(URL url) {
|
||||
URI uri = url.toURI();
|
||||
Path path = Paths.get(uri);
|
||||
return new PathURLConnection(url, path);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user