Skip to content

Commit

Permalink
Refine messages.properties detection
Browse files Browse the repository at this point in the history
Update `ResourceBundleCondition` to only enable the messages source
if `messages.properties` (and not `messages*.properties`) exists. This
operation is much faster that performing a pattern match since a full
jar entry scan is not required.

Since adding `messages.properties` is good practice and this change
allows us to delete the custom `PathMatchingResourcePatternResolver`
it seems like a fine compromise to make.

Fixes gh-4930
See gh-4811
  • Loading branch information
philwebb committed Jan 21, 2016
1 parent cf93f84 commit e520c47
Showing 1 changed file with 3 additions and 70 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,7 @@

package org.springframework.boot.autoconfigure;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
Expand Down Expand Up @@ -164,8 +161,8 @@ private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context,

private Resource[] getResources(ClassLoader classLoader, String name) {
try {
return new SkipPatternPathMatchingResourcePatternResolver(classLoader)
.getResources("classpath*:" + name + "*.properties");
return new PathMatchingResourcePatternResolver(classLoader)
.getResources("classpath*:" + name + ".properties");
}
catch (Exception ex) {
return NO_RESOURCES;
Expand All @@ -174,68 +171,4 @@ private Resource[] getResources(ClassLoader classLoader, String name) {

}

/**
* {@link PathMatchingResourcePatternResolver} that skips well known JARs that don't
* contain messages.properties.
*/
private static class SkipPatternPathMatchingResourcePatternResolver
extends PathMatchingResourcePatternResolver {

private static final ClassLoader ROOT_CLASSLOADER;

static {
ClassLoader classLoader = null;
try {
classLoader = ClassLoader.getSystemClassLoader();
while (classLoader.getParent() != null) {
classLoader = classLoader.getParent();
}
}
catch (Throwable ex) {
// Ignore
}
ROOT_CLASSLOADER = classLoader;
}

private static final String[] SKIPPED = { "aspectjweaver-", "hibernate-core-",
"hsqldb-", "jackson-annotations-", "jackson-core-", "jackson-databind-",
"javassist-", "snakeyaml-", "spring-aop-", "spring-beans-",
"spring-boot-", "spring-boot-actuator-", "spring-boot-autoconfigure-",
"spring-core-", "spring-context-", "spring-data-commons-",
"spring-expression-", "spring-jdbc-", "spring-orm-", "spring-tx-",
"spring-web-", "spring-webmvc-", "tomcat-embed-", "joda-time-",
"hibernate-entitymanager-", "hibernate-validator-", "logback-classic-",
"logback-core-", "thymeleaf-" };

SkipPatternPathMatchingResourcePatternResolver(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected void addAllClassLoaderJarRoots(ClassLoader classLoader,
Set<Resource> result) {
if (classLoader != ROOT_CLASSLOADER) {
super.addAllClassLoaderJarRoots(classLoader, result);
}
}

@Override
protected Set<Resource> doFindAllClassPathResources(String path)
throws IOException {
Set<Resource> resources = super.doFindAllClassPathResources(path);
for (Iterator<Resource> iterator = resources.iterator(); iterator
.hasNext();) {
Resource resource = iterator.next();
for (String skipped : SKIPPED) {
if (resource.getFilename().startsWith(skipped)) {
iterator.remove();
break;
}
}
}
return resources;
}

}

}

0 comments on commit e520c47

Please sign in to comment.