All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.template.TemplateAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.template;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.zodiac.autoconfigure.application.ApplicationInfoProperties;
import org.zodiac.commons.util.ArrayUtil;
import org.zodiac.commons.util.Colls;

@SpringBootConfiguration
@ConditionalOnClass(value = {org.zodiac.template.base.TemplateService.class})
public class TemplateAutoConfiguration {

    public TemplateAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean
    @ConfigurationProperties(prefix = org.zodiac.template.base.constants.TemplateSystemPropertiesConstants.SPRING_TEMPLATE_PREFIX, ignoreInvalidFields = true)
    protected TemplateProperties templateProperties(ApplicationInfoProperties applicationInfoProperties) {
        return new TemplateProperties().setAppEnvType(applicationInfoProperties.envType());
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.template.base.TemplateService templateService(ObjectProvider> templateEngineProvider,
        TemplateProperties templateProperties) {
        org.zodiac.template.base.impl.TemplateServiceImpl service = new org.zodiac.template.base.impl.TemplateServiceImpl();
        service.setCacheEnabled(templateProperties.getCacheEnabled()).setDefaultExtension(templateProperties.getDefaultExtension())
        .setSearchExtensions(templateProperties.isSearchExtensions())
        .setSearchLocalizedTemplates(templateProperties.isSearchLocalizedTemplates())
        .setProductionMode(templateProperties.isProductionMode());
        Map enginesMap = templateEngineProvider.getIfAvailable();
        if (Colls.notEmptyMap(enginesMap)) {
            Map engineNameMappings = Colls.map();
            boolean overwriteDuplicated = templateProperties.isOverwriteDuplicatedExt();
            enginesMap.forEach((engineName, engine) -> {
                if (ArrayUtil.notEmptyArray(engine.getDefaultExtensions())) {
                    Arrays.stream(engine.getDefaultExtensions()).forEach(ext -> {
                        if (overwriteDuplicated)
                            engineNameMappings.put(ext, engineName);
                        else
                            engineNameMappings.putIfAbsent(ext, engineName);
                    });
                }
            });
            service.setEngines(enginesMap).setEngineNameMappings(engineNameMappings);
        }
        return service;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy