Skip to content

Commit

Permalink
Fix last modified and CL for memory resources
Browse files Browse the repository at this point in the history
(cherry picked from commit 7e618cb)
  • Loading branch information
stuartwdouglas authored and gsmet committed May 12, 2021
1 parent 24c8f49 commit 13ed724
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class MemoryClassPathElement extends AbstractClassPathElement {

private volatile Map<String, byte[]> resources;
private volatile long lastModified = System.currentTimeMillis();

public MemoryClassPathElement(Map<String, byte[]> resources) {
this.resources = resources;
Expand All @@ -39,6 +40,7 @@ public void reset(Map<String, byte[]> resources) {
}
}
this.resources = newResources;
lastModified = System.currentTimeMillis();
}

@Override
Expand Down Expand Up @@ -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;
}
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 13ed724

Please sign in to comment.