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

com.checkmarx.configprovider.resource.MultipleResources Maven / Gradle / Ivy

package com.checkmarx.configprovider.resource;

import com.checkmarx.configprovider.dto.interfaces.ConfigResource;
import com.typesafe.config.Config;

import javax.naming.ConfigurationException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * This class should be used with applying several Configuration Resources at the same time.
 * The elements will be applied based on the order ot their addition to the class,
 * unless there is a rule for the order of applying of specific element types
 */
public class MultipleResources extends ParsableResource implements ConfigResource {

    List configSourceList = new LinkedList<>();

    public MultipleResources(){
    }
    
    public MultipleResources(List configResources){
        add(configResources);
    }
    
    public void add(List configResources){
        configSourceList.addAll(configResources);
        applyOrder();
    }
    
    public void add(ParsableResource configSource){
        configSourceList.add(configSource);
        applyOrder();
    }

    private void applyOrder() {
        //TO IMPLEMENT
        
        //Env Yaml
        //application.yml
        //config-as-code
    }

    /**
     * Converts a list on configSources added to the class to a config
     * tree while applying a specified order. An element which has the 
     * same name and path will be overridden by the same element as per 
     * the applied hierarchy
     * @return Config tree
     * @throws ConfigurationException exception
     */
    @Override
    Config loadConfig() throws ConfigurationException {
        Config current = null;
        for (ParsableResource configSource : configSourceList ) {
            Config base = configSource.loadConfig();
            current = Optional.ofNullable(current)
                .map(base::withFallback)
                .orElse(base); /* for first iteration */
        }

        return current;
    }

    @Override
    public String getName() {
        return configSourceList.stream().map(resource -> ((ConfigResource)resource).getName().concat(" ")).collect(Collectors.toList()).toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy