summaryrefslogtreecommitdiffstats
path: root/api/src/main/java/org/openslx/taskmanager/api/Environment.java
diff options
context:
space:
mode:
authorSimon Rettberg2020-02-25 14:21:39 +0100
committerSimon Rettberg2020-02-25 14:21:39 +0100
commit5c27d5800d7c4a9c1a5d41c6ce427156652b74b0 (patch)
treeadde27c2a2c7cdff77c69d47a97e4627a675e924 /api/src/main/java/org/openslx/taskmanager/api/Environment.java
parentDeserialize Strings to UTF-8 byte[] (diff)
downloadtaskman-lite-5c27d5800d7c4a9c1a5d41c6ce427156652b74b0.tar.gz
taskman-lite-5c27d5800d7c4a9c1a5d41c6ce427156652b74b0.tar.xz
taskman-lite-5c27d5800d7c4a9c1a5d41c6ce427156652b74b0.zip
Move Environment class to API, fix loading from file
Diffstat (limited to 'api/src/main/java/org/openslx/taskmanager/api/Environment.java')
-rw-r--r--api/src/main/java/org/openslx/taskmanager/api/Environment.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/api/src/main/java/org/openslx/taskmanager/api/Environment.java b/api/src/main/java/org/openslx/taskmanager/api/Environment.java
new file mode 100644
index 0000000..3942ec5
--- /dev/null
+++ b/api/src/main/java/org/openslx/taskmanager/api/Environment.java
@@ -0,0 +1,68 @@
+package org.openslx.taskmanager.api;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+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 = null;
+
+ public static boolean load( String fileName )
+ {
+ if ( env != null )
+ throw new RuntimeException( "Already loaded" );
+ try {
+ Pattern regex = Pattern.compile( "^([a-zA-Z0-9_]+)(|=.*)$" );
+ FileReader fileReader = new FileReader( fileName );
+ BufferedReader bufferedReader = new BufferedReader( fileReader );
+
+ Map<String, String> env = new LinkedHashMap<>();
+ String line = null;
+ while ( ( line = bufferedReader.readLine() ) != null ) {
+ Matcher m = regex.matcher( line );
+ if ( !m.matches() )
+ continue;
+ String name = m.group( 1 );
+ String value = m.group( 2 );
+ if ( value.isEmpty() ) {
+ value = System.getenv( name );
+ } else {
+ value = value.substring( 1 );
+ }
+ if ( value != null ) {
+ env.put( name, value );
+ }
+ }
+
+ 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 Map<String, String> get()
+ {
+ return Collections.unmodifiableMap( env );
+ }
+
+}