Clear caches on ApplicationContext load

Ensure that JarFile caches are cleared once the ApplicationContext has
loaded. Caches are cleared manually with the assumption that no
further class loading is likely.

Closes gh-4882
This commit is contained in:
Phillip Webb
2016-01-25 11:10:42 -08:00
parent e2368b909b
commit acbb4e63f2
5 changed files with 89 additions and 0 deletions

View File

@@ -17,8 +17,10 @@
package org.springframework.boot.loader;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
@@ -228,6 +230,30 @@ public class LaunchedURLClassLoader extends URLClassLoader {
}
}
/**
* Clear URL caches.
*/
public void clearCache() {
for (URL url : getURLs()) {
try {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
clearCache(connection);
}
}
catch (IOException ex) {
}
}
}
private void clearCache(URLConnection connection) throws IOException {
Object jarFile = ((JarURLConnection) connection).getJarFile();
if (jarFile instanceof JarFile) {
((JarFile) jarFile).clearCache();
}
}
@UsesJava7
private static LockProvider setupLockProvider() {
try {

View File

@@ -347,6 +347,10 @@ public class JarFile extends java.util.jar.JarFile {
}
}
public void clearCache() {
this.entries.clearCache();
}
/**
* Register a {@literal 'java.protocol.handler.pkgs'} property so that a
* {@link URLStreamHandler} will be located to deal with jar URLs.

View File

@@ -270,6 +270,10 @@ class JarFileEntries implements CentralDirectoryVistor, Iterable<JarEntry> {
return index;
}
public void clearCache() {
this.entriesCache.clear();
}
private AsciiBytes applyFilter(AsciiBytes name) {
return (this.filter == null ? name : this.filter.apply(name));
}