Skip to content

Commit 147c0ab

Browse files
committed
Add Windows execution model
1 parent 57b6d09 commit 147c0ab

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

src/windows/WindowsExecution.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package windows;
2+
3+
import java.time.Instant;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
/** A controlled Windows process execution request and result. */
8+
public final class WindowsExecution {
9+
private final WindowsProgram program;
10+
private final WindowsDirective directive;
11+
private final WindowsCitizen citizen;
12+
private final List<String> arguments;
13+
private final Map<String, String> environment;
14+
15+
public WindowsExecution(
16+
WindowsProgram program,
17+
WindowsDirective directive,
18+
WindowsCitizen citizen,
19+
List<String> arguments,
20+
Map<String, String> environment) {
21+
this.program = program;
22+
this.directive = directive;
23+
this.citizen = citizen;
24+
this.arguments = List.copyOf(arguments);
25+
this.environment = Map.copyOf(environment);
26+
}
27+
28+
public WindowsProgram program() { return program; }
29+
public WindowsDirective directive() { return directive; }
30+
public WindowsCitizen citizen() { return citizen; }
31+
public List<String> arguments() { return arguments; }
32+
public Map<String, String> environment() { return environment; }
33+
34+
public WindowsExecutionResult execute() throws Exception {
35+
if (!directive.allowsExecution()) {
36+
throw new SecurityException("Windows directive does not permit execution: " + directive.name());
37+
}
38+
39+
ProcessBuilder builder = new ProcessBuilder(program.command(arguments));
40+
builder.environment().putAll(environment);
41+
builder.redirectErrorStream(true);
42+
43+
Instant started = Instant.now();
44+
Process process = builder.start();
45+
String output = new String(process.getInputStream().readAllBytes());
46+
int exitCode = process.waitFor();
47+
Instant finished = Instant.now();
48+
49+
return new WindowsExecutionResult(exitCode, output, started, finished);
50+
}
51+
52+
public record WindowsExecutionResult(
53+
int exitCode,
54+
String output,
55+
Instant started,
56+
Instant finished) {
57+
public boolean successful() { return exitCode == 0; }
58+
}
59+
}

0 commit comments

Comments
 (0)