package hudson; import hudson.remoting.Callable; import hudson.remoting.VirtualChannel; import hudson.util.CaseInsensitiveComparator; import java.io.File; import java.io.IOException; import java.util.Collections; import java.util.Map; import java.util.TreeMap; /** * Environment variables. * *

* In Hudson, often we need to build up "environment variable overrides" * on master, then to execute the process on slaves. This causes a problem * when working with variables like PATH. So to make this work, * we introduce a special convention PATH+FOO — all entries * that starts with PATH+ are merged and prepended to the inherited * PATH variable, on the process where a new process is executed. * * @author Kohsuke Kawaguchi */ public class EnvVars extends TreeMap { public EnvVars() { super(CaseInsensitiveComparator.INSTANCE); } public EnvVars(Map m) { super(CaseInsensitiveComparator.INSTANCE); putAll(m); } /** * Overrides the current entry by the given entry. * *

* Handles PATH+XYZ notation. */ public void override(String key, String value) { if(value==null || value.length()==0) { remove(key); return; } int idx = key.indexOf('+'); if(idx>0) { String realKey = key.substring(0,idx); String v = get(realKey); if(v==null) v=value; else v=value+File.pathSeparatorChar+v; put(realKey,v); return; } put(key,value); } public void overrideAll(Map all) { for (Map.Entry e : all.entrySet()) { override(e.getKey(),e.getValue()); } } /** * Takes a string that looks like "a=b" and adds that to this map. */ public void addLine(String line) { int sep = line.indexOf('='); if(sep > 0) { put(line.substring(0,sep),line.substring(sep+1)); } } /** * Obtains the environment variables of a remote peer. * * @param channel * Can be null, in which case the map indicating "N/A" will be returned. */ public static Map getRemote(VirtualChannel channel) throws IOException, InterruptedException { if(channel==null) return Collections.singletonMap("N/A","N/A"); return new EnvVars(channel.call(new GetEnvVars())); } private static final class GetEnvVars implements Callable,RuntimeException> { public Map call() { return new TreeMap(EnvVars.masterEnvVars); } private static final long serialVersionUID = 1L; } /** * Environmental variables that we've inherited. * *

* Despite what the name might imply, this is the environment variable * of the current JVM process. And therefore, it is Hudson master's environment * variables only when you access this from the master. * *

* If you access this field from slaves, then this is the environment * variable of the slave agent. */ public static final Map masterEnvVars = new EnvVars(System.getenv()); }