Skip to content

Commit

Permalink
Allow for task-controlled modification of streams
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Dec 21, 2024
1 parent 41a8121 commit 1441847
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
18 changes: 18 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

jar {
manifest {
attributes(
'Automatic-Module-Name': 'dev.lukebemish.forkedtaskexecutor'
)
}
}



runnerJar {
manifest {
attributes(
'Automatic-Module-Name': 'dev.lukebemish.forkedtaskexecutor.runner'
)
}
}

testing {
suites {
def action = { suite ->
Expand Down
17 changes: 14 additions & 3 deletions src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.net.ServerSocket;
import java.net.Socket;
Expand All @@ -22,6 +24,10 @@ private Main(Task task) throws IOException {
this.socket = new ServerSocket(0);
}

private static final PrintStream OUT = System.out;
private static final PrintStream ERR = System.err;
private static final InputStream IN = System.in;

public static void main(String[] args) {
try {
Class<?> taskClass = Class.forName(args[0], false, Main.class.getClassLoader());
Expand All @@ -32,6 +38,11 @@ public static void main(String[] args) {
String[] otherArgs = new String[args.length - 1];
System.arraycopy(args, 1, otherArgs, 0, otherArgs.length);
var task = (Task) constructor.newInstance((Object) otherArgs);

System.setOut(task.replaceSystemOut(OUT));
System.setErr(task.replaceSystemErr(ERR));
System.setIn(task.replaceSystemIn(IN));

try (Main runner = new Main(task)) {
runner.run();
}
Expand All @@ -44,7 +55,7 @@ public static void main(String[] args) {

private void run() throws IOException {
// This tells the parent process what port we're listening on
System.out.println(socket.getLocalPort());
OUT.println(socket.getLocalPort());
var socket = this.socket.accept();
// Communication back to the parent is done through this handle, which ensures synchronization on the output stream.
var socketHandle = new SocketHandle(socket);
Expand Down Expand Up @@ -93,9 +104,9 @@ public void close() throws IOException, TimeoutException {

private static void logException(Throwable t) {
if (STACKTRACE) {
t.printStackTrace(System.err);
t.printStackTrace(ERR);
} else {
System.err.println(t);
ERR.println(t);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package dev.lukebemish.forkedtaskexecutor.runner;

import java.io.InputStream;
import java.io.PrintStream;

public interface Task {
byte[] run(byte[] input) throws Exception;

default PrintStream replaceSystemOut(PrintStream out) {
return out;
}

default InputStream replaceSystemIn(InputStream in) {
return in;
}

default PrintStream replaceSystemErr(PrintStream err) {
return err;
}
}

0 comments on commit 1441847

Please sign in to comment.