001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.mustache; 018 019import javax.annotation.PostConstruct; 020 021import com.samskivert.mustache.Mustache; 022import com.samskivert.mustache.Mustache.Collector; 023import com.samskivert.mustache.Mustache.TemplateLoader; 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026 027import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 028import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 029import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 030import org.springframework.boot.autoconfigure.template.TemplateLocation; 031import org.springframework.boot.context.properties.EnableConfigurationProperties; 032import org.springframework.context.ApplicationContext; 033import org.springframework.context.annotation.Bean; 034import org.springframework.context.annotation.Configuration; 035import org.springframework.context.annotation.Import; 036import org.springframework.core.env.Environment; 037 038/** 039 * {@link EnableAutoConfiguration Auto-configuration} for Mustache. 040 * 041 * @author Dave Syer 042 * @author Brian Clozel 043 * @since 1.2.2 044 */ 045@Configuration 046@ConditionalOnClass(Mustache.class) 047@EnableConfigurationProperties(MustacheProperties.class) 048@Import({ MustacheServletWebConfiguration.class, MustacheReactiveWebConfiguration.class }) 049public class MustacheAutoConfiguration { 050 051 private static final Log logger = LogFactory.getLog(MustacheAutoConfiguration.class); 052 053 private final MustacheProperties mustache; 054 055 private final Environment environment; 056 057 private final ApplicationContext applicationContext; 058 059 public MustacheAutoConfiguration(MustacheProperties mustache, Environment environment, 060 ApplicationContext applicationContext) { 061 this.mustache = mustache; 062 this.environment = environment; 063 this.applicationContext = applicationContext; 064 } 065 066 @PostConstruct 067 public void checkTemplateLocationExists() { 068 if (this.mustache.isCheckTemplateLocation()) { 069 TemplateLocation location = new TemplateLocation(this.mustache.getPrefix()); 070 if (!location.exists(this.applicationContext)) { 071 logger.warn("Cannot find template location: " + location 072 + " (please add some templates, check your Mustache " 073 + "configuration, or set spring.mustache." 074 + "check-template-location=false)"); 075 } 076 } 077 } 078 079 @Bean 080 @ConditionalOnMissingBean 081 public Mustache.Compiler mustacheCompiler(TemplateLoader mustacheTemplateLoader) { 082 return Mustache.compiler().withLoader(mustacheTemplateLoader) 083 .withCollector(collector()); 084 } 085 086 private Collector collector() { 087 MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector(); 088 collector.setEnvironment(this.environment); 089 return collector; 090 } 091 092 @Bean 093 @ConditionalOnMissingBean(TemplateLoader.class) 094 public MustacheResourceTemplateLoader mustacheTemplateLoader() { 095 MustacheResourceTemplateLoader loader = new MustacheResourceTemplateLoader( 096 this.mustache.getPrefix(), this.mustache.getSuffix()); 097 loader.setCharset(this.mustache.getCharsetName()); 098 return loader; 099 } 100 101}