Skip to content

Commit

Permalink
ProcessBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Aug 20, 2024
1 parent fa9f8a5 commit 678243d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package xyz.wagyourtail.jvmdg.j9.stub.java_base;

import xyz.wagyourtail.jvmdg.version.Ref;
import xyz.wagyourtail.jvmdg.version.Stub;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class J_L_ProcessBuilder {

@Stub(ref = @Ref("java/lang/ProcessBuilder"))
public static List<Process> startPipeline(List<ProcessBuilder> builders) throws IOException {
List<Process> processes = new ArrayList<>();
try {
InputStream prevOutput = null;
for (int i = 0; i < builders.size(); i++) {
ProcessBuilder processBuilder = builders.get(i);
if (i > 0) {
ProcessBuilder.Redirect redirect = processBuilder.redirectInput();
if (redirect != ProcessBuilder.Redirect.PIPE) {
throw new IllegalArgumentException("builder redirectInput() must be PIPE except for the first builder: " + processBuilder.redirectInput());
}
}
if (i < builders.size() - 1) {
ProcessBuilder.Redirect redirect = processBuilder.redirectOutput();
if (redirect != ProcessBuilder.Redirect.PIPE) {
throw new IllegalArgumentException("builder redirectOutput() must be PIPE except for the last builder: " + processBuilder.redirectOutput());
}
}
Process p = processBuilder.start();
OutputStream nextInput = p.getOutputStream();
if (i > 0) {
InputStream finalPrevOutput = prevOutput;
CompletableFuture.runAsync(() -> {
try {
J_I_InputStream.transferTo(finalPrevOutput, nextInput);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
processes.add(p);
prevOutput = p.getInputStream();
}
} catch (Exception e) {
for (Process process : processes) {
process.destroyForcibly();
}
for (Process process : processes) {
try {
process.waitFor();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
throw e;
}
return processes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public void init() {
stub(J_L_Module.class);
stub(J_L_ModuleLayer.class);
stub(J_L_Process.class);
// ProcessBuilder
stub(J_L_ProcessBuilder.class);
stub(J_L_ProcessHandle.class);
// ProcessHandleImpl
stub(J_L_Runtime.class);
stub(J_L_Runtime.Version.class);
stub(J_L_Short.class);
Expand Down

0 comments on commit 678243d

Please sign in to comment.