diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobExecutionEvent.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobExecutionEvent.java index 9e704fb810..1cddad4485 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobExecutionEvent.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobExecutionEvent.java @@ -24,6 +24,7 @@ import org.springframework.context.ApplicationEvent; * * @author Dave Syer */ +@SuppressWarnings("serial") public class JobExecutionEvent extends ApplicationEvent { private final JobExecution execution; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java index abf420333b..2223abb108 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java @@ -17,7 +17,6 @@ package org.springframework.boot.autoconfigure.groovy.template; import groovy.text.TemplateEngine; -import groovy.text.markup.BaseTemplate; import groovy.text.markup.MarkupTemplateEngine; import groovy.text.markup.TemplateConfiguration; @@ -28,6 +27,7 @@ import java.util.List; import javax.servlet.Servlet; +import groovy.text.markup.TemplateResolver; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; @@ -35,7 +35,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.groovy.template.web.GroovyTemplateViewResolver; -import org.springframework.boot.autoconfigure.groovy.template.web.LocaleAwareTemplate; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; @@ -89,12 +88,9 @@ public class GroovyTemplateAutoConfiguration { @ConditionalOnMissingBean(TemplateEngine.class) public TemplateEngine groovyTemplateEngine() throws Exception { TemplateConfiguration configuration = this.properties.getConfiguration(); - if (configuration.getBaseTemplateClass() == BaseTemplate.class) { - // Enable locale-dependent includes - configuration.setBaseTemplateClass(LocaleAwareTemplate.class); - } - return new MarkupTemplateEngine(createParentLoaderForTemplates(), - configuration); + + return new MarkupTemplateEngine(createParentLoaderForTemplates(), + configuration, new GroovyTemplateResolver()); } private ClassLoader createParentLoaderForTemplates() throws Exception { @@ -132,6 +128,7 @@ public class GroovyTemplateAutoConfiguration { return resolver; } - } + + } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateResolver.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateResolver.java new file mode 100644 index 0000000000..d1bfa0da6f --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateResolver.java @@ -0,0 +1,55 @@ +/* + * Copyright 2003-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.boot.autoconfigure.groovy.template; + +import groovy.text.markup.MarkupTemplateEngine; +import groovy.text.markup.TemplateConfiguration; +import groovy.text.markup.TemplateResolver; +import org.springframework.context.i18n.LocaleContextHolder; + +import java.io.IOException; +import java.net.URL; + +/** + * A custom {@link groovy.text.markup.TemplateResolver template resolver} which resolves templates using the locale + * found in the thread locale. This resolver ignores the template engine configuration locale. + * + * @author Cédric Champeau + * @since 1.1.0 + */ + +public class GroovyTemplateResolver implements TemplateResolver { + private ClassLoader templateClassLoader; + + @Override + public void configure(final ClassLoader templateClassLoader, final TemplateConfiguration configuration) { + this.templateClassLoader = templateClassLoader; + } + + @Override + public URL resolveTemplate(final String templatePath) throws IOException { + MarkupTemplateEngine.TemplateResource templateResource = MarkupTemplateEngine.TemplateResource.parse(templatePath); + URL resource = templateClassLoader.getResource(templateResource.withLocale(LocaleContextHolder.getLocale().toString().replace("-", "_")).toString()); + if (resource == null) { + // no resource found with the default locale, try without any locale + resource = templateClassLoader.getResource(templateResource.withLocale(null).toString()); + } + if (resource == null) { + throw new IOException("Unable to load template:" + templatePath); + } + return resource; + } +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/LocaleAwareTemplate.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/LocaleAwareTemplate.java deleted file mode 100644 index 8a5d1e739e..0000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/web/LocaleAwareTemplate.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2012-2013 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.groovy.template.web; - -import groovy.text.markup.BaseTemplate; -import groovy.text.markup.MarkupTemplateEngine; -import groovy.text.markup.TemplateConfiguration; - -import java.util.Map; - -import org.springframework.context.i18n.LocaleContextHolder; - -/** - * @author Dave Syer - */ -public abstract class LocaleAwareTemplate extends BaseTemplate { - - public LocaleAwareTemplate(MarkupTemplateEngine templateEngine, Map model, - Map modelTypes, TemplateConfiguration configuration) { - super(localize(templateEngine), model, modelTypes, localize(configuration)); - } - - private static MarkupTemplateEngine localize(MarkupTemplateEngine templateEngine) { - TemplateConfiguration templateConfiguration = templateEngine - .getTemplateConfiguration(); - ClassLoader parent = templateEngine.getTemplateLoader().getParent(); - return new MarkupTemplateEngine(parent, localize(templateConfiguration)); - } - - private static TemplateConfiguration localize(TemplateConfiguration configuration) { - TemplateConfiguration result = new TemplateConfiguration(configuration); - result.setLocale(LocaleContextHolder.getLocale()); - return result; - } - -} diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 2ab786d715..179ce8b30c 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -57,7 +57,7 @@ 2.3.20 7.0.2 1.6 - 2.3.0 + 2.3.1 1.3.175 1.3 4.3.1.Final