Skip to content

Commit ccfc470

Browse files
author
Dave Syer
committed
Fix URL filtering when loading from a directory
The ExplodedArchive would erroneously always attempt to filter its contents (and thereby shield them from a classloader that wrapped it) even if they haven't been explicitly provided. See gh-1352
1 parent fbd429c commit ccfc470

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ExplodedArchive extends Archive {
5656

5757
private Manifest manifest;
5858

59-
private boolean recursive = true;
59+
private boolean filtered = false;
6060

6161
/**
6262
* Create a new {@link ExplodedArchive} instance.
@@ -78,17 +78,18 @@ public ExplodedArchive(File root, boolean recursive) {
7878
throw new IllegalArgumentException("Invalid source folder " + root);
7979
}
8080
this.root = root;
81-
this.recursive = recursive;
82-
buildEntries(root);
81+
buildEntries(root, recursive);
8382
this.entries = Collections.unmodifiableMap(this.entries);
8483
}
8584

8685
private ExplodedArchive(File root, Map<AsciiBytes, Entry> entries) {
8786
this.root = root;
87+
// The entries are pre-filtered
88+
this.filtered = true;
8889
this.entries = Collections.unmodifiableMap(entries);
8990
}
9091

91-
private void buildEntries(File file) {
92+
private void buildEntries(File file, boolean recursive) {
9293
if (!file.equals(this.root)) {
9394
String name = file.toURI().getPath()
9495
.substring(this.root.toURI().getPath().length());
@@ -102,9 +103,9 @@ private void buildEntries(File file) {
102103
}
103104
for (File child : files) {
104105
if (!SKIPPED_NAMES.contains(child.getName())) {
105-
if (file.equals(this.root) || this.recursive
106+
if (file.equals(this.root) || recursive
106107
|| file.getName().equals("META-INF")) {
107-
buildEntries(child);
108+
buildEntries(child, recursive);
108109
}
109110
}
110111
}
@@ -113,7 +114,8 @@ private void buildEntries(File file) {
113114

114115
@Override
115116
public URL getUrl() throws MalformedURLException {
116-
FilteredURLStreamHandler handler = new FilteredURLStreamHandler();
117+
FilteredURLStreamHandler handler = this.filtered ? new FilteredURLStreamHandler()
118+
: null;
117119
return new URL("file", "", -1, this.root.toURI().getPath(), handler);
118120
}
119121

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/ExplodedArchiveTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public AsciiBytes apply(AsciiBytes entryName, Entry entry) {
147147
new URL[] { filteredArchive.getUrl() });
148148
assertThat(classLoader.getResourceAsStream("1.dat").read(), equalTo(1));
149149
assertThat(classLoader.getResourceAsStream("2.dat"), nullValue());
150+
classLoader.close();
150151
}
151152

152153
@Test
@@ -173,6 +174,25 @@ public void getNonRecursiveManifestEvenIfNonRecursive() throws Exception {
173174
assertThat(entries.size(), equalTo(3));
174175
}
175176

177+
@Test
178+
public void getResourceAsStream() throws Exception {
179+
ExplodedArchive archive = new ExplodedArchive(new File("src/test/resources/root"));
180+
assertNotNull(archive.getManifest());
181+
URLClassLoader loader = new URLClassLoader(new URL[] { archive.getUrl() });
182+
assertNotNull(loader.getResourceAsStream("META-INF/spring/application.xml"));
183+
loader.close();
184+
}
185+
186+
@Test
187+
public void getResourceAsStreamNonRecursive() throws Exception {
188+
ExplodedArchive archive = new ExplodedArchive(
189+
new File("src/test/resources/root"), false);
190+
assertNotNull(archive.getManifest());
191+
URLClassLoader loader = new URLClassLoader(new URL[] { archive.getUrl() });
192+
assertNotNull(loader.getResourceAsStream("META-INF/spring/application.xml"));
193+
loader.close();
194+
}
195+
176196
private Map<String, Archive.Entry> getEntriesMap(Archive archive) {
177197
Map<String, Archive.Entry> entries = new HashMap<String, Archive.Entry>();
178198
for (Archive.Entry entry : archive.getEntries()) {

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void jdkJarFile() throws Exception {
9797
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
9898
assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue());
9999
jarFile.close();
100+
urlClassLoader.close();
100101
}
101102

102103
@Test
@@ -139,6 +140,7 @@ public void getSpecialResourceViaClassLoader() throws Exception {
139140
URLClassLoader urlClassLoader = new URLClassLoader(
140141
new URL[] { this.jarFile.getUrl() });
141142
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
143+
urlClassLoader.close();
142144
}
143145

144146
@Test

0 commit comments

Comments
 (0)