public interface Script { public int computeResult(int value) { } }
public class MyGroovyScriptWrapper extends GroovyScriptWrapper<Script> { }with
Script
being the interface you want to implement in the scripting language.
executeScriptImpl()
method. For example, for the script defined in the use case:public class MethodWrapperImpl extends AbstractScriptMethodWrapper<Script, Integer> { public MethodWrapperImpl(ScriptWrapper<Script> wrapper) { super(wrapper); } protected Integer executeScriptImpl(Object... arguments) { Integer i = (Integer)arguments[0]; return script.computeResult(i); } }
public class MethodWrapperImpl extends AbstractScriptMethodWrapper<Script, Integer> { public MethodWrapperImpl(ScriptWrapper<Script> wrapper) { super(wrapper); checkDeclaredArguments("computeResult", Integer.class); } protected Integer executeScriptImpl(Object... arguments) { Integer i = (Integer)arguments[0]; return script.computeResult(i); } }
ScriptWrapper<Script> wrapper = new MyGroovyScriptWrapper(); ScriptMethodWrapper<Script, Integer> methodWrapper = new MethodWrapperImpl(wrapper);And install the script:
File file = new File(<our script file>); wrapper.installScript(file);and:
int value = methodWrapper.executeMethod(10);
public int computeResult(int value) { return value * 10; }You don't need to specify that this groovy script implements the
Script
interface. The framework will take care of that.
ScriptWrapper
if you want to change to script you use. Just call again the ScriptWrapper.installScript(File) method. For example this will work:wrapper = new MyGroovyScriptWrapper(); ScriptMethodProxy<Script, Integer> methodProxy = new MethodWrapperImpl(wrapper); File file = new File(<our first script file>); wrapper.installScript(file); int value = methodWrapper.executeMethod(10); file = new File(<our second script file>); wrapper.installScript(file); int value = methodWrapper.executeMethod(10);
Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence