|
| 1 | +/* |
| 2 | + * Copyright 2013-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.mustache; |
| 18 | + |
| 19 | +import javax.annotation.PostConstruct; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 23 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 24 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 25 | +import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver; |
| 26 | +import org.springframework.boot.autoconfigure.template.TemplateLocation; |
| 27 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 28 | +import org.springframework.context.ApplicationContext; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.core.Ordered; |
| 32 | +import org.springframework.core.env.Environment; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +import com.samskivert.mustache.Mustache; |
| 36 | +import com.samskivert.mustache.Mustache.Collector; |
| 37 | +import com.samskivert.mustache.Mustache.Compiler; |
| 38 | +import com.samskivert.mustache.Mustache.TemplateLoader; |
| 39 | + |
| 40 | +/** |
| 41 | + * @author Dave Syer |
| 42 | + * @since 1.2.2 |
| 43 | + * |
| 44 | + */ |
| 45 | +@Configuration |
| 46 | +@ConditionalOnClass(Mustache.class) |
| 47 | +@EnableConfigurationProperties(MustacheProperties.class) |
| 48 | +public class MustacheAutoConfiguration { |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private MustacheProperties mustache; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + private Environment environment; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private ApplicationContext applicationContext; |
| 58 | + |
| 59 | + @PostConstruct |
| 60 | + public void checkTemplateLocationExists() { |
| 61 | + if (this.mustache.isCheckTemplateLocation()) { |
| 62 | + TemplateLocation location = new TemplateLocation(this.mustache.getPrefix()); |
| 63 | + Assert.state(location.exists(this.applicationContext), |
| 64 | + "Cannot find template location: " + location |
| 65 | + + " (please add some templates, check your Mustache " |
| 66 | + + "configuration, or set spring.mustache.template." |
| 67 | + + "check-template-location=false)"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + @ConditionalOnMissingBean(Mustache.Compiler.class) |
| 73 | + public Mustache.Compiler mustacheCompiler(TemplateLoader mustacheTemplateLoader) { |
| 74 | + return Mustache.compiler().withLoader(mustacheTemplateLoader) |
| 75 | + .withCollector(collector()); |
| 76 | + } |
| 77 | + |
| 78 | + private Collector collector() { |
| 79 | + MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector(); |
| 80 | + collector.setEnvironment(this.environment); |
| 81 | + return collector; |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + @ConditionalOnMissingBean(TemplateLoader.class) |
| 86 | + public MustacheResourceTemplateLoader mustacheTemplateLoader() { |
| 87 | + MustacheResourceTemplateLoader loader = new MustacheResourceTemplateLoader( |
| 88 | + this.mustache.getPrefix(), this.mustache.getSuffix()); |
| 89 | + loader.setCharset(this.mustache.getCharset()); |
| 90 | + return loader; |
| 91 | + } |
| 92 | + |
| 93 | + @Configuration |
| 94 | + @ConditionalOnWebApplication |
| 95 | + protected static class MustacheWebConfiguration { |
| 96 | + |
| 97 | + @Autowired |
| 98 | + private MustacheProperties mustache; |
| 99 | + |
| 100 | + @Bean |
| 101 | + @ConditionalOnMissingBean(MustacheViewResolver.class) |
| 102 | + public MustacheViewResolver mustacheViewResolver(Compiler mustacheCompiler) { |
| 103 | + MustacheViewResolver resolver = new MustacheViewResolver(); |
| 104 | + resolver.setPrefix(this.mustache.getPrefix()); |
| 105 | + resolver.setSuffix(this.mustache.getSuffix()); |
| 106 | + resolver.setCache(this.mustache.isCache()); |
| 107 | + resolver.setViewNames(this.mustache.getViewNames()); |
| 108 | + resolver.setContentType(this.mustache.getContentType()); |
| 109 | + resolver.setCompiler(mustacheCompiler); |
| 110 | + resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10); |
| 111 | + return resolver; |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | +} |
0 commit comments