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);
}
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;
}
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;
}
Aggregations