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

com.dev9.conf.WebtestConfigFactory Maven / Gradle / Ivy

There is a newer version: 1.6
Show newest version
package com.dev9.conf;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import lombok.extern.log4j.Log4j2;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * User: yurodivuie
 * Date: 5/27/13
 * Time: 6:07 PM
 */
@Log4j2
public class WebtestConfigFactory {

    private static final String CONF_EXTENSION = ".conf";
    private static final String PROFILE_PATH = "webtest.profile";
    private static final Config DEFAULT_CONFIG = addProfileToConfig(ConfigFactory.defaultOverrides()
            .withFallback(ConfigFactory.load()));
    private static final Map CLASS_CONFIG = new ConcurrentHashMap();

    public static Config getConfig() {
        return DEFAULT_CONFIG;
    }

    public static Config getConfig(Class testClass) {
        if (!CLASS_CONFIG.containsKey(testClass)) {
            buildConfigForClass(testClass);
        }
        return CLASS_CONFIG.get(testClass);
    }

    private static void buildConfigForClass(Class testClass) {
        File classConfigFile = getClassConfigFile(testClass);
        Config classConfig;
        if (classConfigFile != null) {
            classConfig = ConfigFactory.defaultOverrides()
                    .withFallback(ConfigFactory.parseFile(getClassConfigFile(testClass)))
                    .withFallback(ConfigFactory.load());
            classConfig = addProfileToConfig(classConfig);
        }
        else {
            classConfig = getConfig();
        }
        CLASS_CONFIG.put(testClass, classConfig);
    }

    private static File getClassConfigFile(Class testClass) {
        URL classFileUrl = testClass.getResource(testClass.getSimpleName() + CONF_EXTENSION);
        File classFile = null;
        if (classFileUrl != null) {
            try {
                classFile = new File(classFileUrl.toURI());
            }
            catch (URISyntaxException ex) {
                log.warn("Failed to open config file for " + testClass.getSimpleName() + ": {}", ex.getMessage());
            }
        }
        return classFile;
    }

    private static Config addProfileToConfig(Config config) {
        String profile = getProfile(config);
        if (profile != null && config != null && config.hasPath(profile)) {
            return config.getConfig(profile).withFallback(config);
        }
        return config;
    }

    private static String getProfile(Config config) {
        String profile = null;
        if ( config != null && config.hasPath(PROFILE_PATH) ) {
            profile = config.getString(PROFILE_PATH);
        }
        return profile;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy