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

com.devonfw.cobigen.impl.extension.TemplateEngineRegistry Maven / Gradle / Ivy

There is a newer version: 2021.12.006
Show newest version
package com.devonfw.cobigen.impl.extension;

import java.util.Collections;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
import com.devonfw.cobigen.api.extension.TextTemplateEngine;
import com.devonfw.cobigen.impl.aop.ProxyFactory;
import com.google.common.collect.Maps;

/**
 * Registry for {@link TextTemplateEngine template engines}.
 */
public class TemplateEngineRegistry {

    /** Logger instance. */
    private static final Logger LOG = LoggerFactory.getLogger(TemplateEngineRegistry.class);

    /**
     * Currently registered {@link TextTemplateEngine}s mapped by their type
     */
    private static Map registeredEngines =
        Collections.synchronizedMap(Maps. newHashMap());

    /**
     * Registers a new {@link TextTemplateEngine template engine}
     * @param 
     *            type of the {@link TextTemplateEngine template engine} to be registered
     * @param templateEngine
     *            {@link TextTemplateEngine template engine} to be registered
     */
    public static  void register(Class templateEngine) {

        try {
            TextTemplateEngine engine = templateEngine.newInstance();
            LOG.info("Register template engine '{}'.", templateEngine.getCanonicalName());

            if (StringUtils.isNotBlank(engine.getName())) {
                if (registeredEngines.containsKey(engine.getName())) {
                    throw new CobiGenRuntimeException(
                        "An template engine with type " + engine.getName() + " has already been registered.");
                }
                registeredEngines.put(engine.getName(), engine);
            } else {
                throw new CobiGenRuntimeException("Cannot register a template engine without a type.");
            }

        } catch (InstantiationException | IllegalAccessException e) {
            throw new CobiGenRuntimeException(
                "Could not intantiate TemplateEngine '" + templateEngine.getCanonicalName(), e);
        }
    }

    /**
     * Returns a {@link TextTemplateEngine template engine} based on its name.
     * @param name
     *            of the {@link TextTemplateEngine template engine}
     * @return the {@link TextTemplateEngine template engine} or {@code null} if no template engine has been
     *         registered with the name.
     */
    public static TextTemplateEngine getEngine(String name) {

        TextTemplateEngine templateEngine = registeredEngines.get(name);
        if (templateEngine == null) {
            throw new CobiGenRuntimeException("No template engine with name '" + name + "' registered.");
        }

        return ProxyFactory.getProxy(templateEngine);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy