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

com.devonfw.cobigen.impl.config.ConfigurationHolder Maven / Gradle / Ivy

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

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import com.devonfw.cobigen.api.exception.InvalidConfigurationException;
import com.devonfw.cobigen.impl.config.entity.Trigger;
import com.devonfw.cobigen.impl.extension.PluginRegistry;
import com.devonfw.cobigen.impl.util.FileSystemUtil;
import com.google.common.collect.Maps;

/**
 * Cached in-memory CobiGen configuration.
 */
public class ConfigurationHolder {

    /** Cached templates configurations. Configuration File URI -> Trigger ID -> configuration instance */
    private Map> templatesConfigurations = Maps.newHashMap();

    /** Cached context configuration */
    private ContextConfiguration contextConfiguration;

    /** Root path of the configuration */
    private Path configurationPath;

    /** The OS filesystem path of the configuration */
    private URI configurationLocation;

    /**
     * Creates a new {@link ConfigurationHolder} which serves as a cache for CobiGen's external configuration.
     * @param configurationLocation
     *            the OS Filesystem path of the configuration location.
     */
    public ConfigurationHolder(URI configurationLocation) {
        configurationPath = FileSystemUtil.createFileSystemDependentPath(configurationLocation);
        this.configurationLocation = configurationLocation;
        // updates the root template path and informs all of its observers
        PluginRegistry.notifyPlugins(configurationPath);
    }

    /**
     * @return true if the configuration is based in a JAR file
     */
    public boolean isJarConfig() {
        return FileSystemUtil.isZipFile(configurationLocation);
    }

    /**
     * @return the path of the configuration based on the OS filesystem. It could be a .jar file or a maven
     *         project root folder
     */
    public URI getConfigurationLocation() {
        return configurationLocation;
    }

    /**
     * @return the path within the configuration. Might be a different file system than OS in case of a .jar
     *         configuration
     */
    public Path getConfigurationPath() {
        return configurationPath;
    }

    /**
     * Reads the {@link TemplatesConfiguration} from cache or from file if not present in cache.
     * @param trigger
     *            to get matcher declarations from
     * @return the {@link TemplatesConfiguration}
     * @throws InvalidConfigurationException
     *             if the configuration is not valid
     */
    public TemplatesConfiguration readTemplatesConfiguration(Trigger trigger) {

        Path templateFolder = Paths.get(trigger.getTemplateFolder());
        if (!templatesConfigurations.containsKey(templateFolder)) {
            templatesConfigurations.put(templateFolder, Maps. newHashMap());

            TemplatesConfiguration config = new TemplatesConfiguration(configurationPath, trigger, this);
            templatesConfigurations.get(templateFolder).put(trigger.getId(), config);
        }

        return templatesConfigurations.get(templateFolder).get(trigger.getId());
    }

    /**
     * Reads the {@link ContextConfiguration} from cache or from file if not present in cache.
     * @return the {@link ContextConfiguration}
     * @throws InvalidConfigurationException
     *             if the configuration is not valid
     */
    public ContextConfiguration readContextConfiguration() {
        if (contextConfiguration == null) {
            contextConfiguration = new ContextConfiguration(configurationPath);
        }
        return contextConfiguration;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy