Skip to content

Commit 650e326

Browse files
committed
Align MessageSource path search fix with SPR-12095
Update ExtendedPathMatchingResourcePatternResolver to use similar code as the fix for SPR-12095. Fixes gh-1378
1 parent 9717b7d commit 650e326

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure;
1818

1919
import java.io.IOException;
20+
import java.net.MalformedURLException;
2021
import java.net.URL;
2122
import java.net.URLClassLoader;
2223
import java.util.Arrays;
@@ -40,8 +41,10 @@
4041
import org.springframework.core.Ordered;
4142
import org.springframework.core.annotation.Order;
4243
import org.springframework.core.io.Resource;
44+
import org.springframework.core.io.UrlResource;
4345
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
4446
import org.springframework.core.type.AnnotatedTypeMetadata;
47+
import org.springframework.util.ResourceUtils;
4548
import org.springframework.util.StringUtils;
4649

4750
import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
@@ -146,6 +149,10 @@ private static class ExtendedPathMatchingResourcePatternResolver extends
146149
private static final Log logger = LogFactory
147150
.getLog(PathMatchingResourcePatternResolver.class);
148151

152+
private static final String JAR_FILE_EXTENSION = ".jar";
153+
154+
private static final String JAR_URL_PREFIX = "jar:";
155+
149156
public ExtendedPathMatchingResourcePatternResolver(ClassLoader classLoader) {
150157
super(classLoader);
151158
}
@@ -160,42 +167,66 @@ protected Resource[] findAllClassPathResources(String location)
160167
if ("".equals(path)) {
161168
Set<Resource> result = new LinkedHashSet<Resource>(16);
162169
result.addAll(Arrays.asList(super.findAllClassPathResources(location)));
163-
addAllClassLoaderJarUrls(getClassLoader(), result);
170+
addAllClassLoaderJarRoots(getClassLoader(), result);
164171
return result.toArray(new Resource[result.size()]);
165172
}
166173
return super.findAllClassPathResources(location);
167174
}
168175

169-
private void addAllClassLoaderJarUrls(ClassLoader classLoader,
176+
private void addAllClassLoaderJarRoots(ClassLoader classLoader,
170177
Set<Resource> result) {
171178
if (classLoader != null) {
172179
if (classLoader instanceof URLClassLoader) {
173-
addAllClassLoaderJarUrls(((URLClassLoader) classLoader).getURLs(),
174-
result);
180+
try {
181+
addAllClassLoaderJarUrls(
182+
((URLClassLoader) classLoader).getURLs(), result);
183+
}
184+
catch (Exception ex) {
185+
if (logger.isDebugEnabled()) {
186+
logger.debug("Cannot introspect jar files since "
187+
+ "ClassLoader [" + classLoader
188+
+ "] does not support 'getURLs()': " + ex);
189+
}
190+
}
191+
}
192+
try {
193+
addAllClassLoaderJarRoots(classLoader.getParent(), result);
194+
}
195+
catch (Exception ex) {
196+
if (logger.isDebugEnabled()) {
197+
logger.debug("Cannot introspect jar files in parent "
198+
+ "ClassLoader since [" + classLoader
199+
+ "] does not support 'getParent()': " + ex);
200+
}
175201
}
176-
addAllClassLoaderJarUrls(classLoader.getParent(), result);
177202
}
178203
}
179204

180205
private void addAllClassLoaderJarUrls(URL[] urls, Set<Resource> result) {
181206
for (URL url : urls) {
182-
if ("file".equals(url.getProtocol())
183-
&& url.toString().toLowerCase().endsWith(".jar")) {
207+
if (isJarFileUrl(url)) {
184208
try {
185-
URL jarUrl = new URL("jar:" + url.toString() + "!/");
186-
jarUrl.openConnection();
187-
result.add(convertClassLoaderURL(jarUrl));
209+
UrlResource jarResource = new UrlResource(JAR_URL_PREFIX
210+
+ url.toString() + ResourceUtils.JAR_URL_SEPARATOR);
211+
if (jarResource.exists()) {
212+
result.add(jarResource);
213+
}
188214
}
189-
catch (Exception ex) {
190-
if (logger.isWarnEnabled()) {
191-
logger.warn("Cannot search for matching files underneath "
215+
catch (MalformedURLException ex) {
216+
if (logger.isDebugEnabled()) {
217+
logger.debug("Cannot search for matching files underneath "
192218
+ url + " because it cannot be accessed as a JAR", ex);
193219
}
194220
}
195221
}
196222
}
197223
}
198224

225+
private boolean isJarFileUrl(URL url) {
226+
return ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())
227+
&& url.getPath().toLowerCase().endsWith(JAR_FILE_EXTENSION);
228+
}
229+
199230
}
200231

201232
}

0 commit comments

Comments
 (0)