@Test public void testGroovyScriptFromStringUsingJsr223() { StandardScriptEvaluator evaluator = new StandardScriptEvaluator(); evaluator.setLanguage("Groovy"); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2")); assertEquals(6, result); }
@Override public void afterPropertiesSet() throws Exception { scriptEvaluator = new StandardScriptEvaluator(); Assert.state(scriptSource != null || script != null, "Either the script source or script file must be provided"); Assert.state(scriptSource == null || script == null, "Either a script source or script file must be provided, not both"); if (scriptSource != null) { Assert.isTrue(!StringUtils.isEmpty(language), "Language must be provided when using script source"); ((StandardScriptEvaluator) scriptEvaluator).setLanguage(language); } }
@Override @Nullable public Object evaluate(ScriptSource script) { return evaluate(script, null); }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> argumentBindings) { ScriptEngine engine = getScriptEngine(script); try { if (CollectionUtils.isEmpty(argumentBindings)) { return engine.eval(script.getScriptAsString()); } else { Bindings bindings = StandardScriptUtils.getBindings(argumentBindings); return engine.eval(script.getScriptAsString(), bindings); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access script for ScriptEngine", ex); } catch (ScriptException ex) { throw new ScriptCompilationException(script, new StandardScriptEvalException(ex)); } }
@Test public void testGroovyScriptFromFileUsingJsr223() { ScriptEvaluator evaluator = new StandardScriptEvaluator(); Object result = evaluator.evaluate(new ResourceScriptSource(new ClassPathResource("simple.groovy", getClass()))); assertEquals(6, result); }
@Override public void afterPropertiesSet() throws Exception { scriptEvaluator = new StandardScriptEvaluator(); Assert.state(scriptSource != null || script != null, "Either the script source or script file must be provided"); Assert.state(scriptSource == null || script == null, "Either a script source or script file must be provided, not both"); if (scriptSource != null) { Assert.isTrue(!StringUtils.isEmpty(language), "Language must be provided when using script source"); ((StandardScriptEvaluator) scriptEvaluator).setLanguage(language); } }
@Override @Nullable public Object evaluate(ScriptSource script) { return evaluate(script, null); }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> argumentBindings) { ScriptEngine engine = getScriptEngine(script); try { if (CollectionUtils.isEmpty(argumentBindings)) { return engine.eval(script.getScriptAsString()); } else { Bindings bindings = StandardScriptUtils.getBindings(argumentBindings); return engine.eval(script.getScriptAsString(), bindings); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access script for ScriptEngine", ex); } catch (ScriptException ex) { throw new ScriptCompilationException(script, new StandardScriptEvalException(ex)); } }
@Test public void testGroovyScriptWithArgumentsUsingJsr223() { StandardScriptEvaluator evaluator = new StandardScriptEvaluator(); evaluator.setLanguage("Groovy"); Map<String, Object> arguments = new HashMap<>(); arguments.put("a", 3); arguments.put("b", 2); Object result = evaluator.evaluate(new StaticScriptSource("return a * b"), arguments); assertEquals(6, result); }
@Override @Nullable public Object evaluate(ScriptSource script) { return evaluate(script, null); }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> argumentBindings) { ScriptEngine engine = getScriptEngine(script); try { if (CollectionUtils.isEmpty(argumentBindings)) { return engine.eval(script.getScriptAsString()); } else { Bindings bindings = StandardScriptUtils.getBindings(argumentBindings); return engine.eval(script.getScriptAsString(), bindings); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access script for ScriptEngine", ex); } catch (ScriptException ex) { throw new ScriptCompilationException(script, new StandardScriptEvalException(ex)); } }