Skip to content

Commit eb721b1

Browse files
committed
Find 'messages*.properties' in all jar URLs
Update the the PathMatchingResourcePatternResolver used in the MessageSourceAutoConfiguration condition to deal with the fact that `classpath*:` patterns do not work with URLClassLoaders when the pattern doesn't include a folder. The ExtendedPathMatchingResourcePatternResolver works by searching all classpath URLs when the `findAllClassPathResources` method is called with an empty location. Fixes gh-1378
1 parent 6e6bc25 commit eb721b1

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

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

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

1919
import java.io.IOException;
20-
20+
import java.net.URL;
21+
import java.net.URLClassLoader;
22+
import java.util.Arrays;
23+
import java.util.LinkedHashSet;
24+
import java.util.Set;
25+
26+
import org.apache.commons.logging.Log;
27+
import org.apache.commons.logging.LogFactory;
2128
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
2229
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
2330
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -118,7 +125,7 @@ public ConditionOutcome getMatchOutcome(ConditionContext context,
118125

119126
private Resource[] getResources(ClassLoader classLoader, String name) {
120127
try {
121-
return new PathMatchingResourcePatternResolver(classLoader)
128+
return new ExtendedPathMatchingResourcePatternResolver(classLoader)
122129
.getResources("classpath*:" + name + "*.properties");
123130
}
124131
catch (IOException ex) {
@@ -128,4 +135,67 @@ private Resource[] getResources(ClassLoader classLoader, String name) {
128135

129136
}
130137

138+
/**
139+
* Extended version of {@link PathMatchingResourcePatternResolver} to deal with the
140+
* fact that "{@code classpath*:...*.properties}" patterns don't work with
141+
* {@link URLClassLoader}s.
142+
*/
143+
private static class ExtendedPathMatchingResourcePatternResolver extends
144+
PathMatchingResourcePatternResolver {
145+
146+
private static final Log logger = LogFactory
147+
.getLog(PathMatchingResourcePatternResolver.class);
148+
149+
public ExtendedPathMatchingResourcePatternResolver(ClassLoader classLoader) {
150+
super(classLoader);
151+
}
152+
153+
@Override
154+
protected Resource[] findAllClassPathResources(String location)
155+
throws IOException {
156+
String path = location;
157+
if (path.startsWith("/")) {
158+
path = path.substring(1);
159+
}
160+
if ("".equals(path)) {
161+
Set<Resource> result = new LinkedHashSet<Resource>(16);
162+
result.addAll(Arrays.asList(super.findAllClassPathResources(location)));
163+
addAllClassLoaderJarUrls(getClassLoader(), result);
164+
return result.toArray(new Resource[result.size()]);
165+
}
166+
return super.findAllClassPathResources(location);
167+
}
168+
169+
private void addAllClassLoaderJarUrls(ClassLoader classLoader,
170+
Set<Resource> result) {
171+
if (classLoader != null) {
172+
if (classLoader instanceof URLClassLoader) {
173+
addAllClassLoaderJarUrls(((URLClassLoader) classLoader).getURLs(),
174+
result);
175+
}
176+
addAllClassLoaderJarUrls(classLoader.getParent(), result);
177+
}
178+
}
179+
180+
private void addAllClassLoaderJarUrls(URL[] urls, Set<Resource> result) {
181+
for (URL url : urls) {
182+
if ("file".equals(url.getProtocol())
183+
&& url.toString().toLowerCase().endsWith(".jar")) {
184+
try {
185+
URL jarUrl = new URL("jar:" + url.toString() + "!/");
186+
jarUrl.openConnection();
187+
result.add(convertClassLoaderURL(jarUrl));
188+
}
189+
catch (Exception ex) {
190+
if (logger.isWarnEnabled()) {
191+
logger.warn("Cannot search for matching files underneath "
192+
+ url + " because it cannot be accessed as a JAR", ex);
193+
}
194+
}
195+
}
196+
}
197+
}
198+
199+
}
200+
131201
}

0 commit comments

Comments
 (0)