From 13ed724c096a569419de3f415b37e2044543920c Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Wed, 12 May 2021 10:14:16 +1000 Subject: [PATCH] Fix last modified and CL for memory resources (cherry picked from commit 7e618cb3673ad7362d2515e61e82ff9678b37734) --- .../classloading/MemoryClassPathElement.java | 17 ++++++++++ .../ClassLoadingResourceUrlTestCase.java | 33 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/MemoryClassPathElement.java b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/MemoryClassPathElement.java index 05db1d17360bd..cc875a8cb9600 100644 --- a/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/MemoryClassPathElement.java +++ b/independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/classloading/MemoryClassPathElement.java @@ -18,6 +18,7 @@ public class MemoryClassPathElement extends AbstractClassPathElement { private volatile Map resources; + private volatile long lastModified = System.currentTimeMillis(); public MemoryClassPathElement(Map resources) { this.resources = resources; @@ -39,6 +40,7 @@ public void reset(Map resources) { } } this.resources = newResources; + lastModified = System.currentTimeMillis(); } @Override @@ -128,6 +130,21 @@ public void connect() throws IOException { public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(resources.get(name)); } + + @Override + public long getLastModified() { + return lastModified; + } + + @Override + public int getContentLength() { + return resources.get(name).length; + } + + @Override + public long getContentLengthLong() { + return resources.get(name).length; + } }; } } diff --git a/independent-projects/bootstrap/core/src/test/java/io/quarkus/bootstrap/classloader/ClassLoadingResourceUrlTestCase.java b/independent-projects/bootstrap/core/src/test/java/io/quarkus/bootstrap/classloader/ClassLoadingResourceUrlTestCase.java index 611d70bc06df7..3118acda91d24 100644 --- a/independent-projects/bootstrap/core/src/test/java/io/quarkus/bootstrap/classloader/ClassLoadingResourceUrlTestCase.java +++ b/independent-projects/bootstrap/core/src/test/java/io/quarkus/bootstrap/classloader/ClassLoadingResourceUrlTestCase.java @@ -2,12 +2,18 @@ import io.quarkus.bootstrap.classloading.DirectoryClassPathElement; import io.quarkus.bootstrap.classloading.JarClassPathElement; +import io.quarkus.bootstrap.classloading.MemoryClassPathElement; import io.quarkus.bootstrap.classloading.QuarkusClassLoader; import io.quarkus.bootstrap.util.IoUtils; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Collections; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.exporter.ExplodedExporter; @@ -114,4 +120,31 @@ public void testUrlReturnedFromClassLoaderJarFile() throws Exception { IoUtils.recursiveDelete(path); } } + + @Test + public void testMemoryUrlConnections() throws Exception { + long start = System.currentTimeMillis(); + Thread.sleep(2); + + ClassLoader cl = QuarkusClassLoader.builder("test", getClass().getClassLoader(), false) + .addElement( + new MemoryClassPathElement(Collections.singletonMap("a.txt", "hello".getBytes(StandardCharsets.UTF_8)))) + .build(); + URL res = cl.getResource("a.txt"); + Assertions.assertNotNull(res); + URLConnection urlConnection = res.openConnection(); + Assertions.assertEquals(5, urlConnection.getContentLength()); + Assertions.assertTrue(urlConnection.getLastModified() > start); + Assertions.assertEquals("hello", new String(readAllBytes(urlConnection.getInputStream()), StandardCharsets.UTF_8)); + + } + + public static byte[] readAllBytes(InputStream is) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + byte[] buffer = new byte[0xFFFF]; + for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { + os.write(buffer, 0, len); + } + return os.toByteArray(); + } }