diff options
author | Simon Rettberg | 2020-02-25 14:21:39 +0100 |
---|---|---|
committer | Simon Rettberg | 2020-02-25 14:21:39 +0100 |
commit | 5c27d5800d7c4a9c1a5d41c6ce427156652b74b0 (patch) | |
tree | adde27c2a2c7cdff77c69d47a97e4627a675e924 | |
parent | Deserialize Strings to UTF-8 byte[] (diff) | |
download | taskman-lite-5c27d5800d7c4a9c1a5d41c6ce427156652b74b0.tar.gz taskman-lite-5c27d5800d7c4a9c1a5d41c6ce427156652b74b0.tar.xz taskman-lite-5c27d5800d7c4a9c1a5d41c6ce427156652b74b0.zip |
Move Environment class to API, fix loading from file
-rw-r--r-- | api/src/main/java/org/openslx/taskmanager/api/Environment.java (renamed from daemon/src/main/java/org/openslx/taskmanager/Environment.java) | 45 | ||||
-rw-r--r-- | api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java | 7 | ||||
-rw-r--r-- | daemon/src/main/java/org/openslx/taskmanager/App.java | 1 |
3 files changed, 30 insertions, 23 deletions
diff --git a/daemon/src/main/java/org/openslx/taskmanager/Environment.java b/api/src/main/java/org/openslx/taskmanager/api/Environment.java index acbfad4..3942ec5 100644 --- a/daemon/src/main/java/org/openslx/taskmanager/Environment.java +++ b/api/src/main/java/org/openslx/taskmanager/api/Environment.java @@ -1,11 +1,13 @@ -package org.openslx.taskmanager; +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.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.log4j.Logger; @@ -18,21 +20,33 @@ public class Environment private static final Logger log = Logger.getLogger( Environment.class ); - private static Map<String, String> env = new LinkedHashMap<>(); + 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 ) { - if ( !line.matches( "^[a-zA-Z0-9_]+=" ) ) + Matcher m = regex.matcher( line ); + if ( !m.matches() ) continue; - String[] part = line.split( "=", 2 ); - env.put( part[0], part[1] ); + 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(); @@ -45,23 +59,10 @@ public class Environment } return true; } - - public static void set( Map<String, String> environment ) + + public static Map<String, String> get() { - 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; + return Collections.unmodifiableMap( env ); } } diff --git a/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java b/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java index 18c8ecc..c1aa6e5 100644 --- a/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java +++ b/api/src/main/java/org/openslx/taskmanager/api/SystemCommandTask.java @@ -48,7 +48,12 @@ public abstract class SystemCommandTask extends AbstractTask ProcessBuilder pb = new ProcessBuilder( command ); pb.directory( new File( "/" ) ); - initEnvironment( pb.environment() ); + Map<String, String> env = pb.environment(); + if ( Environment.get() != null ) { + env.clear(); + env.putAll( Environment.get() ); + } + initEnvironment( env ); try { diff --git a/daemon/src/main/java/org/openslx/taskmanager/App.java b/daemon/src/main/java/org/openslx/taskmanager/App.java index d64929d..3b8eb66 100644 --- a/daemon/src/main/java/org/openslx/taskmanager/App.java +++ b/daemon/src/main/java/org/openslx/taskmanager/App.java @@ -6,6 +6,7 @@ import java.util.List; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Logger; +import org.openslx.taskmanager.api.Environment; import org.openslx.taskmanager.main.Taskmanager; import org.openslx.taskmanager.network.NetworkHandlerTcp; import org.openslx.taskmanager.network.NetworkHandlerUdp; |