summaryrefslogtreecommitdiffstats
path: root/daemon/src/main/java/org/openslx/taskmanager/Environment.java
diff options
context:
space:
mode:
Diffstat (limited to 'daemon/src/main/java/org/openslx/taskmanager/Environment.java')
-rw-r--r--daemon/src/main/java/org/openslx/taskmanager/Environment.java67
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;
+ }
+
+}