|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.serverlessworkflow.impl.scripts; |
| 17 | + |
| 18 | +import io.serverlessworkflow.api.types.RunTaskConfiguration; |
| 19 | +import io.serverlessworkflow.impl.TaskContext; |
| 20 | +import io.serverlessworkflow.impl.WorkflowContext; |
| 21 | +import io.serverlessworkflow.impl.WorkflowError; |
| 22 | +import io.serverlessworkflow.impl.WorkflowException; |
| 23 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 24 | +import io.serverlessworkflow.impl.WorkflowModelFactory; |
| 25 | +import io.serverlessworkflow.impl.executors.ProcessResult; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | + |
| 28 | +public abstract class AbstractScriptRunner implements ScriptRunner { |
| 29 | + |
| 30 | + @Override |
| 31 | + public WorkflowModel runScript( |
| 32 | + ScriptContext scriptContext, |
| 33 | + WorkflowContext workflowContext, |
| 34 | + TaskContext taskContext, |
| 35 | + WorkflowModel input) { |
| 36 | + ByteArrayOutputStream stderr = new ByteArrayOutputStream(); |
| 37 | + ByteArrayOutputStream stdout = new ByteArrayOutputStream(); |
| 38 | + try { |
| 39 | + runScript(scriptContext, stdout, stderr, workflowContext, taskContext); |
| 40 | + return scriptContext |
| 41 | + .returnType() |
| 42 | + .map( |
| 43 | + type -> |
| 44 | + modelFromOutput( |
| 45 | + type, |
| 46 | + workflowContext.definition().application().modelFactory(), |
| 47 | + stdout, |
| 48 | + stderr)) |
| 49 | + .orElse(input); |
| 50 | + } catch (Exception ex) { |
| 51 | + throw new WorkflowException(WorkflowError.runtime(taskContext, ex).build()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected abstract void runScript( |
| 56 | + ScriptContext scriptContext, |
| 57 | + ByteArrayOutputStream stdout, |
| 58 | + ByteArrayOutputStream stderr, |
| 59 | + WorkflowContext workflowContext, |
| 60 | + TaskContext taskContext); |
| 61 | + |
| 62 | + protected WorkflowModel modelFromOutput( |
| 63 | + RunTaskConfiguration.ProcessReturnType returnType, |
| 64 | + WorkflowModelFactory modelFactory, |
| 65 | + ByteArrayOutputStream stdout, |
| 66 | + ByteArrayOutputStream stderr) { |
| 67 | + return switch (returnType) { |
| 68 | + case ALL -> modelFactory.fromAny(new ProcessResult(0, toString(stdout), toString(stderr))); |
| 69 | + case NONE -> modelFactory.fromNull(); |
| 70 | + case CODE -> modelFactory.from(0); |
| 71 | + case STDOUT -> modelFactory.from(toString(stdout)); |
| 72 | + case STDERR -> modelFactory.from(toString(stderr)); |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + private String toString(ByteArrayOutputStream stream) { |
| 77 | + return stream.toString().trim(); |
| 78 | + } |
| 79 | +} |
0 commit comments