Skip to content

Commit 8168e8a

Browse files
author
Dave Syer
committed
Special case for root of classpath resource in archive
This turns out to affect JPA, but only because it looks for a URL for the root of the classpath using ClassLoader.getResource("") which barfs in an app launched from an executable JAR. It's easy to make a special case for "" in the class loader, so I went ahead and did that. Possibly need to think what the implication of getResources("") is as well (not tested in an app yet). Fixes gh-420
1 parent afac085 commit 8168e8a

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.net.URLClassLoader;
2222
import java.security.AccessController;
2323
import java.security.PrivilegedExceptionAction;
24+
import java.util.Arrays;
25+
import java.util.Collections;
2426
import java.util.Enumeration;
2527

2628
import org.springframework.boot.loader.jar.JarFile;
@@ -63,6 +65,28 @@ public URL getResource(String name) {
6365
return (url == null ? findResource(name) : url);
6466
}
6567

68+
@Override
69+
public URL findResource(String name) {
70+
if (name.equals("")) {
71+
URL[] urls = getURLs();
72+
if (urls.length > 0) {
73+
return urls[0];
74+
}
75+
}
76+
return super.findResource(name);
77+
}
78+
79+
@Override
80+
public Enumeration<URL> findResources(String name) throws IOException {
81+
if (name.equals("")) {
82+
URL[] urls = getURLs();
83+
if (urls.length > 0) {
84+
return Collections.enumeration(Arrays.asList(urls));
85+
}
86+
}
87+
return super.findResources(name);
88+
}
89+
6690
@Override
6791
public Enumeration<URL> getResources(String name) throws IOException {
6892

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ protected void launch(String[] args) {
7575
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
7676
List<URL> urls = new ArrayList<URL>(archives.size());
7777
for (Archive archive : archives) {
78+
// Add the current archive at end (it will be reversed and end up taking
79+
// precedence)
7880
urls.add(archive.getUrl());
7981
}
8082
return createClassLoader(urls.toArray(new URL[urls.size()]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader;
18+
19+
import java.net.URL;
20+
21+
import org.junit.Test;
22+
23+
import static org.junit.Assert.assertNotNull;
24+
import static org.junit.Assert.assertTrue;
25+
26+
/**
27+
* @author Dave Syer
28+
*/
29+
public class LaunchedURLClassLoaderTests {
30+
31+
@Test
32+
public void resolveResourceFromArchive() throws Exception {
33+
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
34+
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
35+
.getClassLoader());
36+
assertNotNull(loader.getResource("demo/Application.java"));
37+
}
38+
39+
@Test
40+
public void resolveResourcesFromArchive() throws Exception {
41+
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
42+
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
43+
.getClassLoader());
44+
assertTrue(loader.getResources("demo/Application.java").hasMoreElements());
45+
}
46+
47+
@Test
48+
public void resolveRootPathFromArchive() throws Exception {
49+
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
50+
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
51+
.getClassLoader());
52+
assertNotNull(loader.getResource(""));
53+
}
54+
55+
@Test
56+
public void resolveRootResourcesFromArchive() throws Exception {
57+
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
58+
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
59+
.getClassLoader());
60+
assertTrue(loader.getResources("").hasMoreElements());
61+
}
62+
63+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ public void nestedDirArchive() throws Exception {
126126
}
127127

128128
@Test
129-
@SuppressWarnings("resource")
130129
public void getFilteredArchive() throws Exception {
131130
Archive filteredArchive = this.archive
132131
.getFilteredArchive(new Archive.EntryRenameFilter() {

0 commit comments

Comments
 (0)