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

org.hawkular.commons.templates.internal.TemplateServiceImpl Maven / Gradle / Ivy

Go to download

Provides template services for Hawkular components. Templates are based on Freemarker and the services can be consumed as CDI or EJB dependencies.

There is a newer version: 0.9.8.Final
Show newest version
/*
 * Copyright 2014-2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hawkular.commons.templates.internal;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.security.PermitAll;
import javax.ejb.Singleton;
import javax.inject.Inject;

import org.hawkular.commons.templates.TemplateService;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * @author Juraci Paixão Kröhling
 */
@PermitAll
@Singleton
public class TemplateServiceImpl implements TemplateService {
    MsgLogger logger = MsgLogger.LOGGER;

    @Inject @OverrideDirectory
    String overrideDirectory;

    @Inject @ConfigurationDirectory
    String configurationDirectory;

    private Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);

    public TemplateServiceImpl() {
        this(null, null);
    }

    public TemplateServiceImpl(String overrideDirectory, String configurationDirectory) {
        if (null != overrideDirectory) {
            this.overrideDirectory = overrideDirectory;
        }

        if (null != configurationDirectory) {
            this.configurationDirectory = configurationDirectory;
        }
    }

    @Override
    public String getProcessedTemplate(String templateName, Locale locale, Map properties)
            throws IOException, TemplateException {
        Template template = configuration.getTemplate(templateName, locale);
        StringWriter writer = new StringWriter();
        template.process(properties, writer);
        return writer.toString();
    }

    @PostConstruct
    void configure() {
        Set loaders = new LinkedHashSet<>();

        if (null != overrideDirectory) {
            try {
                loaders.add(new FileTemplateLoader(new File(this.overrideDirectory)));
            } catch (IOException e) {
                logger.failedToLoadTemplatesFromOverrideDirectory(overrideDirectory, e);
            }
        }

        if (null != configurationDirectory) {
            try {
                loaders.add(new FileTemplateLoader(new File(this.configurationDirectory)));
            } catch (IOException e) {
                logger.failedToLoadTemplatesFromConfigDirectory(configurationDirectory, e);
            }
        }

        MultiTemplateLoader mtl = new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]));
        configuration.setDefaultEncoding("UTF-8");
        configuration.setTemplateLoader(mtl);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy