Search in sources :

Example 71 with Bindings

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

the class ScriptLoginModule method login.

@Override
public boolean login() throws LoginException {
    File script = getScriptFile((String) this.options.get("scriptURI"));
    if (script == null) {
        script = getScriptFile(JavaSecurityManagers.getSystemProperty("openejb.ScriptLoginModule.scriptURI"));
        if (script == null) {
            script = getScriptFile(null);
        }
    }
    if (script == null) {
        throw new LoginException("No login script defined");
    }
    final String scriptText;
    try {
        scriptText = new Scanner(script).useDelimiter("\\Z").next();
    } catch (final FileNotFoundException e) {
        throw new LoginException("Invalid login script URI.");
    }
    this.userData = getUserData();
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName((String) this.options.get("engineName"));
    // new context for the execution of this script
    final ScriptContext newContext = new SimpleScriptContext();
    // creating the bidings object for the current execution
    final Bindings bindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("user", this.userData.user);
    bindings.put("password", this.userData.pass);
    final List<String> myGroups;
    try {
        myGroups = (List) engine.eval(scriptText, newContext);
    } catch (final ScriptException e) {
        throw new LoginException("Cannot execute login script. Msg: " + e.getMessage());
    }
    this.userData.groups.addAll(myGroups);
    return true;
}
Also used : Scanner(java.util.Scanner) ScriptException(javax.script.ScriptException) SimpleScriptContext(javax.script.SimpleScriptContext) FileNotFoundException(java.io.FileNotFoundException) ScriptEngineManager(javax.script.ScriptEngineManager) LoginException(javax.security.auth.login.LoginException) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) File(java.io.File) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 72 with Bindings

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

the class OpenEJBScripter method bindLocal.

private static void bindLocal(final ScriptContext context) {
    final Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("bm", new BeanManagerHelper());
}
Also used : Bindings(javax.script.Bindings)

Example 73 with Bindings

use of javax.script.Bindings in project hazelcast by hazelcast.

the class HazelcastOSGiScriptEngineManagerTest method putAndGetOverBindingsSuccessfully.

@Test
public void putAndGetOverBindingsSuccessfully() {
    ScriptEngineManager scriptEngineManager = ScriptEngineManagerContext.getScriptEngineManager();
    Bindings bindings = scriptEngineManager.getBindings();
    assertNull(bindings.get("my-key"));
    bindings.put("my-key", "my-value");
    assertEquals("my-value", bindings.get("my-key"));
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) Bindings(javax.script.Bindings) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 74 with Bindings

use of javax.script.Bindings in project hazelcast by hazelcast.

the class HazelcastOSGiScriptEngineTest method verifyThatBindingsPutAndGetOverBindingsSuccessfully.

private void verifyThatBindingsPutAndGetOverBindingsSuccessfully(ScriptEngine scriptEngine) {
    Bindings bindings = scriptEngine.createBindings();
    assertNull(bindings.get("my-key"));
    bindings.put("my-key", "my-value");
    assertEquals("my-value", bindings.get("my-key"));
}
Also used : Bindings(javax.script.Bindings)

Example 75 with Bindings

use of javax.script.Bindings in project hazelcast by hazelcast.

the class HazelcastOSGiScriptEngineTest method verifyScriptEngineEvaluation.

private void verifyScriptEngineEvaluation(ScriptEngine scriptEngine) throws ScriptException {
    assertNotNull(scriptEngine.getFactory());
    final String SCRIPT_SOURCE_WITH_VARIABLE = "\"I am \" + name";
    final String SCRIPT_SOURCE_WITHOUT_VARIABLE = "\"I am human\"";
    Bindings bindings = scriptEngine.createBindings();
    bindings.put("name", "Serkan");
    ScriptContext scriptContext = scriptEngine.getContext();
    scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    assertEquals("I am Serkan", scriptEngine.eval(SCRIPT_SOURCE_WITH_VARIABLE, bindings));
    assertEquals("I am Serkan", scriptEngine.eval(SCRIPT_SOURCE_WITH_VARIABLE, scriptContext));
    assertEquals("I am human", scriptEngine.eval(SCRIPT_SOURCE_WITHOUT_VARIABLE));
    assertEquals("I am Serkan", scriptEngine.eval(new StringReader(SCRIPT_SOURCE_WITH_VARIABLE), bindings));
    assertEquals("I am Serkan", scriptEngine.eval(new StringReader(SCRIPT_SOURCE_WITH_VARIABLE), scriptContext));
    assertEquals("I am human", scriptEngine.eval(new StringReader(SCRIPT_SOURCE_WITHOUT_VARIABLE)));
}
Also used : StringReader(java.io.StringReader) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings)

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