diff options
author | Simon Rettberg | 2014-06-03 16:44:56 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-06-03 16:44:56 +0200 |
commit | efb5ad9f5fe48a77b6cd14e7bd2b25e3b13ecb1f (patch) | |
tree | ab7310095194cea2a74acd8aac782b1ec95e508d /daemon/src/main/java/org/openslx/taskmanager/Environment.java | |
download | taskman-lite-efb5ad9f5fe48a77b6cd14e7bd2b25e3b13ecb1f.tar.gz taskman-lite-efb5ad9f5fe48a77b6cd14e7bd2b25e3b13ecb1f.tar.xz taskman-lite-efb5ad9f5fe48a77b6cd14e7bd2b25e3b13ecb1f.zip |
Initial commit
Diffstat (limited to 'daemon/src/main/java/org/openslx/taskmanager/Environment.java')
-rw-r--r-- | daemon/src/main/java/org/openslx/taskmanager/Environment.java | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/daemon/src/main/java/org/openslx/taskmanager/Environment.java b/daemon/src/main/java/org/openslx/taskmanager/Environment.java new file mode 100644 index 0000000..acbfad4 --- /dev/null +++ b/daemon/src/main/java/org/openslx/taskmanager/Environment.java @@ -0,0 +1,67 @@ +package org.openslx.taskmanager; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.log4j.Logger; + +/** + * Holds the environment that tasks running a system command *should* + * use. The environment is read from a config file. + */ +public class Environment +{ + + private static final Logger log = Logger.getLogger( Environment.class ); + + private static Map<String, String> env = new LinkedHashMap<>(); + + public static boolean load( String fileName ) + { + try { + FileReader fileReader = new FileReader( fileName ); + BufferedReader bufferedReader = new BufferedReader( fileReader ); + + Map<String, String> env = new LinkedHashMap<>(); + String line = null; + while ( ( line = bufferedReader.readLine() ) != null ) { + if ( !line.matches( "^[a-zA-Z0-9_]+=" ) ) + continue; + String[] part = line.split( "=", 2 ); + env.put( part[0], part[1] ); + } + + bufferedReader.close(); + + Environment.env = env; + log.info( "Loaded " + env.size() + " environment lines." ); + } catch ( IOException e ) { + log.info( "Could not load environment definition from " + fileName + ". Processes might use the same environment as this thread." ); + return false; + } + return true; + } + + public static void set( Map<String, String> environment ) + { + environment.clear(); + environment.putAll( env ); + } + + public static String[] get() + { + // Get reference to env so it doesn't change while in this function (load() from other thread) + Map<String, String> env = Environment.env; + String ret[] = new String[ env.size() ]; + int i = 0; + for ( Entry<String, String> it : env.entrySet() ) { + ret[i++] = it.getKey() + "=" + it.getValue(); + } + return ret; + } + +} |