From e520c47c4f8a80b491849679e0deae3509f02805 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 21 Jan 2016 15:05:53 -0800 Subject: [PATCH] Refine `messages.properties` detection 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 --- .../MessageSourceAutoConfiguration.java | 73 +------------------ 1 file changed, 3 insertions(+), 70 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java index 32b315d4e707..8cbd87fc88f4 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java @@ -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. @@ -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; @@ -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; @@ -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 result) { - if (classLoader != ROOT_CLASSLOADER) { - super.addAllClassLoaderJarRoots(classLoader, result); - } - } - - @Override - protected Set doFindAllClassPathResources(String path) - throws IOException { - Set resources = super.doFindAllClassPathResources(path); - for (Iterator iterator = resources.iterator(); iterator - .hasNext();) { - Resource resource = iterator.next(); - for (String skipped : SKIPPED) { - if (resource.getFilename().startsWith(skipped)) { - iterator.remove(); - break; - } - } - } - return resources; - } - - } - }