Example usage for javax.script Bindings put

List of usage examples for javax.script Bindings put

Introduction

In this page you can find the example usage for javax.script Bindings put.

Prototype

public Object put(String name, Object value);

Source Link

Document

Set a named value.

Usage

From source file:com.qspin.qtaste.testsuite.impl.JythonTestScript.java

public static void addToGlobalJythonScope(String name, Object value) {
    Bindings globalContext = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    globalContext.put(name, value);
}

From source file:com.intuit.karate.Script.java

public static ScriptValue evalInNashorn(String exp, ScriptContext context, ScriptValue selfValue,
        ScriptValue parentValue) {/*from ww w .j av  a2 s  .c  o  m*/
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine nashorn = manager.getEngineByName("nashorn");
    Bindings bindings = nashorn.getBindings(javax.script.ScriptContext.ENGINE_SCOPE);
    if (context != null) {
        Map<String, Object> map = context.getVariableBindings();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            bindings.put(entry.getKey(), entry.getValue());
        }
        bindings.put(ScriptContext.KARATE_NAME, new ScriptBridge(context));
    }
    if (selfValue != null) {
        bindings.put(VAR_SELF, selfValue.getValue());
    }
    if (parentValue != null) {
        bindings.put(VAR_DOLLAR, parentValue.getAfterConvertingFromJsonOrXmlIfNeeded());
    }
    try {
        Object o = nashorn.eval(exp);
        ScriptValue result = new ScriptValue(o);
        logger.trace("nashorn returned: {}", result);
        return result;
    } catch (Exception e) {
        throw new RuntimeException("script failed: " + exp, e);
    }
}

From source file:org.apache.tinkerpop.gremlin.hadoop.structure.io.script.ScriptRecordReader.java

