Search in sources :

Example 46 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project java8-tutorial by winterbe.

the class Nashorn11 method test2.

private static void test2() throws ScriptException {
    NashornScriptEngine engine = createEngine();
    engine.eval("function foo() { print('bar') };");
    SimpleScriptContext context = new SimpleScriptContext();
    engine.eval("print(Function);", context);
    engine.eval("foo();", context);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine)

Example 47 with SimpleScriptContext

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

the class ScriptEngineHelper method eval.

public Object eval(String javascriptCode, Map<String, Object> data, final StringWriter sw) throws ScriptException {
    final PrintWriter pw = new PrintWriter(sw, true);
    ScriptContext ctx = new SimpleScriptContext();
    final Bindings b = new SimpleBindings();
    b.put("out", pw);
    if (data != null) {
        for (Map.Entry<String, Object> e : data.entrySet()) {
            b.put(e.getKey(), e.getValue());
        }
    }
    ctx.setBindings(b, ScriptContext.ENGINE_SCOPE);
    ctx.setWriter(sw);
    ctx.setErrorWriter(new OutputStreamWriter(System.err));
    Object result = getEngine().eval(javascriptCode, ctx);
    if (result instanceof Wrapper) {
        result = ((Wrapper) result).unwrap();
    }
    if (result instanceof ScriptableObject) {
        Context.enter();
        try {
            result = ((ScriptableObject) result).getDefaultValue(null);
        } finally {
            Context.exit();
        }
    }
    return result;
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) ScriptableObject(org.mozilla.javascript.ScriptableObject) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) OutputStreamWriter(java.io.OutputStreamWriter) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 48 with SimpleScriptContext

use of javax.script.SimpleScriptContext 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)

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)48 ScriptContext (javax.script.ScriptContext)45 Test (org.junit.Test)25 SimpleBindings (javax.script.SimpleBindings)22 Bindings (javax.script.Bindings)20 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 CompiledScript (javax.script.CompiledScript)5 Test (org.testng.annotations.Test)5 ScriptEngine (javax.script.ScriptEngine)3 ScriptException (javax.script.ScriptException)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Scanner (java.util.Scanner)1