Skip to content

Commit 951d0b0

Browse files
committed
Fix exploded jar classloader issues
Fix a bug in `ExplodedURLClassLoader` and merge the code into the existing `LaunchedURLClassLoader` class. Also polish a few method names relating to layer support. See gh-19848 See gh-19767
1 parent b422923 commit 951d0b0

File tree

6 files changed

+42
-63
lines changed

6 files changed

+42
-63
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/ExecutableArchiveLauncher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ protected void postProcessClassPathArchives(List<Archive> archives) throws Excep
164164
}
165165

166166
@Override
167-
protected boolean supportsNestedJars() {
168-
return this.archive.supportsNestedJars();
167+
protected boolean isExploded() {
168+
return this.archive.isExploded();
169169
}
170170

171171
/**

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/ExplodedURLClassLoader.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,33 @@ public class LaunchedURLClassLoader extends URLClassLoader {
4242
ClassLoader.registerAsParallelCapable();
4343
}
4444

45+
private final boolean exploded;
46+
4547
/**
4648
* Create a new {@link LaunchedURLClassLoader} instance.
4749
* @param urls the URLs from which to load classes and resources
4850
* @param parent the parent class loader for delegation
4951
*/
5052
public LaunchedURLClassLoader(URL[] urls, ClassLoader parent) {
53+
this(false, urls, parent);
54+
}
55+
56+
/**
57+
* Create a new {@link LaunchedURLClassLoader} instance.
58+
* @param exploded the the underlying archive is exploded
59+
* @param urls the URLs from which to load classes and resources
60+
* @param parent the parent class loader for delegation
61+
*/
62+
public LaunchedURLClassLoader(boolean exploded, URL[] urls, ClassLoader parent) {
5163
super(urls, parent);
64+
this.exploded = exploded;
5265
}
5366

5467
@Override
5568
public URL findResource(String name) {
69+
if (this.exploded) {
70+
return super.findResource(name);
71+
}
5672
Handler.setUseFastConnectionExceptions(true);
5773
try {
5874
return super.findResource(name);
@@ -64,6 +80,9 @@ public URL findResource(String name) {
6480

6581
@Override
6682
public Enumeration<URL> findResources(String name) throws IOException {
83+
if (this.exploded) {
84+
return super.findResources(name);
85+
}
6786
Handler.setUseFastConnectionExceptions(true);
6887
try {
6988
return new UseFastConnectionExceptionsEnumeration(super.findResources(name));
@@ -86,6 +105,9 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
86105
catch (ClassNotFoundException ex) {
87106
}
88107
}
108+
if (this.exploded) {
109+
return super.loadClass(name, resolve);
110+
}
89111
Handler.setUseFastConnectionExceptions(true);
90112
try {
91113
try {
@@ -168,6 +190,9 @@ private void definePackage(String className, String packageName) {
168190
* Clear URL caches.
169191
*/
170192
public void clearCache() {
193+
if (this.exploded) {
194+
return;
195+
}
171196
for (URL url : getURLs()) {
172197
try {
173198
URLConnection connection = url.openConnection();

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public abstract class Launcher {
4949
* @throws Exception if the application fails to launch
5050
*/
5151
protected void launch(String[] args) throws Exception {
52-
if (supportsNestedJars()) {
52+
if (!isExploded()) {
5353
JarFile.registerUrlProtocolHandler();
5454
}
5555
ClassLoader classLoader = createClassLoader(getClassPathArchivesIterator());
@@ -94,10 +94,7 @@ protected ClassLoader createClassLoader(Iterator<Archive> archives) throws Excep
9494
* @throws Exception if the classloader cannot be created
9595
*/
9696
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
97-
if (supportsNestedJars()) {
98-
return new LaunchedURLClassLoader(urls, getClass().getClassLoader());
99-
}
100-
return new ExplodedURLClassLoader(urls, getClass().getClassLoader());
97+
return new LaunchedURLClassLoader(isExploded(), urls, getClass().getClassLoader());
10198
}
10299

103100
/**
@@ -168,12 +165,12 @@ protected final Archive createArchive() throws Exception {
168165
}
169166

170167
/**
171-
* Returns if the launcher needs to support fully nested JARs. If this method returns
172-
* {@code false} then only regular JARs are supported and the additional URL and
173-
* ClassLoader support infrastructure will not be installed.
174-
* @return if nested JARs are supported
168+
* Returns if the launcher is running in an exploded mode. If this method returns
169+
* {@code true} then only regular JARs are supported and the additional URL and
170+
* ClassLoader support infrastructure can be optimized.
171+
* @return if the jar is exploded.
175172
*/
176-
protected boolean supportsNestedJars() {
173+
protected boolean isExploded() {
177174
return true;
178175
}
179176

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/archive/Archive.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ default List<Archive> getNestedArchives(EntryFilter filter) throws IOException {
9090
@Override
9191
Iterator<Entry> iterator();
9292

93-
default boolean supportsNestedJars() {
94-
return true;
93+
/**
94+
* Return if the archive is exploded (already unpacked).
95+
* @return if the archive is exploded
96+
*/
97+
default boolean isExploded() {
98+
return false;
9599
}
96100

97101
/**

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/archive/ExplodedArchive.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ protected Archive getNestedArchive(Entry entry) throws IOException {
114114
}
115115

116116
@Override
117-
public boolean supportsNestedJars() {
118-
return false;
117+
public boolean isExploded() {
118+
return true;
119119
}
120120

121121
@Override

0 commit comments

Comments
 (0)