org.zodiac.autoconfigure.template.velocity.view.VelocityTemplateViewAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.template.velocity.view;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.template.TemplateLocation;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.zodiac.autoconfigure.template.velocity.VelocityTemplatePriperties;
import org.zodiac.autoconfigure.template.velocity.condition.ConditionalOnVelocityTemplateEnabled;
@SpringBootConfiguration
@ConditionalOnVelocityTemplateEnabled
@EnableConfigurationProperties(value = {VelocityTemplateViewProperties.class})
@ConditionalOnClass(value = {org.apache.velocity.app.VelocityEngine.class, org.zodiac.template.velocity.spring.ui.VelocityEngineFactory.class})
@ConditionalOnProperty(name = org.zodiac.template.velocity.constants.VelocityTemplateSystemPropertiesConstants.SPRING_TEMPLATE_VELOCITY_VIEW_ENABLED, havingValue = "true")
public class VelocityTemplateViewAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(VelocityTemplateViewAutoConfiguration.class);
private final ApplicationContext applicationContext;
private final VelocityTemplatePriperties velocityTemplatePriperties;
private final VelocityTemplateViewProperties velocityTemplateViewProperties;
public VelocityTemplateViewAutoConfiguration(ApplicationContext applicationContext,
ObjectProvider velocityTemplatePripertiesProvider,
VelocityTemplateViewProperties velocityTemplateViewProperties) {
this.applicationContext = applicationContext;
this.velocityTemplatePriperties = velocityTemplatePripertiesProvider.getIfAvailable();
this.velocityTemplateViewProperties = velocityTemplateViewProperties;
this.init();
}
// @Bean
// @ConfigurationProperties(prefix = org.zodiac.template.velocity.constants.VelocityTemplateSystemPropertiesConstants.SPRING_TEMPLATE_VELOCITY_VIEW_PREFIX, ignoreInvalidFields = true)
// protected VelocityTemplateViewProperties velocityViewProperties() {
// VelocityTemplateViewProperties viewProperties = new VelocityTemplateViewProperties();
// if (null != velocityTemplatePriperties) {
// if (Colls.notEmptyMap(velocityTemplatePriperties.getProperties()))
// velocityTemplatePriperties.getProperties().forEach((k, v) -> {
// viewProperties.getProperties().put(k, String.valueOf(v));
// });
//
// }
// return viewProperties;
// }
@PostConstruct
public void checkTemplateLocationExists() {
if (this.velocityTemplateViewProperties.isCheckTemplateLocation()) {
TemplateLocation location =
new TemplateLocation(this.velocityTemplateViewProperties.getResourceLoaderPath());
if (!location.exists(this.applicationContext)) {
logger.warn(
"Cannot find template location: {} (please add some templates, check your Velocity configuration, or set {}=false)",
location, org.zodiac.template.velocity.constants.VelocityTemplateSystemPropertiesConstants.SPRING_TEMPLATE_VELOCITY_VIEW_CHECK_TEMPLATE_LOCATION);
}
}
}
private void init() {
if (null != velocityTemplatePriperties) {
//if (null == velocityTemplateViewProperties.getResourceLoaderPath())
this.velocityTemplateViewProperties.setResourceLoaderPath(velocityTemplatePriperties.getPath());
//if (null == velocityTemplateViewProperties.getCharset())
this.velocityTemplateViewProperties.setCharset(velocityTemplatePriperties.getCharset());
}
}
}