|
25 | 25 | import io.serverlessworkflow.impl.WorkflowModel; |
26 | 26 | import io.serverlessworkflow.impl.WorkflowUtils; |
27 | 27 | import io.serverlessworkflow.impl.expressions.ExpressionUtils; |
28 | | - |
29 | 28 | import java.io.IOException; |
30 | 29 | import java.io.OutputStream; |
31 | 30 | import java.util.Map; |
32 | 31 | import java.util.concurrent.CompletableFuture; |
33 | 32 |
|
34 | 33 | public class RunShellExecutor implements RunnableTask<RunShell> { |
35 | 34 |
|
36 | | - private ProcessResultSupplier processResultSupplier; |
37 | | - private CommandSupplier commandSupplier; |
38 | | - |
39 | | - @FunctionalInterface |
40 | | - private interface ProcessResultSupplier { |
41 | | - ProcessResult apply(TaskContext taskContext, ProcessBuilder processBuilder); |
42 | | - } |
43 | | - |
44 | | - private interface CommandSupplier { |
45 | | - ProcessBuilder apply(TaskContext taskContext, WorkflowContext workflowContext); |
46 | | - } |
47 | | - |
48 | | - @Override |
49 | | - public CompletableFuture<WorkflowModel> apply( |
50 | | - WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { |
51 | | - ProcessBuilder processBuilder = this.commandSupplier.apply(taskContext, workflowContext); |
52 | | - ProcessResult processResult = this.processResultSupplier.apply(taskContext, processBuilder); |
53 | | - return CompletableFuture.completedFuture( |
54 | | - workflowContext.definition().application().modelFactory().fromAny(processResult)); |
55 | | - } |
56 | | - |
57 | | - @Override |
58 | | - public void init(RunShell taskConfiguration, WorkflowDefinition definition) { |
59 | | - Shell shell = taskConfiguration.getShell(); |
60 | | - final String shellCommand = shell.getCommand(); |
61 | | - |
62 | | - this.commandSupplier = |
63 | | - (taskContext, workflowContext) -> { |
64 | | - WorkflowApplication application = definition.application(); |
65 | | - |
66 | | - String command = |
67 | | - ExpressionUtils.isExpr(shellCommand) |
68 | | - ? WorkflowUtils.buildStringResolver( |
69 | | - application, shellCommand, taskContext.input().asJavaObject()) |
70 | | - .apply(workflowContext, taskContext, taskContext.input()) |
71 | | - : shellCommand; |
72 | | - |
73 | | - ProcessBuilder builder = new ProcessBuilder("sh", "-c", command); |
74 | | - |
75 | | - if (shell.getEnvironment() != null |
76 | | - && shell.getEnvironment().getAdditionalProperties() != null) { |
77 | | - for (Map.Entry<String, Object> entry : |
78 | | - shell.getEnvironment().getAdditionalProperties().entrySet()) { |
79 | | - String value = |
80 | | - ExpressionUtils.isExpr(entry.getValue()) |
81 | | - ? WorkflowUtils.buildStringResolver( |
82 | | - application, |
83 | | - entry.getValue().toString(), |
84 | | - taskContext.input().asJavaObject()) |
85 | | - .apply(workflowContext, taskContext, taskContext.input()) |
86 | | - : entry.getValue().toString(); |
87 | | - builder.environment().put(entry.getKey(), value); |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - return builder; |
92 | | - }; |
93 | | - |
94 | | - this.processResultSupplier = |
95 | | - (taskContext, processBuilder) -> { |
96 | | - if (shellCommand == null || shellCommand.isBlank()) { |
97 | | - throw new IllegalStateException( |
98 | | - "Missing shell command in RunShell task: " + taskContext.taskName()); |
99 | | - } |
100 | | - |
101 | | - try { |
102 | | - Process process = processBuilder.start(); |
103 | | - StringBuilder stdout = new StringBuilder(); |
104 | | - StringBuilder stderr = new StringBuilder(); |
105 | | - |
106 | | - process.getInputStream().transferTo(the(stdout)); |
107 | | - process.getErrorStream().transferTo(the(stderr)); |
108 | | - |
109 | | - int exitCode = process.waitFor(); |
110 | | - |
111 | | - return new ProcessResult(exitCode, stdout.toString().trim(), stderr.toString().trim()); |
112 | | - |
113 | | - } catch (IOException | InterruptedException e) { |
114 | | - throw new RuntimeException(e); |
115 | | - } |
116 | | - }; |
117 | | - } |
118 | | - |
119 | | - @Override |
120 | | - public boolean accept(Class<? extends RunTaskConfiguration> clazz) { |
121 | | - return RunShell.class.equals(clazz); |
122 | | - } |
123 | | - |
124 | | - |
125 | | - /** |
126 | | - * Helper to create an OutputStream that writes to a StringBuilder |
127 | | - * @param output the {@link StringBuilder} to write to |
128 | | - * @return the {@link OutputStream} |
129 | | - */ |
130 | | - private static OutputStream the(StringBuilder output) { |
131 | | - return new OutputStream() { |
132 | | - @Override |
133 | | - public void write(int b) { |
134 | | - output.append((char) b); |
| 35 | + private ProcessResultSupplier processResultSupplier; |
| 36 | + private CommandSupplier commandSupplier; |
| 37 | + |
| 38 | + @FunctionalInterface |
| 39 | + private interface ProcessResultSupplier { |
| 40 | + ProcessResult apply(TaskContext taskContext, ProcessBuilder processBuilder); |
| 41 | + } |
| 42 | + |
| 43 | + private interface CommandSupplier { |
| 44 | + ProcessBuilder apply(TaskContext taskContext, WorkflowContext workflowContext); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public CompletableFuture<WorkflowModel> apply( |
| 49 | + WorkflowContext workflowContext, TaskContext taskContext, WorkflowModel input) { |
| 50 | + ProcessBuilder processBuilder = this.commandSupplier.apply(taskContext, workflowContext); |
| 51 | + ProcessResult processResult = this.processResultSupplier.apply(taskContext, processBuilder); |
| 52 | + return CompletableFuture.completedFuture( |
| 53 | + workflowContext.definition().application().modelFactory().fromAny(processResult)); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void init(RunShell taskConfiguration, WorkflowDefinition definition) { |
| 58 | + Shell shell = taskConfiguration.getShell(); |
| 59 | + final String shellCommand = shell.getCommand(); |
| 60 | + |
| 61 | + this.commandSupplier = |
| 62 | + (taskContext, workflowContext) -> { |
| 63 | + WorkflowApplication application = definition.application(); |
| 64 | + |
| 65 | + String command = |
| 66 | + ExpressionUtils.isExpr(shellCommand) |
| 67 | + ? WorkflowUtils.buildStringResolver( |
| 68 | + application, shellCommand, taskContext.input().asJavaObject()) |
| 69 | + .apply(workflowContext, taskContext, taskContext.input()) |
| 70 | + : shellCommand; |
| 71 | + |
| 72 | + ProcessBuilder builder = new ProcessBuilder("sh", "-c", command); |
| 73 | + |
| 74 | + if (shell.getEnvironment() != null |
| 75 | + && shell.getEnvironment().getAdditionalProperties() != null) { |
| 76 | + for (Map.Entry<String, Object> entry : |
| 77 | + shell.getEnvironment().getAdditionalProperties().entrySet()) { |
| 78 | + String value = |
| 79 | + ExpressionUtils.isExpr(entry.getValue()) |
| 80 | + ? WorkflowUtils.buildStringResolver( |
| 81 | + application, |
| 82 | + entry.getValue().toString(), |
| 83 | + taskContext.input().asJavaObject()) |
| 84 | + .apply(workflowContext, taskContext, taskContext.input()) |
| 85 | + : entry.getValue().toString(); |
| 86 | + builder.environment().put(entry.getKey(), value); |
135 | 87 | } |
| 88 | + } |
| 89 | + |
| 90 | + return builder; |
| 91 | + }; |
| 92 | + |
| 93 | + this.processResultSupplier = |
| 94 | + (taskContext, processBuilder) -> { |
| 95 | + if (shellCommand == null || shellCommand.isBlank()) { |
| 96 | + throw new IllegalStateException( |
| 97 | + "Missing shell command in RunShell task: " + taskContext.taskName()); |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + Process process = processBuilder.start(); |
| 102 | + StringBuilder stdout = new StringBuilder(); |
| 103 | + StringBuilder stderr = new StringBuilder(); |
| 104 | + |
| 105 | + process.getInputStream().transferTo(the(stdout)); |
| 106 | + process.getErrorStream().transferTo(the(stderr)); |
| 107 | + |
| 108 | + int exitCode = process.waitFor(); |
| 109 | + |
| 110 | + return new ProcessResult(exitCode, stdout.toString().trim(), stderr.toString().trim()); |
| 111 | + |
| 112 | + } catch (IOException | InterruptedException e) { |
| 113 | + throw new RuntimeException(e); |
| 114 | + } |
136 | 115 | }; |
137 | | - } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean accept(Class<? extends RunTaskConfiguration> clazz) { |
| 120 | + return RunShell.class.equals(clazz); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Helper to create an OutputStream that writes to a StringBuilder |
| 125 | + * |
| 126 | + * @param output the {@link StringBuilder} to write to |
| 127 | + * @return the {@link OutputStream} |
| 128 | + */ |
| 129 | + private static OutputStream the(StringBuilder output) { |
| 130 | + return new OutputStream() { |
| 131 | + @Override |
| 132 | + public void write(int b) { |
| 133 | + output.append((char) b); |
| 134 | + } |
| 135 | + }; |
| 136 | + } |
138 | 137 | } |
0 commit comments