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.freemarker; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import javax.annotation.PostConstruct; 023 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.template.TemplateLocation; 030import org.springframework.boot.context.properties.EnableConfigurationProperties; 031import org.springframework.context.ApplicationContext; 032import org.springframework.context.annotation.Configuration; 033import org.springframework.context.annotation.Import; 034import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory; 035 036/** 037 * {@link EnableAutoConfiguration Auto-configuration} for FreeMarker. 038 * 039 * @author Andy Wilkinson 040 * @author Dave Syer 041 * @author Kazuki Shimizu 042 * @since 1.1.0 043 */ 044@Configuration 045@ConditionalOnClass({ freemarker.template.Configuration.class, 046 FreeMarkerConfigurationFactory.class }) 047@EnableConfigurationProperties(FreeMarkerProperties.class) 048@Import({ FreeMarkerServletWebConfiguration.class, 049 FreeMarkerReactiveWebConfiguration.class, FreeMarkerNonWebConfiguration.class }) 050public class FreeMarkerAutoConfiguration { 051 052 private static final Log logger = LogFactory 053 .getLog(FreeMarkerAutoConfiguration.class); 054 055 private final ApplicationContext applicationContext; 056 057 private final FreeMarkerProperties properties; 058 059 public FreeMarkerAutoConfiguration(ApplicationContext applicationContext, 060 FreeMarkerProperties properties) { 061 this.applicationContext = applicationContext; 062 this.properties = properties; 063 } 064 065 @PostConstruct 066 public void checkTemplateLocationExists() { 067 if (logger.isWarnEnabled() && this.properties.isCheckTemplateLocation()) { 068 List<TemplateLocation> locations = getLocations(); 069 if (locations.stream().noneMatch(this::locationExists)) { 070 logger.warn("Cannot find template location(s): " + locations 071 + " (please add some templates, " 072 + "check your FreeMarker configuration, or set " 073 + "spring.freemarker.checkTemplateLocation=false)"); 074 } 075 } 076 } 077 078 private List<TemplateLocation> getLocations() { 079 List<TemplateLocation> locations = new ArrayList<>(); 080 for (String templateLoaderPath : this.properties.getTemplateLoaderPath()) { 081 TemplateLocation location = new TemplateLocation(templateLoaderPath); 082 locations.add(location); 083 } 084 return locations; 085 } 086 087 private boolean locationExists(TemplateLocation location) { 088 return location.exists(this.applicationContext); 089 } 090 091}