@Override
public boolean nextKeyValue() throws IOException {
    while (true) {
        if (!this.lineRecordReader.nextKeyValue())
            return false;
        try {/*from  w w  w  . j av  a  2 s.co m*/
            final Bindings bindings = this.engine.createBindings();
            final StarGraph graph = StarGraph.open();
            final ScriptElementFactory factory = new ScriptElementFactory(graph);
            bindings.put(GRAPH, graph);
            bindings.put(LINE, this.lineRecordReader.getCurrentValue().toString());
            bindings.put(FACTORY, factory);
            final StarGraph.StarVertex sv = (StarGraph.StarVertex) engine.eval(this.parse, bindings);
            if (sv != null) {
                final Optional<StarGraph.StarVertex> vertex = sv.applyGraphFilter(this.graphFilter);
                if (vertex.isPresent()) {
                    this.vertexWritable.set(vertex.get());
                    return true;
                }
            }
        } catch (final ScriptException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
}

From source file:prototypes.ws.proxy.soap.configuration.BooleanExecutableExpression.java

/**
 *
 * @param o//from  www.java2  s .  c o m
 * @return
 */
public Boolean execute(Object o) {
    if (o != null) {
        try {
            Bindings bindings = engine.createBindings();
            Map<String, Method> fieldMethods = resolveGetters(o.getClass());
            for (Map.Entry<String, Method> entry : fieldMethods.entrySet()) {
                try {
                    bindings.put(entry.getKey(), entry.getValue().invoke(o, new Object[0]));
                } catch (InvocationTargetException ex) {
                    logger.warn(Messages.MSG_ERROR_DETAILS, ex);
                    throw new IllegalArgumentException("Error with given object class '" + o.getClass() + "'.",
                            ex);
                } catch (IllegalAccessException ex) {
                    logger.warn(Messages.MSG_ERROR_DETAILS, ex);
                    throw new IllegalArgumentException("Error with given object class '" + o.getClass() + "'.",
                            ex);
                }
            }
            Object result;
            if (script != null) {
                result = script.eval(bindings);
            } else {
                result = engine.eval(body, bindings);
            }
            return (Boolean) result;
        } catch (ScriptException ex) {
            logger.warn("Error on script", ex);
        } catch (java.lang.IllegalArgumentException ex) {
            logger.warn("Error on script execution", ex);
        }
    }
    return null;
}

From source file:org.apache.sling.scripting.sightly.js.impl.JsEnvironment.java

private Bindings buildBindings(Resource scriptResource, Bindings local, Bindings arguments,
        CommonJsModule commonJsModule) {
    Bindings bindings = new SimpleBindings();
    bindings.putAll(engineBindings);// ww  w  . j a v  a 2 s . c  o  m
    DependencyResolver dependencyResolver = new DependencyResolver(scriptResource, this, local);
    UseFunction useFunction = new UseFunction(dependencyResolver, arguments);
    bindings.put(Variables.JS_USE, useFunction);
    bindings.put(Variables.MODULE, commonJsModule);
    bindings.put(Variables.EXPORTS, commonJsModule.getExports());
    bindings.put(Variables.CONSOLE, new Console(LoggerFactory.getLogger(scriptResource.getName())));
    bindings.putAll(local);
    return bindings;
}

From source file:com.adobe.cq.wcm.core.components.testing.WCMUsePojoBaseTest.java

/**
 * <p>// w w  w .j a v a 2  s . com
 * Creates a {@link Bindings} map initialised with the following default bindings available to Sightly use objects based on {@link
 * WCMUsePojo}:
 * </p>
 * <ul>
 * <li>{@link SlingBindings#RESOURCE}</li>
 * <li>{@link SlingBindings#REQUEST}</li>
 * <li>{@link SlingBindings#RESPONSE}</li>
 * <li>{@link WCMBindings#PROPERTIES}</li>
 * <li>{@link WCMBindings#WCM_MODE}</li>
 * <li>{@link WCMBindings#PAGE_MANAGER}</li>
 * <li>{@link WCMBindings#RESOURCE_PAGE}</li>
 * <li>{@link WCMBindings#CURRENT_PAGE}</li>
 * <li>{@link WCMBindings#PAGE_PROPERTIES}</li>
 * </ul>
 *
 * @param resourcePath the path to a resource already loaded in the testing context
 * @return the bindings map
 */
protected Bindings getResourceBackedBindings(String resourcePath) {
    Bindings bindings = getDefaultSlingBindings();
    Resource resource = context.resourceResolver().getResource(resourcePath);
    if (resource != null) {
        ValueMap properties = resource.adaptTo(ValueMap.class);
        bindings.put(SlingBindings.RESOURCE, resource);
        bindings.put(WCMBindings.PROPERTIES, properties);
        bindings.put(WCMBindings.WCM_MODE, new SightlyWCMMode(context.request()));
        PageManager pageManager = context.pageManager();
        bindings.put(WCMBindings.PAGE_MANAGER, pageManager);

        context.request().setResource(resource);
        Page resourcePage = pageManager.getContainingPage(resource);
        if (resourcePage != null) {
            bindings.put(WCMBindings.RESOURCE_PAGE, resourcePage);
            bindings.put(WCMBindings.CURRENT_PAGE, resourcePage);
            bindings.put(WCMBindings.PAGE_PROPERTIES, properties);
        }
    } else {
        throw new IllegalArgumentException("Cannot find a resource at " + resourcePath);
    }
    return bindings;
}

From source file:org.apache.sling.scripting.sightly.js.impl.jsapi.ProxyAsyncScriptableFactory.java

public void registerProxies(Bindings bindings) {
    slyBindingsValuesProvider.initialise(bindings);
    Bindings bindingsCopy = new SimpleBindings();
    for (String factoryName : slyBindingsValuesProvider.getScriptPaths().keySet()) {
        ShadowScriptableObject shadowScriptableObject = new ShadowScriptableObject(factoryName, bindingsCopy);
        bindings.put(factoryName, shadowScriptableObject);
    }/*from  ww w.ja va  2  s  . c  om*/
    bindingsCopy.putAll(bindings);
}

From source file:org.mule.module.scripting.component.Scriptable.java

private void populateHeadersVariablesAndException(Bindings bindings, MuleMessage message) {
    bindings.put("flowVars", new MesssagePropertyMap(message, PropertyScope.INVOCATION));
    bindings.put("sessionVars", new MesssagePropertyMap(message, PropertyScope.SESSION));

    // Only add exception is present
    if (message.getExceptionPayload() != null) {
        bindings.put("exception", message.getExceptionPayload().getException());
    } else {/*  w w  w.  ja va  2 s  .co m*/
        bindings.put("exception", null);
    }
}

From source file:org.labkey.nashorn.NashornController.java

private Pair<ScriptEngine, ScriptContext> getNashorn(boolean useSession) throws Exception {
    ScriptEngineManager engineManager = new ScriptEngineManager();
    ScriptEngine engine;/*w ww.  j a  va2 s  .c om*/
    ScriptContext context;

    Callable<ScriptEngine> createContext = new Callable<ScriptEngine>() {
        @Override
        @NotNull
        public ScriptEngine call() throws Exception {
            NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
            ScriptEngine engine = factory.getScriptEngine(new String[] { "--global-per-engine" });
            return engine;
        }
    };

    if (useSession) {
        HttpServletRequest req = getViewContext().getRequest();
        engine = SessionHelper.getAttribute(req, this.getClass().getName() + "#scriptEngine", createContext);
    } else {
        engine = createContext.call();
    }

    Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    engineScope.put("LABKEY", new org.labkey.nashorn.env.LABKEY(getViewContext()));

    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    // null==engine.getBindings(ScriptContext.GLOBAL_SCOPE), because of --global-per-engine
    // some docs mention enginScope.get("nashorn.global"), but that is also null
    if (null == globalScope)
        globalScope = (Bindings) engineScope.get("nashorn.global");
    if (null == globalScope)
        globalScope = engineScope;
    globalScope.put("console", new Console());

    return new Pair<>(engine, engine.getContext());
}

From source file:org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilderFactory.java

public ScriptEngine createEngine(AuthenticationContext authenticationContext) {

    ScriptEngine engine = factory.getScriptEngine("--no-java");

    Bindings bindings = engine.createBindings();
    engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
    engine.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    SelectAcrFromFunction selectAcrFromFunction = new SelectAcrFromFunction();
    //        todo move to functions registry
    bindings.put(FrameworkConstants.JSAttributes.JS_FUNC_SELECT_ACR_FROM,
            (SelectOneFunction) selectAcrFromFunction::evaluate);

    JsLogger jsLogger = new JsLogger();
    bindings.put(FrameworkConstants.JSAttributes.JS_LOG, jsLogger);
    return engine;
}