private ScriptEngine getEngine(ScriptTask t) throws ExecutionException { if (t.getLanguage() != null) { return scriptEngineManager.getEngineByName(t.getLanguage()); } if (t.getContent() == null) { throw new ExecutionException("Script task must have a language set or a path to an external script: " + t.getId()); } String ext = getExtension(t.getContent()); if (ext == null) { throw new ExecutionException("Unknown external script extension: " + t.getContent()); } return scriptEngineManager.getEngineByExtension(ext); }
throw new ExecutionException("Script engine not found: " + t.getLanguage()); Variables vars = VariablesHelper.applyInVariables(contextFactory, state.getVariables(), t.getIn(), t.isCopyAllVariables()); ctx = contextFactory.create(vars, a.getDefinitionId(), a.getElementId()); state = VariablesHelper.applyOutVariables(contextFactory, state, ctx, t.getOut());
@Override default T script(ScriptTask.Type type, String language, String content, boolean copyAllVariables) { return add(new ScriptTask(nextStepId(), type, language, content, copyAllVariables)); }
@Override public String toString() { return "ScriptTask (" + getId() + ") {" + "type=" + type + ", language='" + language + '\'' + ", in=" + in + ", out=" + out + ", content='" + content + '\'' + ", copyAllVariables='" + copyAllVariables + '\'' + '}'; } }
private Reader openReader(ScriptTask t) throws ExecutionException { Type type = t.getType(); if (type == Type.REFERENCE) { String ref = t.getContent(); try { InputStream in = resourceResolver.getResourceAsStream(ref); if (in == null) { throw new ExecutionException("Resource not found: " + ref); } return new InputStreamReader(in); } catch (IOException e) { throw new ExecutionException("Can't open resource: " + ref); } } else if (type == Type.CONTENT) { return new StringReader(t.getContent()); } else { throw new ExecutionException("Unsupported script task type: " + type); } }
@Override public Chunk convert(ConverterContext ctx, YamlScript s) throws YamlConverterException { Chunk c = new Chunk(); String id = ctx.nextId(); switch (s.getType()) { case CONTENT: { c.addElement(new ScriptTask(id, s.getType(), s.getLanguage(), s.getBody(), true)); break; } case REFERENCE: { c.addElement(new ScriptTask(id, s.getType(), null, s.getBody(), true)); break; } default: throw new YamlConverterException("Unsupported script task type: " + s.getType()); } c.addOutput(id); c.addSourceMap(id, toSourceMap(s, "Script")); return c; } }