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

com.socialbakers.config.Envio Maven / Gradle / Ivy

Go to download

Maven plugin for small application config support. Allows you to define configuration for your application at one place and generate config files and java class. Configuration can be instantiate by var args, file config or environment variables or combination of all. TODO: - boolean opts -- is/get -- option without value true - make def enum public - static instance to access from everywhere without pass an instance

The newest version!
package com.socialbakers.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Envio {

	private static final Logger LOGGER = LoggerFactory.getLogger(Envio.class);

	public static void loadConfiguration() {

		File envFile = new File(System.getProperty("user.dir"), File.separator + ".env");

		if (!envFile.exists()) {
			return;
		}

		LOGGER.info("Loading envio configuration: {}", envFile);

		BufferedReader br = null;

		try {
			br = new BufferedReader(new FileReader(System.getProperty("user.dir") + File.separator + ".env"));

			Map currentEnv = System.getenv();
			Map newEnv = new HashMap();

			for (Entry entry : currentEnv.entrySet()) {
				newEnv.put(entry.getKey(), entry.getValue());
			}
			String line;

			while ((line = br.readLine()) != null) {
				String[] conf = line.split("=", 2);

				if (conf.length == 2) {
					newEnv.put(conf[0], conf[1]);
				}
			}
			setEnv(newEnv);
		} catch (Exception e1) {
			LOGGER.error(e1.getMessage(), e1);
		} finally {
			IOUtils.closeQuietly(br);
		}
	}

	/**
	 * code from http://stackoverflow.com/a/7201825
	 */
	protected static void setEnv(Map newenv) {
		try {
			Class processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
			Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
			theEnvironmentField.setAccessible(true);
			Map env = (Map) theEnvironmentField.get(null);
			env.putAll(newenv);
			Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
					.getDeclaredField("theCaseInsensitiveEnvironment");
			theCaseInsensitiveEnvironmentField.setAccessible(true);
			Map cienv = (Map) theCaseInsensitiveEnvironmentField.get(null);
			cienv.putAll(newenv);
		} catch (NoSuchFieldException e) {
			try {
				Class[] classes = Collections.class.getDeclaredClasses();
				Map env = System.getenv();
				for (Class cl : classes) {
					if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
						Field field = cl.getDeclaredField("m");
						field.setAccessible(true);
						Object obj = field.get(env);
						Map map = (Map) obj;
						map.clear();
						map.putAll(newenv);
					}
				}
			} catch (Exception e2) {
				LOGGER.error(e2.getMessage(), e2);
			}
		} catch (Exception e1) {
			LOGGER.error(e1.getMessage(), e1);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy