Skip to content

Commit

Permalink
allo setting env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Jan 14, 2025
1 parent efe0f0b commit 539853b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/io/bioimage/modelrunner/apposed/appose/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class Service implements AutoCloseable {
private Thread stdoutThread;
private Thread stderrThread;
private Thread monitorThread;

private Map<String, String> envMap = new HashMap<String, String>();

private Consumer<String> debugListener;

Expand All @@ -87,6 +89,17 @@ public Service(File cwd, String... args) {
public void debug(Consumer<String> debugListener) {
this.debugListener = debugListener;
}

/**
* Set the wanted environment variable for the task
* @param key
* key of the env var
* @param val
* value of the environment var
*/
public void setEnvVar(String key, String val) {
this.envMap.put(key, val);
}

/**
* Launches the worker process associated with this service.
Expand All @@ -102,6 +115,12 @@ public Service start() throws IOException {

String prefix = "Appose-Service-" + serviceID;
ProcessBuilder pb = new ProcessBuilder(args).directory(cwd);
envMap.entrySet().stream().forEach(ee -> {
if (ee.getValue() == null && pb.environment().get(ee.getKey()) != null)
pb.environment().remove(ee.getKey());
else if (ee.getValue() != null)
pb.environment().put(ee.getKey(), ee.getValue());
});
process = pb.start();
stdin = new PrintWriter(process.getOutputStream());
stdoutThread = new Thread(this::stdoutLoop, prefix + "-Stdout");
Expand Down

0 comments on commit 539853b

Please sign in to comment.