Commit 7787b2ef authored by Dave Syer's avatar Dave Syer

Make Groovy templates work with Groovy 2.2

Fixes gh-890
parent e964b9eb
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.groovy.template; package org.springframework.boot.autoconfigure.groovy.template;
import groovy.text.SimpleTemplateEngine;
import groovy.text.TemplateEngine; import groovy.text.TemplateEngine;
import groovy.text.markup.MarkupTemplateEngine; import groovy.text.markup.MarkupTemplateEngine;
import groovy.text.markup.TemplateConfiguration; import groovy.text.markup.TemplateConfiguration;
...@@ -27,15 +28,16 @@ import java.util.List; ...@@ -27,15 +28,16 @@ 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;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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.ConditionalOnMissingClass;
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.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -66,16 +68,14 @@ public class GroovyTemplateAutoConfiguration { ...@@ -66,16 +68,14 @@ public class GroovyTemplateAutoConfiguration {
@Autowired @Autowired
private GroovyTemplateProperties properties; private GroovyTemplateProperties properties;
@Configuration public abstract static class BaseGroovyTemplateConfiguration implements
@ConditionalOnClass({ Servlet.class, LocaleContextHolder.class }) BeanClassLoaderAware {
@ConditionalOnWebApplication
public static class GroovyWebConfiguration implements BeanClassLoaderAware {
@Autowired @Autowired
private ApplicationContext resourceLoader; private GroovyTemplateProperties properties;
@Autowired @Autowired
private GroovyTemplateProperties properties; private ApplicationContext resourceLoader;
private ClassLoader classLoader = GroovyWebConfiguration.class.getClassLoader(); private ClassLoader classLoader = GroovyWebConfiguration.class.getClassLoader();
...@@ -84,16 +84,7 @@ public class GroovyTemplateAutoConfiguration { ...@@ -84,16 +84,7 @@ public class GroovyTemplateAutoConfiguration {
this.classLoader = classLoader; this.classLoader = classLoader;
} }
@Bean protected ClassLoader createParentLoaderForTemplates() throws Exception {
@ConditionalOnMissingBean(TemplateEngine.class)
public TemplateEngine groovyTemplateEngine() throws Exception {
TemplateConfiguration configuration = this.properties.getConfiguration();
return new MarkupTemplateEngine(createParentLoaderForTemplates(),
configuration, new GroovyTemplateResolver());
}
private ClassLoader createParentLoaderForTemplates() throws Exception {
Resource[] resources = this.resourceLoader.getResources(this.properties Resource[] resources = this.resourceLoader.getResources(this.properties
.getPrefix()); .getPrefix());
if (resources.length > 0) { if (resources.length > 0) {
...@@ -111,6 +102,55 @@ public class GroovyTemplateAutoConfiguration { ...@@ -111,6 +102,55 @@ public class GroovyTemplateAutoConfiguration {
} }
} }
}
@Configuration
@ConditionalOnClass(MarkupTemplateEngine.class)
public static class GroovyMarkupConfiguration extends BaseGroovyTemplateConfiguration {
@Autowired
private GroovyTemplateProperties properties;
@Bean
@ConfigurationProperties(prefix = "spring.groovy.template.configuration")
public TemplateConfiguration groovyTemplateConfiguration() {
return new TemplateConfiguration();
}
@Bean
@ConditionalOnMissingBean(TemplateEngine.class)
public TemplateEngine groovyTemplateEngine() throws Exception {
TemplateConfiguration configuration = groovyTemplateConfiguration();
configuration.setCacheTemplates(this.properties.isCache());
return new MarkupTemplateEngine(createParentLoaderForTemplates(),
configuration, new GroovyTemplateResolver());
}
}
@Configuration
@ConditionalOnMissingClass(name = "groovy.text.markup.MarkupTemplateEngine")
public static class GroovySimpleConfiguration extends BaseGroovyTemplateConfiguration {
@Autowired
private GroovyTemplateProperties properties;
@Bean
@ConditionalOnMissingBean(TemplateEngine.class)
public TemplateEngine groovyTemplateEngine() throws Exception {
return new SimpleTemplateEngine(createParentLoaderForTemplates());
}
}
@Configuration
@ConditionalOnClass({ Servlet.class, LocaleContextHolder.class })
@ConditionalOnWebApplication
public static class GroovyWebConfiguration {
@Autowired
private GroovyTemplateProperties properties;
@Bean @Bean
@ConditionalOnMissingBean(name = "groovyTemplateViewResolver") @ConditionalOnMissingBean(name = "groovyTemplateViewResolver")
public GroovyTemplateViewResolver groovyTemplateViewResolver(TemplateEngine engine) { public GroovyTemplateViewResolver groovyTemplateViewResolver(TemplateEngine engine) {
...@@ -129,6 +169,6 @@ public class GroovyTemplateAutoConfiguration { ...@@ -129,6 +169,6 @@ public class GroovyTemplateAutoConfiguration {
return resolver; return resolver;
} }
} }
} }
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
package org.springframework.boot.autoconfigure.groovy.template; package org.springframework.boot.autoconfigure.groovy.template;
import groovy.text.markup.TemplateConfiguration; import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
...@@ -45,7 +46,7 @@ public class GroovyTemplateProperties { ...@@ -45,7 +46,7 @@ public class GroovyTemplateProperties {
private boolean checkTemplateLocation = false; private boolean checkTemplateLocation = false;
private TemplateConfiguration configuration = new TemplateConfiguration(); private Map<String, Object> configuration = new HashMap<String, Object>();
public void setCheckTemplateLocation(boolean checkTemplateLocation) { public void setCheckTemplateLocation(boolean checkTemplateLocation) {
this.checkTemplateLocation = checkTemplateLocation; this.checkTemplateLocation = checkTemplateLocation;
...@@ -105,11 +106,11 @@ public class GroovyTemplateProperties { ...@@ -105,11 +106,11 @@ public class GroovyTemplateProperties {
this.suffix = suffix; this.suffix = suffix;
} }
public void setConfiguration(TemplateConfiguration configuration) { public void setConfiguration(Map<String, Object> configuration) {
this.configuration = configuration; this.configuration = configuration;
} }
public TemplateConfiguration getConfiguration() { public Map<String, Object> getConfiguration() {
return this.configuration; return this.configuration;
} }
......
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