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 env = new LinkedHashMap<>(); public static boolean load( String fileName ) { try { FileReader fileReader = new FileReader( fileName ); BufferedReader bufferedReader = new BufferedReader( fileReader ); Map 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 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 env = Environment.env; String ret[] = new String[ env.size() ]; int i = 0; for ( Entry it : env.entrySet() ) { ret[i++] = it.getKey() + "=" + it.getValue(); } return ret; } }