日期:2014-05-16 浏览次数:20348 次
package com.byd.mes.util.script; import java.util.HashMap; import java.util.Map; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import sun.org.mozilla.javascript.internal.NativeObject; public class ScriptUtils { private static final ScriptEngineManager manager = new ScriptEngineManager(); private Map<String,Callback> custom = new HashMap<String,Callback>(); public interface Callback{ void callBack(String script); } private Map<String,Callback> conf = new HashMap<String,Callback>(); { conf.put("testing", new ScriptUtils.Callback(){ @Override public void callBack(String script) { ScriptEngine engine = manager.getEngineByName("javascript"); try { engine.eval(script); Invocable invocableEngine = (Invocable)engine; NativeObject obj = (NativeObject) invocableEngine.invokeFunction("testing", 1); System.out.println(obj.get("nextOperation", null)); } catch (Exception e) { throw new RuntimeException("执行Script脚本失败[function:testing]",e); } }}); } public static void testing(String script){ } public void setCustom(Map<String,Callback> custom) { this.custom = custom; this.conf.putAll(custom); } public Map<String,Callback> getCustom() { return custom; } }
?