Commit 0f3b9596 authored by Phillip Webb's avatar Phillip Webb

Scan full classpath with /templates checks

Update template auto configurations to search for template folders in
the entire classpath rather than just the root jar.

Fixes gh-2184
parent 7e7acea5
...@@ -31,13 +31,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -31,13 +31,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.template.TemplateLocation;
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.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean; import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -60,7 +59,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; ...@@ -60,7 +59,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
public class FreeMarkerAutoConfiguration { public class FreeMarkerAutoConfiguration {
@Autowired @Autowired
private final ResourceLoader resourceLoader = new DefaultResourceLoader(); private ApplicationContext applicationContext;
@Autowired @Autowired
private FreeMarkerProperties properties; private FreeMarkerProperties properties;
...@@ -68,18 +67,18 @@ public class FreeMarkerAutoConfiguration { ...@@ -68,18 +67,18 @@ public class FreeMarkerAutoConfiguration {
@PostConstruct @PostConstruct
public void checkTemplateLocationExists() { public void checkTemplateLocationExists() {
if (this.properties.isCheckTemplateLocation()) { if (this.properties.isCheckTemplateLocation()) {
Resource templatePathResource = null; TemplateLocation templatePathLocation = null;
List<Resource> resources = new ArrayList<Resource>(); List<TemplateLocation> locations = new ArrayList<TemplateLocation>();
for (String templateLoaderPath : this.properties.getTemplateLoaderPath()) { for (String templateLoaderPath : this.properties.getTemplateLoaderPath()) {
Resource resource = this.resourceLoader.getResource(templateLoaderPath); TemplateLocation location = new TemplateLocation(templateLoaderPath);
resources.add(resource); locations.add(location);
if (resource.exists()) { if (location.exists(this.applicationContext)) {
templatePathResource = resource; templatePathLocation = location;
break; break;
} }
} }
Assert.notNull(templatePathResource, "Cannot find template location(s): " Assert.notNull(templatePathLocation, "Cannot find template location(s): "
+ resources + " (please add some templates, " + locations + " (please add some templates, "
+ "check your FreeMarker configuration, or set " + "check your FreeMarker configuration, or set "
+ "spring.freemarker.checkTemplateLocation=false)"); + "spring.freemarker.checkTemplateLocation=false)");
} }
......
...@@ -30,16 +30,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; ...@@ -30,16 +30,15 @@ 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.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.template.TemplateLocation;
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.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.servlet.view.UrlBasedViewResolver; import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfig; import org.springframework.web.servlet.view.groovy.GroovyMarkupConfig;
...@@ -68,7 +67,7 @@ public class GroovyTemplateAutoConfiguration { ...@@ -68,7 +67,7 @@ public class GroovyTemplateAutoConfiguration {
public static class GroovyMarkupConfiguration { public static class GroovyMarkupConfiguration {
@Autowired @Autowired
private final ResourceLoader resourceLoader = new DefaultResourceLoader(); private ApplicationContext applicationContext;
@Autowired @Autowired
private GroovyTemplateProperties properties; private GroovyTemplateProperties properties;
...@@ -79,12 +78,13 @@ public class GroovyTemplateAutoConfiguration { ...@@ -79,12 +78,13 @@ public class GroovyTemplateAutoConfiguration {
@PostConstruct @PostConstruct
public void checkTemplateLocationExists() { public void checkTemplateLocationExists() {
if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) { if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) {
Resource resource = this.resourceLoader.getResource(this.properties TemplateLocation location = new TemplateLocation(
.getPrefix()); this.properties.getPrefix());
Assert.state(resource.exists(), "Cannot find template location: " Assert.state(location.exists(this.applicationContext),
+ resource + " (please add some templates, " "Cannot find template location: " + location
+ "check your Groovy configuration, or set " + " (please add some templates, check your Groovy "
+ "spring.groovy.template.check-template-location=false)"); + "configuration, or set spring.groovy.template."
+ "check-template-location=false)");
} }
} }
......
/*
* Copyright 2012-2014 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.template;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.Assert;
/**
* Contains a location that templates can be loaded from.
*
* @author Phillip Webb
* @since 1.2.1
*/
public class TemplateLocation {
private final String path;
public TemplateLocation(String path) {
Assert.notNull(path, "Path must not be null");
this.path = path;
}
/**
* Determine if this template location exists using the specified
* {@link ResourcePatternResolver}.
* @param resolver the resolver used to test if the location exists
* @return {@code true} if the location exists.
*/
public boolean exists(ResourcePatternResolver resolver) {
Assert.notNull(resolver, "Resolver must not be null");
if (resolver.getResource(this.path).exists()) {
return true;
}
try {
if (anyExists(resolver)) {
return true;
}
}
catch (IOException ex) {
}
return false;
}
private boolean anyExists(ResourcePatternResolver resolver) throws IOException {
String searchPath = this.path;
if (searchPath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)) {
searchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ searchPath.substring(ResourceLoader.CLASSPATH_URL_PREFIX.length());
}
if (this.path.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX)) {
Resource[] resources = resolver.getResources(this.path);
for (Resource resource : resources) {
if (resource.exists()) {
return true;
}
}
}
return false;
}
@Override
public String toString() {
return this.path;
}
}
...@@ -31,14 +31,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; ...@@ -31,14 +31,13 @@ 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.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.template.TemplateLocation;
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.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.thymeleaf.dialect.IDialect; import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect; import org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect;
...@@ -72,17 +71,18 @@ public class ThymeleafAutoConfiguration { ...@@ -72,17 +71,18 @@ public class ThymeleafAutoConfiguration {
private ThymeleafProperties properties; private ThymeleafProperties properties;
@Autowired @Autowired
private final ResourceLoader resourceLoader = new DefaultResourceLoader(); private ApplicationContext applicationContext;
@PostConstruct @PostConstruct
public void checkTemplateLocationExists() { public void checkTemplateLocationExists() {
Boolean checkTemplateLocation = this.properties.isCheckTemplateLocation(); boolean checkTemplateLocation = this.properties.isCheckTemplateLocation();
if (checkTemplateLocation) { if (checkTemplateLocation) {
Resource resource = this.resourceLoader.getResource(this.properties TemplateLocation location = new TemplateLocation(
.getPrefix()); this.properties.getPrefix());
Assert.state(resource.exists(), "Cannot find template location: " Assert.state(location.exists(this.applicationContext),
+ resource + " (please add some templates " "Cannot find template location: " + location
+ "or check your Thymeleaf configuration)"); + " (please add some templates or check "
+ "your Thymeleaf configuration)");
} }
} }
......
...@@ -32,13 +32,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -32,13 +32,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.template.TemplateLocation;
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.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.ui.velocity.VelocityEngineFactory; import org.springframework.ui.velocity.VelocityEngineFactory;
import org.springframework.ui.velocity.VelocityEngineFactoryBean; import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -59,7 +58,7 @@ import org.springframework.web.servlet.view.velocity.VelocityViewResolver; ...@@ -59,7 +58,7 @@ import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
public class VelocityAutoConfiguration { public class VelocityAutoConfiguration {
@Autowired @Autowired
private final ResourceLoader resourceLoader = new DefaultResourceLoader(); private ApplicationContext applicationContext;
@Autowired @Autowired
private VelocityProperties properties; private VelocityProperties properties;
...@@ -67,11 +66,13 @@ public class VelocityAutoConfiguration { ...@@ -67,11 +66,13 @@ public class VelocityAutoConfiguration {
@PostConstruct @PostConstruct
public void checkTemplateLocationExists() { public void checkTemplateLocationExists() {
if (this.properties.isCheckTemplateLocation()) { if (this.properties.isCheckTemplateLocation()) {
Resource resource = this.resourceLoader.getResource(this.properties TemplateLocation location = new TemplateLocation(
.getResourceLoaderPath()); this.properties.getResourceLoaderPath());
Assert.state(resource.exists(), "Cannot find template location: " + resource Assert.state(location.exists(this.applicationContext),
+ " (please add some templates, check your Velocity configuration, " "Cannot find template location: " + location
+ "or set spring.velocity.checkTemplateLocation=false)"); + " (please add some templates, check your Velocity "
+ "configuration, or set spring.velocity."
+ "checkTemplateLocation=false)");
} }
} }
......
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