ru.ydn.wicket.wicketconsole.EmbeddedScriptEngine Maven / Gradle / Ivy
package ru.ydn.wicket.wicketconsole;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import org.apache.wicket.ajax.json.JSONException;
import org.apache.wicket.ajax.json.JSONObject;
public class EmbeddedScriptEngine implements IScriptEngine{
private String name;
private ScriptEngine engine;
private transient ScriptContext ctx;
private boolean asJSONComatibleAvailable = true;
public EmbeddedScriptEngine(String name, ScriptEngine engine) {
this.name = name;
this.engine = engine;
this.ctx = new SimpleScriptContext();
ctx.setWriter(new StringWriter());
ctx.setErrorWriter(new StringWriter());
ctx.setReader(new StringReader(""));
}
@Override
public String getName() {
return name;
}
private static String getContentAndClear(StringWriter writer)
{
StringBuffer buf = writer.getBuffer();
String ret = buf.toString();
buf.setLength(0);
return ret;
}
@Override
public ScriptResult eval(String command,IScriptContext context) {
ScriptResult result = new ScriptResult(name, command);
if (context!=null){
Set> bindings = context.getBindings().entrySet();
for (Entry binding : bindings) {
ctx.setAttribute(binding.getKey(), binding.getValue(), ScriptContext.ENGINE_SCOPE);
}
}
try {
Object ret = engine.eval(command, ctx);
ctx.getBindings(ScriptContext.ENGINE_SCOPE).put("$result", ret);
ret = tryConvertToJSON(ret);
result.setResultModel(new StorageModel