You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nifi.apache.org by Mike Thomsen <mi...@gmail.com> on 2018/02/15 14:13:21 UTC

Threading issues w/ the script bundle

I know there were some tricky threading issues with the scripting bundle,
so before I even go too far into trying another attempt at Kotlin support I
wanted to see if this looks like a sane approach based on those issues:

public class KotlinScriptEngineConfigurator extends
AbstractModuleClassloaderConfigurator {
    @Override
    public String getScriptEngineName() {
        return "kotlin";
    }

    private ScriptEngine scriptEngine;

    @Override
    public Object init(ScriptEngine engine, String[] modulePaths) throws
ScriptException {
        scriptEngine = engine;
        compiledForm = null;
        return scriptEngine;
    }

    private String currentScript;
    private CompiledScript compiledForm;

    private synchronized void compile(String scriptBody) {
        try {
            compiledForm = ((Compilable)scriptEngine).compile(scriptBody);
            currentScript = scriptBody;
        } catch (ScriptException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object eval(ScriptEngine engine, String scriptBody, String[]
modulePaths) throws ScriptException {
        if (currentScript == null || compiledForm == null ||
!currentScript.equals(scriptBody)) {
            compile(scriptBody);
        }
        return compiledForm.eval();
    }
}