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

com.automationrockstars.base.ConfigLoader Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2015, 2016 Automation RockStars Ltd.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Automation RockStars - initial API and implementation
 *******************************************************************************/
package com.automationrockstars.base;

import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.EnvironmentConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.commons.configuration.event.ConfigurationListener;
import org.apache.commons.configuration.event.EventSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Iterators;
import com.google.common.io.Resources;

public class ConfigLoader {

	private static CompositeConfiguration config;
	public static final String PROPS = "test_props";
	public static Configuration config(){
		return getConfig();
	}
	private static Logger LOG = LoggerFactory.getLogger(ConfigLoader.class);

	private static Configuration getConfig(){
		if (config == null){
			PropertiesConfiguration props = new PropertiesConfiguration();
			try {
				File propertiesFirectory = new File(Resources.getResource(PROPS).toURI());
				for (File propFile : propertiesFirectory.listFiles()){

					try {
						props.load(propFile);
						LOG.debug("Properties loaded from file {}", propFile);
						logConfig(props);
					} catch (ConfigurationException e) {
						LOG.error("Error loading file {} {}",propFile, e.getMessage());		
					}

				}
			} catch (IllegalArgumentException | URISyntaxException  e) {
				LOG.error("Error loading configuration",e.getMessage());
			}
			
			config = new RuntimeSetterDecorator(
					Arrays.asList(
					new SystemConfiguration(),
					new EnvironmentConfiguration(),
					props
					));
			for (int i =0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy