我一直在努力解決使用java scripting API來控制某些用戶定義的javascript的執行問題。我正在使用內置的Rhino引擎,它說你可以設置InstructionObserverThreshold,如果達到限制,它將負責停止執行。我一直在玩下面的示例應用程序一段時間,並且我爲什麼不起作用而難以接受。你會看到我已經設置了MaximumInterpreterStackDepth。這很好,但指導觀察員似乎沒有做任何事情。在javax.scripting環境中設置instructionObserverThreshold
有關此代碼缺失的任何想法,使其工作?
謝謝!
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import com.sun.script.javascript.RhinoScriptEngine;
public class RhinoTester2 {
public static void main(String[] args) {
new RhinoScriptEngine(); // initialize the global context.
sun.org.mozilla.javascript.internal.ContextFactory cx = sun.org.mozilla.javascript.internal.ContextFactory.getGlobal();
cx.addListener(new sun.org.mozilla.javascript.internal.ContextFactory.Listener() {
public void contextCreated(sun.org.mozilla.javascript.internal.Context cxt) {
cxt.setInstructionObserverThreshold(10000); // This should stop after 10 seconds or so.
cxt.setMaximumInterpreterStackDepth(1); // this should not be a factor for the moment
System.out.println("Context Factory threshold set. "+cxt.getInstructionObserverThreshold());
}
@Override
public void contextReleased(
sun.org.mozilla.javascript.internal.Context arg0) {
System.out.println("Context Released.");
}
});
// Now run the script to see if it will be stopped after a short time.
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
try {
// engine.eval("while(true){println(\"hello\");};"); // This will fail immediately due to the interpreter stack depth.
engine.eval("while(true){};"); // this never fails. runs happily forever.
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
不幸的是,腳本API使用的基本ContextFactory中沒有提供處理InstructionObservationCount的推薦方法。在查看RhinoScriptEngine時,用戶無法修改ContextFactory以添加實現(密封類和匿名靜態塊調用initGlobal)。從我可以告訴你只有兩個選項 - 要麼創建自己的腳本引擎,並替換內置版本或在線程中管理它,並在超過超時時間後終止線程。令人失望的..... –