Java Code Examples for javax.script.ScriptEngine#setBindings()
The following examples show how to use
javax.script.ScriptEngine#setBindings() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebDriverSampler.java From jmeter-plugins-webdriver with Apache License 2.0 | 6 votes |
ScriptEngine createScriptEngineWith(SampleResult sampleResult) { final ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(this.getScriptLanguage()); Bindings engineBindings = new SimpleBindings(); WebDriverScriptable scriptable = new WebDriverScriptable(); scriptable.setName(getName()); scriptable.setParameters(getParameters()); JMeterContext context = JMeterContextService.getContext(); scriptable.setVars(context.getVariables()); scriptable.setProps(JMeterUtils.getJMeterProperties()); scriptable.setCtx(context); scriptable.setLog(LOGGER); scriptable.setSampleResult(sampleResult); scriptable.setBrowser(getWebDriver()); engineBindings.put("WDS", scriptable); scriptEngine.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE); return scriptEngine; }
Example 2
Source File: Test3.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { System.out.println("\nTest3\n"); final Reader reader = new FileReader( new File(System.getProperty("test.src", "."), "Test3.js")); ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine engine = Helper.getJsEngine(m); if (engine == null) { System.out.println("Warning: No js engine found; test vacuously passes."); return; } Bindings en = new SimpleBindings(); engine.setBindings(en, ScriptContext.ENGINE_SCOPE); en.put("key", "engine value"); Bindings gn = new SimpleBindings(); engine.setBindings(gn, ScriptContext.GLOBAL_SCOPE); gn.put("key", "global value"); engine.eval(reader); }
Example 3
Source File: JsTestUtils.java From grammaticus with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * @return a script engine with an appropriate global context for $Api so that we don't have to revaluate the functions constantly * * If you want to add stuff to bindings while running tests, just comment out the if and the closing brace */ public static ScriptEngine getScriptEngine() { Bindings bindings = BINDINGS.get(); if (bindings == null) { // Create an engine just to compile the $AP ScriptEngine compEngine = new ScriptEngineManager().getEngineByName("JavaScript"); compEngine.put("out", System.out); try { bindings = compEngine.getBindings(ScriptContext.GLOBAL_SCOPE); bindings.put("Grammaticus", compEngine.eval(Resources.toString(JsTestUtils.class.getResource("grammaticus.js"), Charsets.UTF_8))); BINDINGS.set(bindings); } catch (ScriptException | IOException x) { x.printStackTrace(); } } ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE); return engine; }
Example 4
Source File: JsGraphBuilderFactory.java From carbon-identity-framework with Apache License 2.0 | 6 votes |
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; }
Example 5
Source File: SoapUIScriptEngineBinder.java From microcks with Apache License 2.0 | 6 votes |
/** * Create and bind a SoapUI environment for a ScriptEngine. * @param engine The engine to enrich with binding environment. * @param requestContent The content of request to use as data * @param request The wrapped incoming servlet request. */ public static void bindSoapUIEnvironment(ScriptEngine engine, String requestContent, HttpServletRequest request){ // Build a map of header values. StringToStringsMap headers = new StringToStringsMap(); for (String headerName : Collections.list(request.getHeaderNames())) { headers.put(headerName, Collections.list(request.getHeaders(headerName))); } // Build a fake request container. FakeSoapUIMockRequest mockRequest = new FakeSoapUIMockRequest(requestContent, headers); mockRequest.setRequest(request); // Create bindings and put content according to SoapUI binding environment. Bindings bindings = engine.createBindings(); bindings.put("mockRequest", mockRequest); bindings.put("log", log); engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); }
Example 6
Source File: StandardScriptUtils.java From spring-analysis-note with MIT License | 5 votes |
/** * Retrieve a {@link ScriptEngine} from the given {@link ScriptEngineManager} * by name, delegating to {@link ScriptEngineManager#getEngineByName} but * throwing a descriptive exception if not found or if initialization failed. * @param scriptEngineManager the ScriptEngineManager to use * @param engineName the name of the engine * @return a corresponding ScriptEngine (never {@code null}) * @throws IllegalArgumentException if no matching engine has been found * @throws IllegalStateException if the desired engine failed to initialize */ public static ScriptEngine retrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName) { ScriptEngine engine = scriptEngineManager.getEngineByName(engineName); if (engine == null) { Set<String> engineNames = new LinkedHashSet<>(); for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) { List<String> factoryNames = engineFactory.getNames(); if (factoryNames.contains(engineName)) { // Special case: getEngineByName returned null but engine is present... // Let's assume it failed to initialize (which ScriptEngineManager silently swallows). // If it happens to initialize fine now, alright, but we really expect an exception. try { engine = engineFactory.getScriptEngine(); engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE); } catch (Throwable ex) { throw new IllegalStateException("Script engine with name '" + engineName + "' failed to initialize", ex); } } engineNames.addAll(factoryNames); } throw new IllegalArgumentException("Script engine with name '" + engineName + "' not found; registered engine names: " + engineNames); } return engine; }
Example 7
Source File: StandardScriptUtils.java From java-technology-stack with MIT License | 5 votes |
/** * Retrieve a {@link ScriptEngine} from the given {@link ScriptEngineManager} * by name, delegating to {@link ScriptEngineManager#getEngineByName} but * throwing a descriptive exception if not found or if initialization failed. * @param scriptEngineManager the ScriptEngineManager to use * @param engineName the name of the engine * @return a corresponding ScriptEngine (never {@code null}) * @throws IllegalArgumentException if no matching engine has been found * @throws IllegalStateException if the desired engine failed to initialize */ public static ScriptEngine retrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName) { ScriptEngine engine = scriptEngineManager.getEngineByName(engineName); if (engine == null) { Set<String> engineNames = new LinkedHashSet<>(); for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) { List<String> factoryNames = engineFactory.getNames(); if (factoryNames.contains(engineName)) { // Special case: getEngineByName returned null but engine is present... // Let's assume it failed to initialize (which ScriptEngineManager silently swallows). // If it happens to initialize fine now, alright, but we really expect an exception. try { engine = engineFactory.getScriptEngine(); engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE); } catch (Throwable ex) { throw new IllegalStateException("Script engine with name '" + engineName + "' failed to initialize", ex); } } engineNames.addAll(factoryNames); } throw new IllegalArgumentException("Script engine with name '" + engineName + "' not found; registered engine names: " + engineNames); } return engine; }
Example 8
Source File: StandardScriptUtils.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Retrieve a {@link ScriptEngine} from the given {@link ScriptEngineManager} * by name, delegating to {@link ScriptEngineManager#getEngineByName} but * throwing a descriptive exception if not found or if initialization failed. * @param scriptEngineManager the ScriptEngineManager to use * @param engineName the name of the engine * @return a corresponding ScriptEngine (never {@code null}) * @throws IllegalArgumentException if no matching engine has been found * @throws IllegalStateException if the desired engine failed to initialize */ public static ScriptEngine retrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName) { ScriptEngine engine = scriptEngineManager.getEngineByName(engineName); if (engine == null) { Set<String> engineNames = new LinkedHashSet<String>(); for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) { List<String> factoryNames = engineFactory.getNames(); if (factoryNames.contains(engineName)) { // Special case: getEngineByName returned null but engine is present... // Let's assume it failed to initialize (which ScriptEngineManager silently swallows). // If it happens to initialize fine now, alright, but we really expect an exception. try { engine = engineFactory.getScriptEngine(); engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE); } catch (Throwable ex) { throw new IllegalStateException("Script engine with name '" + engineName + "' failed to initialize", ex); } } engineNames.addAll(factoryNames); } throw new IllegalArgumentException("Script engine with name '" + engineName + "' not found; registered engine names: " + engineNames); } return engine; }
Example 9
Source File: StandardScriptUtils.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Retrieve a {@link ScriptEngine} from the given {@link ScriptEngineManager} * by name, delegating to {@link ScriptEngineManager#getEngineByName} but * throwing a descriptive exception if not found or if initialization failed. * @param scriptEngineManager the ScriptEngineManager to use * @param engineName the name of the engine * @return a corresponding ScriptEngine (never {@code null}) * @throws IllegalArgumentException if no matching engine has been found * @throws IllegalStateException if the desired engine failed to initialize */ public static ScriptEngine retrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName) { ScriptEngine engine = scriptEngineManager.getEngineByName(engineName); if (engine == null) { Set<String> engineNames = new LinkedHashSet<String>(); for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) { List<String> factoryNames = engineFactory.getNames(); if (factoryNames.contains(engineName)) { // Special case: getEngineByName returned null but engine is present... // Let's assume it failed to initialize (which ScriptEngineManager silently swallows). // If it happens to initialize fine now, alright, but we really expect an exception. try { engine = engineFactory.getScriptEngine(); engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE); } catch (Throwable ex) { throw new IllegalStateException("Script engine with name '" + engineName + "' failed to initialize", ex); } } engineNames.addAll(factoryNames); } throw new IllegalArgumentException("Script engine with name '" + engineName + "' not found; registered engine names: " + engineNames); } return engine; }
Example 10
Source File: ConsoleLogger.java From rug-cli with GNU General Public License v3.0 | 5 votes |
public static AbstractFunction1<ScriptEngine, BoxedUnit> consoleLogger(String indent, boolean enabled) { return new AbstractFunction1<ScriptEngine, BoxedUnit>() { @Override public BoxedUnit apply(ScriptEngine engine) { Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("console", new ConsoleLogger(indent, enabled)); engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); return null; } }; }
Example 11
Source File: AbstractEvaluatableScriptAdapter.java From keycloak with Apache License 2.0 | 5 votes |
/** * Note, calling this method modifies the underlying {@link ScriptEngine}, * preventing concurrent use of the ScriptEngine (Nashorn's {@link ScriptEngine} and * {@link javax.script.CompiledScript} is thread-safe, but {@link Bindings} isn't). */ InvocableScriptAdapter prepareInvokableScript(final ScriptBindingsConfigurer bindingsConfigurer) { final Bindings bindings = createBindings(bindingsConfigurer); evalUnchecked(bindings); final ScriptEngine engine = getEngine(); engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); return new InvocableScriptAdapter(scriptModel, engine); }
Example 12
Source File: ScriptLanguageFixture.java From hsac-fitnesse-fixtures with Apache License 2.0 | 4 votes |
@Override public void clearValues() { ScriptEngine e = getEngine(); e.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE); super.clearValues(); }