@Override public ValueExpression setVariable(String name, ValueExpression value) { ValueExpression previousValue = resolveVariable(name); scriptContext.setAttribute(name, value, ScriptContext.ENGINE_SCOPE); return previousValue; } }
private static void setContext(ScriptEngine engine, Map<String, String> groups, ITestNGMethod tm) { ScriptContext context = engine.getContext(); Method method = tm.getConstructorOrMethod().getMethod(); context.setAttribute("method", method, ScriptContext.ENGINE_SCOPE); context.setAttribute("groups", groups, ScriptContext.ENGINE_SCOPE); context.setAttribute("testngMethod", tm, ScriptContext.ENGINE_SCOPE); }
public static void importFunctions(ScriptContext ctx, String namespace, Object obj) { Class<?> clazz = null; if (obj instanceof Class) { clazz = (Class<?>) obj; } else if (obj instanceof String) { try { clazz = ReflectUtil.loadClass((String) obj); } catch (ActivitiException ae) { throw new ELException(ae); } } else { throw new ELException("Class or class name is missing"); } Method[] methods = clazz.getMethods(); for (Method m : methods) { int mod = m.getModifiers(); if (Modifier.isStatic(mod) && Modifier.isPublic(mod)) { String name = namespace + ":" + m.getName(); ctx.setAttribute(name, m, ScriptContext.ENGINE_SCOPE); } } }
scriptCtx.setAttribute("context", scriptCtx, ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("out:print", getPrintMethod(), ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("lang:import", getImportMethod(), ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("elcontext", elContext, ScriptContext.ENGINE_SCOPE); return elContext;
scriptEngine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "weak", ScriptContext.ENGINE_SCOPE); } catch (Exception ignore) {
try { ScriptContext context = engine.getContext(); context.setAttribute("page", page, ScriptContext.ENGINE_SCOPE); context.setAttribute("config", site, ScriptContext.ENGINE_SCOPE); try { switch (language) {
@Override public <T> T executeTemplate(String template, HttpRequest request, Class<? extends DTO<T>> dtoClass) { T result = null; try { Writer writer = new StringWriter(); ScriptContext context = engine.getContext(); context.setWriter(writer); context.setAttribute("request", new HttpRequestTemplateObject(request), ScriptContext.ENGINE_SCOPE); engine.eval(template); logFormatter.info(TEMPLATE_GENERATED, request, "generated output:{}from template:{}for request:{}", writer.toString(), template, request); result = httpTemplateOutputDeserializer.deserializer(request, writer.toString(), dtoClass); } catch (Exception e) { throw new RuntimeException(formatLogMessage("Exception transforming template:{}for request:{}", template, request), e); } return result; } }
protected Object evaluateExpression(Object expression, Map<String, Object> parameters) { try { Object result = null; StringWriter writer = new StringWriter(); ScriptContext context = new SimpleScriptContext(); for (Map.Entry<String, Object> property : engineProperties.entrySet()) { context.setAttribute(property.getKey(), property.getValue(), ScriptContext.ENGINE_SCOPE); } Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.putAll(parameters); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); context.setWriter(writer); if (expression instanceof CompiledScript) { logger.debug("About to evaluate compiled expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = ((CompiledScript) expression).eval(context); } else { logger.debug("About to evaluate expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = scriptEngine.eval(expression.toString(), context); } if (result == null) { result = writer.toString(); } return result; } catch (ScriptException e) { throw new RuntimeException("Error when evaluating script", e); } }
/** * Allows providing custom configuration for the groovy script engine. * @param scriptEngine the groovy script engine to configure. */ protected void configureGroovyScriptEngine(ScriptEngine scriptEngine) { // make sure Groovy compiled scripts only hold weak references to java methods scriptEngine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "weak", ScriptContext.ENGINE_SCOPE); }
@Override public ValueExpression setVariable(String name, ValueExpression value) { ValueExpression previousValue = resolveVariable(name); scriptContext.setAttribute(name, value, ScriptContext.ENGINE_SCOPE); return previousValue; } }
@Override public ValueExpression setVariable(String name, ValueExpression value) { ValueExpression previousValue = resolveVariable(name); scriptContext.setAttribute(name, value, ScriptContext.ENGINE_SCOPE); return previousValue; } }
/** * Allows providing custom configuration for the groovy script engine. * @param scriptEngine the groovy script engine to configure. */ protected void configureGroovyScriptEngine(ScriptEngine scriptEngine) { // make sure Groovy compiled scripts only hold weak references to java methods scriptEngine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "weak", ScriptContext.ENGINE_SCOPE); }
@Override public void setVariable(String name, Object value) { synchronized (context) { int scope = context.getAttributesScope(name); if (scope == -1) { scope = ScriptContext.ENGINE_SCOPE; } context.setAttribute(name, value, scope); } } };
String javaScriptExpression = "sayHello(name);"; Reader javaScriptFile = new StringReader( "function sayHello(name) {\n" + " println('Hello, '+name+'!');\n" + "}"); ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory .getEngineByName("JavaScript"); ScriptContext context = engine.getContext(); context.setAttribute("name", "JavaScript", ScriptContext.ENGINE_SCOPE); engine.eval(javaScriptFile); engine.eval(javaScriptExpression);
@Override public void setVariable(String name, Object value) { synchronized (ctx) { int scope = ctx.getAttributesScope(name); if (scope == -1) { scope = ScriptContext.ENGINE_SCOPE; } ctx.setAttribute(name, value, scope); } } };
scriptCtx.setAttribute("context", scriptCtx, ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("out:print", getPrintMethod(), ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("lang:import", getImportMethod(), ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("elcontext", elContext, ScriptContext.ENGINE_SCOPE); return elContext;
public static void importFunctions(ScriptContext ctx, String namespace, Object obj) { Class< ? > clazz = null; if (obj instanceof Class) { clazz = (Class< ? >) obj; } else if (obj instanceof String) { try { clazz = ReflectUtil.loadClass((String) obj); } catch (ProcessEngineException ae) { throw new ELException(ae); } } else { throw new ELException("Class or class name is missing"); } Method[] methods = clazz.getMethods(); for (Method m : methods) { int mod = m.getModifiers(); if (Modifier.isStatic(mod) && Modifier.isPublic(mod)) { String name = namespace + ":" + m.getName(); ctx.setAttribute(name, m, ScriptContext.ENGINE_SCOPE); } } }
scriptCtx.setAttribute("context", scriptCtx, ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("out:print", getPrintMethod(), ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("lang:import", getImportMethod(), ScriptContext.ENGINE_SCOPE); scriptCtx.setAttribute("elcontext", elContext, ScriptContext.ENGINE_SCOPE); return elContext;
public static void importFunctions(ScriptContext ctx, String namespace, Object obj) { Class< ? > clazz = null; if (obj instanceof Class) { clazz = (Class< ? >) obj; } else if (obj instanceof String) { try { clazz = ReflectUtil.loadClass((String) obj); } catch (ProcessEngineException ae) { throw new ELException(ae); } } else { throw new ELException("Class or class name is missing"); } Method[] methods = clazz.getMethods(); for (Method m : methods) { int mod = m.getModifiers(); if (Modifier.isStatic(mod) && Modifier.isPublic(mod)) { String name = namespace + ":" + m.getName(); ctx.setAttribute(name, m, ScriptContext.ENGINE_SCOPE); } } }
globalClosures.put(e.getKey(), (Closure) e.getValue()); context.setAttribute(e.getKey(), e.getValue(), ScriptContext.ENGINE_SCOPE); });