Commit e964b9eb authored by Dave Syer's avatar Dave Syer

Improved integration with Groovy 2.3.1

Groovy 2.3.1 has a new template loader abstraction that handles
compiler caching (better performance by factor of 10).
parent 85ac8f6b
...@@ -24,6 +24,7 @@ import org.springframework.context.ApplicationEvent; ...@@ -24,6 +24,7 @@ import org.springframework.context.ApplicationEvent;
* *
* @author Dave Syer * @author Dave Syer
*/ */
@SuppressWarnings("serial")
public class JobExecutionEvent extends ApplicationEvent { public class JobExecutionEvent extends ApplicationEvent {
private final JobExecution execution; private final JobExecution execution;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.springframework.boot.autoconfigure.groovy.template; package org.springframework.boot.autoconfigure.groovy.template;
import groovy.text.TemplateEngine; import groovy.text.TemplateEngine;
import groovy.text.markup.BaseTemplate;
import groovy.text.markup.MarkupTemplateEngine; import groovy.text.markup.MarkupTemplateEngine;
import groovy.text.markup.TemplateConfiguration; import groovy.text.markup.TemplateConfiguration;
...@@ -28,6 +27,7 @@ import java.util.List; ...@@ -28,6 +27,7 @@ import java.util.List;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import groovy.text.markup.TemplateResolver;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
...@@ -35,7 +35,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; ...@@ -35,7 +35,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.groovy.template.web.GroovyTemplateViewResolver; 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.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
...@@ -89,12 +88,9 @@ public class GroovyTemplateAutoConfiguration { ...@@ -89,12 +88,9 @@ public class GroovyTemplateAutoConfiguration {
@ConditionalOnMissingBean(TemplateEngine.class) @ConditionalOnMissingBean(TemplateEngine.class)
public TemplateEngine groovyTemplateEngine() throws Exception { public TemplateEngine groovyTemplateEngine() throws Exception {
TemplateConfiguration configuration = this.properties.getConfiguration(); TemplateConfiguration configuration = this.properties.getConfiguration();
if (configuration.getBaseTemplateClass() == BaseTemplate.class) {
// Enable locale-dependent includes return new MarkupTemplateEngine(createParentLoaderForTemplates(),
configuration.setBaseTemplateClass(LocaleAwareTemplate.class); configuration, new GroovyTemplateResolver());
}
return new MarkupTemplateEngine(createParentLoaderForTemplates(),
configuration);
} }
private ClassLoader createParentLoaderForTemplates() throws Exception { private ClassLoader createParentLoaderForTemplates() throws Exception {
...@@ -132,6 +128,7 @@ public class GroovyTemplateAutoConfiguration { ...@@ -132,6 +128,7 @@ public class GroovyTemplateAutoConfiguration {
return resolver; return resolver;
} }
}
}
} }
/* /*
* Copyright 2012-2013 the original author or authors. * Copyright 2003-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
...@@ -13,38 +13,43 @@ ...@@ -13,38 +13,43 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.autoconfigure.groovy.template;
package org.springframework.boot.autoconfigure.groovy.template.web;
import groovy.text.markup.BaseTemplate;
import groovy.text.markup.MarkupTemplateEngine; import groovy.text.markup.MarkupTemplateEngine;
import groovy.text.markup.TemplateConfiguration; import groovy.text.markup.TemplateConfiguration;
import groovy.text.markup.TemplateResolver;
import java.util.Map;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import java.io.IOException;
import java.net.URL;
/** /**
* @author Dave Syer * 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 abstract class LocaleAwareTemplate extends BaseTemplate {
public LocaleAwareTemplate(MarkupTemplateEngine templateEngine, Map<?, ?> model,
Map<String, String> 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;
}
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;
}
} }
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
<freemarker.version>2.3.20</freemarker.version> <freemarker.version>2.3.20</freemarker.version>
<gemfire.version>7.0.2</gemfire.version> <gemfire.version>7.0.2</gemfire.version>
<gradle.version>1.6</gradle.version> <gradle.version>1.6</gradle.version>
<groovy.version>2.3.0</groovy.version> <groovy.version>2.3.1</groovy.version>
<h2.version>1.3.175</h2.version> <h2.version>1.3.175</h2.version>
<hamcrest.version>1.3</hamcrest.version> <hamcrest.version>1.3</hamcrest.version>
<hibernate.version>4.3.1.Final</hibernate.version> <hibernate.version>4.3.1.Final</hibernate.version>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment