diff --git a/build.gradle b/build.gradle index 3782a89..b429c0a 100644 --- a/build.gradle +++ b/build.gradle @@ -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 -> diff --git a/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Main.java b/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Main.java index 37629f8..2f7de6b 100644 --- a/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Main.java +++ b/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Main.java @@ -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; @@ -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()); @@ -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(); } @@ -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); @@ -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); } } diff --git a/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Task.java b/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Task.java index 02f137c..c5c76d7 100644 --- a/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Task.java +++ b/src/runner/java/dev/lukebemish/forkedtaskexecutor/runner/Task.java @@ -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; + } }