Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
Configuration: environment variable expansion
Browse files Browse the repository at this point in the history
Enable environment variable expansion in crail-site.conf.

Close #7

Signed-off-by: Jonas Pfefferle <pepperjo@apache.org>
  • Loading branch information
raduioanstoica authored and PepperJo committed Mar 2, 2018
1 parent baab8d0 commit 9b857f5
Showing 1 changed file with 54 additions and 24 deletions.
78 changes: 54 additions & 24 deletions client/src/main/java/org/apache/crail/conf/CrailConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,80 @@

package org.apache.crail.conf;

import org.apache.crail.utils.CrailUtils;
import org.slf4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.crail.utils.CrailUtils;
import org.slf4j.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CrailConfiguration {
private static final Logger LOG = CrailUtils.getLogger();
private ConcurrentHashMap<String, String> conf;


public CrailConfiguration() throws IOException{

public CrailConfiguration() throws IOException {
conf = new ConcurrentHashMap<>();
Properties properties = loadProperties("crail-site.conf");
mergeProperties(properties);
}

private static String expandEnvVars(String input) throws IOException {
if (null == input) {
return null;
}
// match ${ENV_VAR_NAME} or $ENV_VAR_NAME
Pattern p = Pattern.compile("\\$\\{(\\w+)\\}|\\$(\\w+)");
Matcher m = p.matcher(input);
StringBuffer output = new StringBuffer();
while (m.find()) {
String envVar;
if (m.group(1) != null) {
envVar = m.group(1);
} else {
envVar = m.group(2);
}
String envVal = System.getenv(envVar);
if (envVal == null) {
throw new IOException("Could not expand environment variable $" + envVar);
}
m.appendReplacement(output, envVal);
}
m.appendTail(output);
return output.toString();
}

private static Properties loadProperties(String resourceName) throws IOException {
Properties properties = new Properties();

String base = System.getenv("CRAIL_HOME");
FileInputStream inputStream = new FileInputStream(new File(base + "/conf/" + resourceName));

try {
properties.load(inputStream);
} finally {
inputStream.close();
}
for (String key : properties.stringPropertyNames()) {
String val = properties.getProperty(key);
properties.setProperty(key, expandEnvVars(val));
}
return properties;
}

public String get(String key) {
return conf.get(key);
}

public void set(String key, String value) {
conf.put(key, value);
}

public boolean getBoolean(String key, boolean fallback) {
if (conf.containsKey(key)){
if (conf.containsKey(key)) {
return Boolean.parseBoolean(conf.get(key));
} else {
return fallback;
Expand All @@ -59,27 +103,13 @@ public void setInt(String key, int level) {
String value = Integer.toString(level);
conf.put(key, value);
}

private void mergeProperties(Properties properties) {
if (properties == null){
if (properties == null) {
return;
}
for (String key : properties.stringPropertyNames()) {
conf.put(key.trim(), properties.getProperty(key).trim());
}
}

private static Properties loadProperties(String resourceName) throws IOException {
Properties properties = new Properties();

String base = System.getenv("CRAIL_HOME");
FileInputStream inputStream = new FileInputStream(new File(base + "/conf/" + resourceName));

try {
properties.load(inputStream);
} finally {
inputStream.close();
}
return properties;
}
}

0 comments on commit 9b857f5

Please sign in to comment.