added JPMS URL stream handler
All checks were successful
CI / build (push) Successful in 1m41s

This commit is contained in:
2025-01-09 16:13:38 +08:00
parent 8af4a84e49
commit 89817c5624
16 changed files with 176 additions and 11 deletions

View File

@@ -8,7 +8,8 @@ jobs:
steps:
- name: Checkout sources
uses: actions/checkout@v4
- uses: actions/setup-java@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: graalvm
java-version: 21

View File

@@ -27,6 +27,7 @@ allprojects {
}
dependencies {
testRuntimeOnly project(':jwo-test-module')
testImplementation catalog.junit.jupiter.api
testImplementation catalog.junit.jupiter.params
testRuntimeOnly catalog.junit.jupiter.engine
@@ -97,7 +98,6 @@ ext {
setProperty('jpms.module.name', 'net.woggioni.jwo')
}
configurations {
pathClassloaderTest
zipTestBundle {
@@ -144,5 +144,3 @@ test {
'--add-opens', 'java.base/sun.nio.fs=ALL-UNNAMED',
])
}

View File

@@ -3,6 +3,6 @@ org.gradle.parallel=true
org.gradle.caching=true
gitea.maven.url = https://gitea.woggioni.net/api/packages/woggioni/maven
jwo.version = 2024.12.31
lys.version = 2024.12.28
jwo.version = 2025.01.09
lys.version = 2025.01.08
guice.version = 5.0.1

View File

@@ -0,0 +1,7 @@
plugins {
id 'java-library'
}
java {
modularity.inferModulePath = true
}

View File

@@ -0,0 +1,3 @@
open module net.woggioni.jwo.test.module {
exports my.test.resource;
}

View File

@@ -0,0 +1,4 @@
package my.test.resource;
public class Foo {
}

View File

@@ -0,0 +1 @@
test

31
jwo-test/build.gradle Normal file
View File

@@ -0,0 +1,31 @@
plugins {
id 'java-library'
alias(catalog.plugins.lombok)
}
import org.gradle.api.attributes.LibraryElements
import static org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE
import static org.gradle.api.attributes.LibraryElements.JAR
configurations {
testImplementation {
attributes {
attribute(LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements.class, JAR))
}
}
testRuntimeClasspath {
attributes {
attribute(LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements.class, JAR))
}
}
}
java {
modularity.inferModulePath = true
}
dependencies {
testImplementation project(':jwo-test-module')
testImplementation project(':')
}

View File

@@ -0,0 +1,6 @@
open module net.woggioni.jwo.unit.test {
requires net.woggioni.jwo;
requires net.woggioni.jwo.test.module;
requires org.junit.jupiter.api;
requires static lombok;
}

View File

@@ -0,0 +1,29 @@
package net.woggioni.jwo.test;
import lombok.SneakyThrows;
import net.woggioni.jwo.JWO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class JpmsUrlTest {
@Test
@SneakyThrows
public void test() {
JWO.registerUrlProtocolHandler();
final var module = getClass().getModule();
final var url = new URL("jpms://net.woggioni.jwo.test.module/my/test/resource/file.txt");
System.out.println(module.getName());
System.out.println(url.getHost());
System.out.println(url.getPath());
final var baos = new ByteArrayOutputStream();
try(final var is = url.openConnection().getInputStream()) {
JWO.copy(is, baos);
}
final var content = baos.toString(StandardCharsets.UTF_8);
Assertions.assertEquals("test", content);
}
}

View File

@@ -25,6 +25,8 @@ dependencyResolutionManagement {
rootProject.name = 'jwo'
include('benchmark')
include('jmath')
include('jmath-benchmark')
include 'benchmark'
include 'jmath'
include 'jmath-benchmark'
include 'jwo-test'
include 'jwo-test-module'

View File

@@ -1,6 +1,7 @@
package net.woggioni.jwo;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
@@ -14,6 +15,7 @@ public class UncloseableOutputStream extends FilterOutputStream {
}
@Override
public void close() {
public void close() throws IOException {
flush();
}
}

View File

@@ -10,5 +10,7 @@ public class UncloseableWriter extends FilterWriter {
}
@Override
public void close() throws IOException {}
public void close() throws IOException {
flush();
}
}

View File

@@ -7,5 +7,6 @@ module net.woggioni.jwo {
exports net.woggioni.jwo;
exports net.woggioni.jwo.exception;
exports net.woggioni.jwo.url.classpath;
exports net.woggioni.jwo.url.jpms;
exports net.woggioni.jwo.xml;
}

View File

@@ -0,0 +1,52 @@
package net.woggioni.jwo.url.jpms;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Module;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Optional;
public class Handler extends URLStreamHandler {
private final ClassLoader classLoader;
public Handler() {
this.classLoader = getClass().getClassLoader();
}
public Handler(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
protected URLConnection openConnection(URL u) throws IOException {
final Module thisModule = getClass().getModule();
final Module sourceModule = Optional.ofNullable(thisModule)
.map(Module::getLayer)
.flatMap(layer -> {
final String moduleName = u.getHost();
return layer.findModule(moduleName);
}).orElse(thisModule);
return new ModuleResourceURLConnection(u, sourceModule);
}
private static class ModuleResourceURLConnection extends URLConnection {
private final Module module;
private ModuleResourceURLConnection(URL url, Module module) {
super(url);
this.module = module;
}
@Override
public void connect() {
}
@Override
public InputStream getInputStream() throws IOException {
return module.getResourceAsStream(getURL().getPath());
}
}
}

View File

@@ -0,0 +1,26 @@
package net.woggioni.jwo.url;
import lombok.SneakyThrows;
import net.woggioni.jwo.JWO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class JpmsUrlTest {
@Test
@SneakyThrows
public void test() {
JWO.registerUrlProtocolHandler();
final var module = getClass().getModule();
final var url = new URL("jpms://net.woggioni.jwo.test.module/my/test/resource/file.txt");
final var baos = new ByteArrayOutputStream();
try(final var is = url.openConnection().getInputStream()) {
JWO.copy(is, baos);
}
final var content = baos.toString(StandardCharsets.UTF_8);
Assertions.assertEquals("test", content);
}
}