public Object call() { try { return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding); } catch (ResourceException e) { throw new RuntimeException(e); } catch (ScriptException e) { throw new RuntimeException(e); } }
/** * Run a script identified by name with a single argument. * * @param scriptName name of the script to run * @param argument a single argument passed as a variable named <code>arg</code> in the binding * @return a <code>toString()</code> representation of the result of the execution of the script * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public String run(String scriptName, String argument) throws ResourceException, ScriptException { Binding binding = new Binding(); binding.setVariable("arg", argument); Object result = run(scriptName, binding); return result == null ? "" : result.toString(); }
/** * Simple testing harness for the GSE. Enter script roots as arguments and * then input script names to run them. * * @param urls an array of URLs * @throws Exception if something goes wrong */ public static void main(String[] urls) throws Exception { GroovyScriptEngine gse = new GroovyScriptEngine(urls); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while (true) { System.out.print("groovy> "); if ((line = br.readLine()) == null || line.equals("quit")) { break; } try { System.out.println(gse.run(line, new Binding())); } catch (Exception e) { e.printStackTrace(); } } }
@Override public void run() { try { Binding b = new Binding(); b.setVariable("number", count); result = (String) this.gse.run(script, b); } catch (Throwable t) { throw new RuntimeException("problem running script", t); } }
public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException { // @todo refactor this path File currentDir = new File("./src/test/groovy/bugs"); String file = "bug1567_script.groovy"; Binding binding = new Binding(); GroovyShell shell = new GroovyShell(binding); String[] test = null; Object result = shell.run(new File(currentDir, file), test); String[] roots = new String[]{currentDir.getAbsolutePath()}; GroovyScriptEngine gse = new GroovyScriptEngine(roots); binding = new Binding(); // a MME was ensued here stating no 't.start()' was available // in the script gse.run(file, binding); } }
@Override public void executeScriptByPath(String scriptPath, Map<Object, Object> context) { try { String scriptName = PathUtil.fileName(scriptPath); String scriptDir = PathUtil.parentFolder(scriptPath); GroovyScriptEngine gse = new GroovyScriptEngine(scriptDir); Binding binding = new Binding(context); gse.run(scriptName, binding); } catch (Exception e) { throw new CloudRuntimeException(e); } } }
public Object call() { try { return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding); } catch (ResourceException e) { throw new RuntimeException(e); } catch (ScriptException e) { throw new RuntimeException(e); } }
public Object call() { try { return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding); } catch (ResourceException | ScriptException e) { throw new RuntimeException(e); } }
public Object call() { try { return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding); } catch (ResourceException e) { throw new RuntimeException(e); } catch (ScriptException e) { throw new RuntimeException(e); } }
public Object call() { try { return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding); } catch (ResourceException e) { throw new RuntimeException(e); } catch (ScriptException e) { throw new RuntimeException(e); } }
public Object call() { try { return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding); } catch (ResourceException e) { throw new RuntimeException(e); } catch (ScriptException e) { throw new RuntimeException(e); } }
/** * Run a script identified by name with a single argument. * * @param scriptName name of the script to run * @param argument a single argument passed as a variable named <code>arg</code> in the binding * @return a <code>toString()</code> representation of the result of the execution of the script * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public String run(String scriptName, String argument) throws ResourceException, ScriptException { Binding binding = new Binding(); binding.setVariable("arg", argument); Object result = run(scriptName, binding); return result == null ? "" : result.toString(); }
GroovyScriptEngine gse = new GroovyScriptEngine( "C:/Eclipse/work/S/src/com/test" ) gse.run( "Hello.groovy", binding )
GroovyScriptEngine gse = new GroovyScriptEngine( "C:/Eclipse/work/S/src" ) gse.run( "com/test/Hello.groovy", binding )
GroovyScriptEngine gse = new GroovyScriptEngine(roots); Binding binding = new Binding(); binding.setVariable("variableTextField", textField); gse.run("gview.groovy", binding);
String script = "full path to the script" GroovyScriptEngine gse = new GroovyScriptEngine() Binding binding = new Binding(); Object result = gse.run(script, binding)
@SuppressWarnings("unchecked") private List<Map<String, String>> runScript() throws Exception { Map<String, Object> gContext = new HashMap<>(); gContext.put(ScriptUtil.PARAMETERS_KEY, params); engine.run("framework/common/groovyScripts/GetLocaleList.groovy", GroovyUtil.getBinding(gContext)); return (List<Map<String, String>>) gContext.get("locales"); }
import groovy.lang.Binding; import groovy.util.GroovyScriptEngine; import groovy.util.ResourceException ; import groovy.util.ScriptException ; import java.io.IOException ; public class Test { public static void main( String[] args ) throws IOException, ResourceException, ScriptException { String[] roots = new String[] { "." }; GroovyScriptEngine gse = new GroovyScriptEngine(roots); Binding binding = new Binding(); binding.setVariable("input", "world"); gse.run("test.groovy", binding); System.out.println(binding.getVariable("output")); } }
public static void main(String[] args) { GroovyScriptEngine gse = new GroovyScriptEngine(new String[] {"/home/user/tmp"}); Binding varSet = new Binding(); varSet.setVariable("testVar", "Hello World"); varSet.setVariable("args", null); //null, empty string, whatever evaluates to false in groovy gse.run("printHello.groovy", varSet); }
@Override public Object run(String scriptPath, Binding binding) throws SiteWhereException { try { return getGroovyScriptEngine().run(scriptPath, binding); } catch (ResourceException e) { throw new SiteWhereException("Unable to access Groovy script.", e); } catch (ScriptException e) { throw new SiteWhereException("Unable to run Groovy script.", e); } catch (CompilationFailedException e) { throw new SiteWhereException("Error compiling Groovy script.", e); } catch (Throwable e) { throw new SiteWhereException("Unhandled exception in Groovy script.", e); } }