Skip to content

Commit

Permalink
Add non-caching Jar URL stream handler
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Jun 16, 2024
1 parent 7605f72 commit 1c4f23b
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
import java.lang.System.Logger;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
Expand All @@ -47,6 +50,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarFile;

import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.Events;
Expand Down Expand Up @@ -216,6 +220,8 @@ private void addDelegates(Collection<URI> libURIs, DelegatingClassLoader holder)
*/
private static class URLClassFinder extends GlassfishUrlClassLoader implements ClassFinder {

private static final URLStreamHandler urlStreamHandler = new NonCachingURLStreamHandler();

private final Set<String> notFoundResources = ConcurrentHashMap.newKeySet();

URLClassFinder(URL url, ClassLoader parent) {
Expand Down Expand Up @@ -266,6 +272,13 @@ public URL findResource(String name) {
return null;
}
URL resourceURL = super.findResource(name);
if (resourceURL != null) {
try {
resourceURL = new URL(resourceURL, resourceURL.toExternalForm(), urlStreamHandler);
} catch (MalformedURLException e) {
resourceURL = null;
}
}
if (resourceURL == null){
notFoundResources.add(name);
}
Expand All @@ -286,6 +299,55 @@ public Enumeration<URL> findResources(String name) {
}
}

/**
* Non-caching {@link URLStreamHandler}.
*/
private static class NonCachingURLStreamHandler extends URLStreamHandler {

@Override
protected URLConnection openConnection(URL url) throws IOException {
return new NonCachedURLConnection(url);
}
}

/**
* Non-cached {@link JarURLConnection}.
*/
private static class NonCachedURLConnection extends JarURLConnection {

private JarURLConnection jarURLConnection;

public NonCachedURLConnection(URL url) throws MalformedURLException {
super(url);
}

@Override
public void connect() throws IOException {
if (jarURLConnection == null) {
URLConnection urlConnection = new URL(url.toExternalForm()).openConnection();
urlConnection.setUseCaches(false);
jarURLConnection = (JarURLConnection) urlConnection;
}
}

@Override
public JarFile getJarFile() throws IOException {
connect();
return jarURLConnection.getJarFile();
}

@Override
public InputStream getInputStream() throws IOException {
connect();
return jarURLConnection.getInputStream();
}

@Override
public void setUseCaches(boolean useCaches) {
// do nothing
}
}

/**
* This class loader has a list of class loaders called as delegates
* that it uses to find resources and classes.
Expand Down

0 comments on commit 1c4f23b

Please sign in to comment.