/ Published in: Java
Boilerplate Java scripting API code to evaluate scripts located in either the classpath or in the file system and then get variable values by evaluating expressions (in case no easy/direct mapping can be made from script structures to java objects)
Expand |
Embed | Plain Text
/** * This default implementation tries to create and use a JavaScript script(ing) engine. * * @author Octavian Theodor NITA * @version 1.0, Feb 27, 2012 */ public class ScriptEngineFacade { private static final Logger logger = Logger.getLogger(ScriptEngineFacade.class); private final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript"); scriptEngine.put(name, value); } if (script == null) { // cannot find the script in the classpath... try in the file system } try { } finally { try { script.close(); logger.warn("cannot close stream for script " + scriptName + "; ignoring...", e); } } } if (klass == null) { throw new IllegalArgumentException("cannot cast the value of " + expr + " using a null class name"); } if (expr == null) { return null; } try { return klass.cast(scriptEngine.eval(expr)); logger.error("cannot get the value of " + expr + ": a null or undefined reference was encountered", e); return null; logger.error("cannot get the value of " + expr + ": expected value type for is " + klass.getName(), e); return null; } catch (ScriptException e) { logger.error("cannot get the value of " + expr + ": cannot evaluate expression", e); return null; } } }
You need to login to post a comment.
