Don't check for /templates when using groovy-all

Update GroovyTemplateAutoConfiguration so that the `/template`
folder check only occurs when the groovy-all jar is not being
used.

Fixes gh-2190
See gh-1915
This commit is contained in:
Phillip Webb 2014-12-17 21:34:50 -08:00
parent 6ad23b1cc4
commit 7e7acea548
1 changed files with 26 additions and 1 deletions

View File

@ -18,6 +18,9 @@ package org.springframework.boot.autoconfigure.groovy.template;
import groovy.text.markup.MarkupTemplateEngine;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import javax.annotation.PostConstruct;
import javax.servlet.Servlet;
@ -75,7 +78,7 @@ public class GroovyTemplateAutoConfiguration {
@PostConstruct
public void checkTemplateLocationExists() {
if (this.properties.isCheckTemplateLocation()) {
if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) {
Resource resource = this.resourceLoader.getResource(this.properties
.getPrefix());
Assert.state(resource.exists(), "Cannot find template location: "
@ -85,6 +88,28 @@ public class GroovyTemplateAutoConfiguration {
}
}
/**
* MarkupTemplateEngine could be loaded from groovy-templates or groovy-all.
* Unfortunately it's quite common for people to use groovy-all and not actually
* need templating support. This method checks attempts to check the source jar so
* that we can skip the {@code /template} folder check for such cases.
* @return true if the groovy-all jar is used
*/
private boolean isUsingGroovyAllJar() {
try {
ProtectionDomain domain = MarkupTemplateEngine.class
.getProtectionDomain();
CodeSource codeSource = domain.getCodeSource();
if (codeSource != null
&& codeSource.getLocation().toString().contains("-all")) {
return true;
}
}
catch (Exception ex) {
}
return false;
}
@Bean
@ConditionalOnMissingBean(GroovyMarkupConfig.class)
@ConfigurationProperties(prefix = "spring.groovy.template.configuration")