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

eu.unicore.util.configuration.VariableResolver Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2017 ICM Uniwersytet Warszawski All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package eu.unicore.util.configuration;

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

import org.apache.logging.log4j.Logger;

/**
 * Maintains a list of defined variables and allow for resolving them.
 * Order is as follows (first matchin is used): 
 * 
    *
  1. System Java properties (typically from command line -D) *
  2. environment variables *
  3. config file-defined variables *
* @author K. Benedyczak */ public class VariableResolver { protected Logger log; private Map configVariables = new HashMap<>(); public VariableResolver(Logger log) { this.log = log; } public void addVariable(String variable, String value) { configVariables.put(variable, value); } public String resolve(String variable) { String resolved = System.getProperty(variable); if (resolved != null) { log.trace("Using system property as a source for " + variable + ": " + resolved); return resolved; } resolved = System.getenv(variable); if (resolved != null) { log.trace("Using environment variable as a source for " + variable + ": " + resolved); return resolved; } resolved = configVariables.get(variable); if (resolved != null) { log.trace("Using config file defined variable as a source for " + variable + ": " + resolved); return resolved; } throw new ConfigurationException("Variable " + variable + " is not defined"); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy