Align MessageSource path search fix with SPR-12095

Update ExtendedPathMatchingResourcePatternResolver to use similar
code as the fix for SPR-12095.

Fixes gh-1378
pull/1487/merge
Phillip Webb 10 years ago
parent 9717b7d840
commit 650e326ae7

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
@ -40,8 +41,10 @@ import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
@ -146,6 +149,10 @@ public class MessageSourceAutoConfiguration {
private static final Log logger = LogFactory
.getLog(PathMatchingResourcePatternResolver.class);
private static final String JAR_FILE_EXTENSION = ".jar";
private static final String JAR_URL_PREFIX = "jar:";
public ExtendedPathMatchingResourcePatternResolver(ClassLoader classLoader) {
super(classLoader);
}
@ -160,35 +167,54 @@ public class MessageSourceAutoConfiguration {
if ("".equals(path)) {
Set<Resource> result = new LinkedHashSet<Resource>(16);
result.addAll(Arrays.asList(super.findAllClassPathResources(location)));
addAllClassLoaderJarUrls(getClassLoader(), result);
addAllClassLoaderJarRoots(getClassLoader(), result);
return result.toArray(new Resource[result.size()]);
}
return super.findAllClassPathResources(location);
}
private void addAllClassLoaderJarUrls(ClassLoader classLoader,
private void addAllClassLoaderJarRoots(ClassLoader classLoader,
Set<Resource> result) {
if (classLoader != null) {
if (classLoader instanceof URLClassLoader) {
addAllClassLoaderJarUrls(((URLClassLoader) classLoader).getURLs(),
result);
try {
addAllClassLoaderJarUrls(
((URLClassLoader) classLoader).getURLs(), result);
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Cannot introspect jar files since "
+ "ClassLoader [" + classLoader
+ "] does not support 'getURLs()': " + ex);
}
}
}
try {
addAllClassLoaderJarRoots(classLoader.getParent(), result);
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Cannot introspect jar files in parent "
+ "ClassLoader since [" + classLoader
+ "] does not support 'getParent()': " + ex);
}
}
addAllClassLoaderJarUrls(classLoader.getParent(), result);
}
}
private void addAllClassLoaderJarUrls(URL[] urls, Set<Resource> result) {
for (URL url : urls) {
if ("file".equals(url.getProtocol())
&& url.toString().toLowerCase().endsWith(".jar")) {
if (isJarFileUrl(url)) {
try {
URL jarUrl = new URL("jar:" + url.toString() + "!/");
jarUrl.openConnection();
result.add(convertClassLoaderURL(jarUrl));
UrlResource jarResource = new UrlResource(JAR_URL_PREFIX
+ url.toString() + ResourceUtils.JAR_URL_SEPARATOR);
if (jarResource.exists()) {
result.add(jarResource);
}
}
catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn("Cannot search for matching files underneath "
catch (MalformedURLException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Cannot search for matching files underneath "
+ url + " because it cannot be accessed as a JAR", ex);
}
}
@ -196,6 +222,11 @@ public class MessageSourceAutoConfiguration {
}
}
private boolean isJarFileUrl(URL url) {
return ResourceUtils.URL_PROTOCOL_FILE.equals(url.getProtocol())
&& url.getPath().toLowerCase().endsWith(JAR_FILE_EXTENSION);
}
}
}

Loading…
Cancel
Save