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

com.github.sitture.env.config.EnvironmentVariables Maven / Gradle / Ivy

Go to download

A simple utility to manage environment configs in Java-based projects by merging *.properties files with environment variables overrides.

There is a newer version: 1.12.1
Show newest version
package com.github.sitture.env.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.MapConfiguration;
import org.apache.commons.configuration2.SystemConfiguration;

class EnvironmentVariables {

	private static Configuration systemConfiguration;
	private static Configuration environmentConfiguration;

	EnvironmentVariables() {
		systemConfiguration = new SystemConfiguration();
		environmentConfiguration = new MapConfiguration(getEnvMap());
	}

	Configuration getSystemConfiguration() {
		return systemConfiguration;
	}

	Configuration getEnvironmentConfiguration() {
		return environmentConfiguration;
	}

	private static Map getEnvMap() {
		Map envMap = new HashMap();
		System.getenv().entrySet().forEach(entry -> {
			envMap.put(entry.getKey(), entry.getValue());
			envMap.put(getProcessedEnvKey(entry.getKey()), entry.getValue());
		});
		return envMap;
	}

	private static String getProcessedEnvKey(final String envVar) {
		return envVar.replaceAll("_", ".").toLowerCase();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy