Search in sources :

Example 66 with Bindings

use of javax.script.Bindings in project sling by apache.

the class EsxScriptEngine method eval.

@Override
public Object eval(Reader reader, ScriptContext context) throws ScriptException {
    log.debug("starting to eval ESX Script");
    Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingScriptHelper scriptHelper = (SlingScriptHelper) bindings.get("sling");
    Resource scriptResource = scriptHelper.getScript().getScriptResource();
    Resource resource = scriptHelper.getRequest().getResource();
    ModuleScript moduleScript = new ModuleScript(ModuleScript.JS_FILE, scriptResource);
    //public Module (EsxScriptEngineFactory factory, Resource resource, ModuleScript moduleScript, String id, Module parent, SlingScriptHelper scriptHelper) throws ScriptException {
    Module module = new Module((EsxScriptEngineFactory) getFactory(), resource, moduleScript, scriptResource.getPath(), null, scriptHelper, Module.LOADER_JS);
    try {
        Object moduleResults = module.require(scriptResource.getPath());
        String result = ((EsxScriptEngineFactory) getFactory()).getNashornEngine().eval("if(exports.render && typeof exports.render === 'function') { exports.render('test'); }" + " else if(typeof exports === 'class') { new exports().render('function') } else {" + "'You need to define either a render function or export an object with a render method'; }", module).toString();
        context.getWriter().write(result);
    } catch (IOException ex) {
        throw new ScriptException(ex);
    }
    return null;
}
Also used : ScriptException(javax.script.ScriptException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) Bindings(javax.script.Bindings)

Example 67 with Bindings

use of javax.script.Bindings in project dubbo by alibaba.

the class ScriptRouter method route.

@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
    try {
        List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers);
        Compilable compilable = (Compilable) engine;
        Bindings bindings = engine.createBindings();
        bindings.put("invokers", invokersCopy);
        bindings.put("invocation", invocation);
        bindings.put("context", RpcContext.getContext());
        CompiledScript function = compilable.compile(rule);
        Object obj = function.eval(bindings);
        if (obj instanceof Invoker[]) {
            invokersCopy = Arrays.asList((Invoker<T>[]) obj);
        } else if (obj instanceof Object[]) {
            invokersCopy = new ArrayList<Invoker<T>>();
            for (Object inv : (Object[]) obj) {
                invokersCopy.add((Invoker<T>) inv);
            }
        } else {
            invokersCopy = (List<Invoker<T>>) obj;
        }
        return invokersCopy;
    } catch (ScriptException e) {
        // fail then ignore rule .invokers.
        logger.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
        return invokers;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) ScriptException(javax.script.ScriptException) Invoker(com.alibaba.dubbo.rpc.Invoker) Compilable(javax.script.Compilable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Bindings(javax.script.Bindings)

Example 68 with Bindings

use of javax.script.Bindings in project metrics by dropwizard.

the class PickledGraphiteTest method unpickleOutput.

private String unpickleOutput() throws Exception {
    StringBuilder results = new StringBuilder();
    // the charset is important. if the GraphitePickleReporter and this test
    // don't agree, the header is not always correctly unpacked.
    String payload = output.toString("UTF-8");
    PyList result = new PyList();
    int nextIndex = 0;
    while (nextIndex < payload.length()) {
        Bindings bindings = new SimpleBindings();
        bindings.put("payload", payload.substring(nextIndex));
        unpickleScript.eval(bindings);
        result.addAll(result.size(), (PyList) bindings.get("metrics"));
        nextIndex += (Integer) bindings.get("batchLength");
    }
    for (Object aResult : result) {
        PyTuple datapoint = (PyTuple) aResult;
        String name = datapoint.get(0).toString();
        PyTuple valueTuple = (PyTuple) datapoint.get(1);
        Object timestamp = valueTuple.get(0);
        Object value = valueTuple.get(1);
        results.append(name).append(" ").append(value).append(" ").append(timestamp).append("\n");
    }
    return results.toString();
}
Also used : SimpleBindings(javax.script.SimpleBindings) PyList(org.python.core.PyList) PyTuple(org.python.core.PyTuple) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Example 69 with Bindings

use of javax.script.Bindings in project Lucee by lucee.

the class ScriptEngineImpl method getContext.

private ScriptContext getContext(Bindings b) {
    ScriptContext def = getContext();
    SimpleScriptContext custom = new SimpleScriptContext();
    Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
    if (gs != null)
        custom.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
    custom.setBindings(b, ScriptContext.ENGINE_SCOPE);
    custom.setReader(def.getReader());
    custom.setWriter(def.getWriter());
    custom.setErrorWriter(def.getErrorWriter());
    return custom;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings)

Example 70 with Bindings

use of javax.script.Bindings in project cas by apereo.

the class ScriptingUtils method executeGroovyScriptEngine.

/**
 * Execute inline groovy script engine.
 *
 * @param <T>       the type parameter
 * @param script    the script
 * @param variables the variables
 * @param clazz     the clazz
 * @return the t
 */
public static <T> T executeGroovyScriptEngine(final String script, final Map<String, Object> variables, final Class<T> clazz) {
    try {
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("groovy");
        if (engine == null) {
            LOGGER.warn("Script engine is not available for Groovy");
            return null;
        }
        final Bindings binding = new SimpleBindings();
        if (variables != null && !variables.isEmpty()) {
            binding.putAll(variables);
        }
        if (!binding.containsKey("logger")) {
            binding.put("logger", LOGGER);
        }
        final Object result = engine.eval(script, binding);
        if (result != null && !clazz.isAssignableFrom(result.getClass())) {
            throw new ClassCastException("Result [" + result + " is of type " + result.getClass() + " when we were expecting " + clazz);
        }
        return (T) result;
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) GroovyObject(groovy.lang.GroovyObject) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) ScriptEngine(javax.script.ScriptEngine)

Aggregations

Bindings (javax.script.Bindings)150 SimpleBindings (javax.script.SimpleBindings)77 Test (org.junit.Test)36 ScriptException (javax.script.ScriptException)30 ScriptContext (javax.script.ScriptContext)26 SimpleScriptContext (javax.script.SimpleScriptContext)24 ScriptEngine (javax.script.ScriptEngine)20 SlingBindings (org.apache.sling.api.scripting.SlingBindings)18 Test (org.testng.annotations.Test)17 CompiledScript (javax.script.CompiledScript)14 Resource (org.apache.sling.api.resource.Resource)11 Map (java.util.Map)10 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 ScriptEngineManager (javax.script.ScriptEngineManager)9 PrintWriter (java.io.PrintWriter)8 StringWriter (java.io.StringWriter)8 ArrayList (java.util.ArrayList)7