Run javascript in sandbox
This task use java and Rhino to run javascript in sandbox.
Run javascript in sandbox
- Create Machine class as following
- Create DataHandler class as following
- Create javascript as following
- Call Machine.run() method as following
Call Machine.run() method
String js = loadJS(); Map args = new HashMap(); List links = new ArrayList(); args.put("links", links); links.add("Input link 1"); links.add("Input link 2"); Machine env = new Machine(new DataHandler()); Machine.run(env, js, args); for (int i = 0; i < links.size(); i++) { logger.info(links.get(i)); }Machine class
public class Machine { private Handler handler; public static void run(Machine env, String js, Map args) throws Exception { try { Context cx = Context.enter(); cx.setClassShutter(new ClassShutter() { public boolean visibleToScripts(String className) { if ("com.paesia.schema.script.Machine".equals(className)) return true; if ("java.lang.String".equals(className)) return true; if ("java.lang.Object".equals(className)) return true; if ("java.util.HashMap".equals(className)) return true; if ("java.util.ArrayList".equals(className)) return true; if ("java.lang.Byte".equals(className)) return true; if ("java.lang.Short".equals(className)) return true; if ("java.lang.Integer".equals(className)) return true; if ("java.lang.Long".equals(className)) return true; if ("java.lang.Float".equals(className)) return true; if ("java.lang.Double".equals(className)) return true; if ("java.lang.Boolean".equals(className)) return true; if ("java.lang.Character".equals(className)) return true; if (className.startsWith("com.paesia.schema.script.safe.")) return true; return false; } }); Scriptable scope = cx.initStandardObjects(); Object result = cx.evaluateString(scope, js, "<js>", 1, null); Object fObj = scope.get("main", scope); if (!(fObj instanceof Function)) { throw new Exception("main() is undefined or not a function."); } else { Object functionArgs[] = { env, args }; Function f = (Function)fObj; result = f.call(cx, scope, scope, functionArgs); } } catch (Exception e) { throw e; } finally { Context.exit(); } } public String newString(String src) { return src; } public String newString(byte[] src, String charset) throws Exception { return new String(src, charset); } public HashMap newHashMap() { return new HashMap(); } public ArrayList newArrayList() { return new ArrayList(); } public Byte newByte(byte src) { return (Byte)src; } public Short newShort(short src) { return (Short)src; } public Integer newInteger(int src) { return (Integer)src; } public Long newLong(long src) { return (Long)src; } public Float newFloat(float src) { return (Float)src; } public Double newDouble(double src) { return (Double)src; } public Boolean newBoolean(boolean src) { return (Boolean)src; } public Character newCharacter(char src) { return (Character)src; } public Machine(Handler handler) { this.handler = handler; } public void debug(String message) { if (handler != null) { handler.debug(message); } } public void error(String message) { if (handler != null) { handler.error(message); } } public void fatal(String message) { if (handler != null) { handler.fatal(message); } } public void info(String message) { if (handler != null) { handler.info(message); } } public static class Handler { public void debug(String message) { } public void error(String message) { } public void fatal(String message) { } public void info(String message) { } } }DataHandler class
public static class DataHandler extends Machine.Handler { public void debug(String message) { logger.debug(message); } public void error(String message) { logger.error(message); } public void fatal(String message) { logger.fatal(message); } public void info(String message) { logger.info(message); } }javascript
function main(env, args) { var links = args.get('links'); for (var i = 0; i < links.size(); i++) { env.info(links.get(i)); } links.clear(); links.add('Output link 1'); links.add('Output link 2'); }
No comments:
Post a Comment