use of org.mozilla.javascript.ScriptableObject in project jaggery by wso2.
the class CommandLineExecutor method parseJaggeryExpression.
/**
* Parse Jaggery expressions
*
* @param expression Jaggery expression string
*/
static void parseJaggeryExpression(final String expression) {
try {
//Initialize the Rhino context
RhinoEngine.enterGlobalContext();
final RhinoEngine engine = CommandLineManager.getCommandLineEngine();
final ScriptableObject scope = engine.getRuntimeScope();
//initialize JaggeryContext
final JaggeryContext jaggeryContext = new JaggeryContext();
jaggeryContext.setTenantDomain("ca");
jaggeryContext.setEngine(engine);
jaggeryContext.setScope(scope);
jaggeryContext.addProperty(CommonManager.JAGGERY_OUTPUT_STREAM, out);
RhinoEngine.putContextProperty("jaggeryContext", jaggeryContext);
//Parsing the script
ShellUtilityService.initializeUtilityServices();
engine.exec(new StringReader(expression), scope, null);
ShellUtilityService.destroyUtilityServices();
out.flush();
} catch (Exception e) {
out.println("Error: " + e.getMessage());
}
}
use of org.mozilla.javascript.ScriptableObject in project jaggery by wso2.
the class JaggeryDeployerManager method executeScripts.
private static void executeScripts(Context context, JSONArray arr) {
if (arr != null) {
try {
JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(context.getServletContext());
CommonManager.setJaggeryContext(sharedContext);
RhinoEngine engine = sharedContext.getEngine();
org.mozilla.javascript.Context cx = engine.enterContext();
ServletContext servletContext = (ServletContext) sharedContext.getProperty(org.jaggeryjs.hostobjects.web.Constants.SERVLET_CONTEXT);
ScriptableObject sharedScope = sharedContext.getScope();
Object[] scripts = arr.toArray();
for (Object script : scripts) {
if (!(script instanceof String)) {
log.error("Invalid value for initScripts/destroyScripts in jaggery.conf : " + script);
continue;
}
String path = (String) script;
path = path.startsWith("/") ? path : "/" + path;
Stack<String> callstack = CommonManager.getCallstack(sharedContext);
callstack.push(path);
String[] parts = WebAppManager.getKeys(servletContext.getContextPath(), path, path);
ScriptCachingContext sctx = new ScriptCachingContext(sharedContext.getTenantDomain(), parts[0], parts[1], parts[2]);
sctx.setSecurityDomain(new JaggerySecurityDomain(path, servletContext));
engine.exec(new ScriptReader(servletContext.getResourceAsStream(path)) {
@Override
protected void build() throws IOException {
try {
sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
} catch (ScriptException e) {
// throw new IOException(e);
}
}
}, sharedScope, sctx);
}
} catch (ScriptException e) {
log.error(e.getMessage(), e);
} finally {
if (org.mozilla.javascript.Context.getCurrentContext() != null) {
RhinoEngine.exitContext();
}
}
}
}
use of org.mozilla.javascript.ScriptableObject in project sling by apache.
the class JavascriptEngine method execute.
/** Execute supplied code against supplied data,
* see JavascriptEngineTest for examples */
public String execute(String code, String jsonData) throws IOException {
final String jsCode = "data=" + jsonData + ";\n" + code;
final Context rhinoContext = Context.enter();
final ScriptableObject scope = rhinoContext.initStandardObjects();
// execute the script, out script variable maps to sw
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope));
final int lineNumber = 1;
final Object securityDomain = null;
try {
rhinoContext.evaluateString(scope, jsCode, getClass().getSimpleName(), lineNumber, securityDomain);
} catch (Exception e) {
final IOException ioe = new IOException("While executing [" + code + "]:" + e);
ioe.initCause(e);
throw ioe;
}
// check script output
pw.flush();
return sw.toString().trim();
}
use of org.mozilla.javascript.ScriptableObject in project sling by apache.
the class RhinoJavaScriptEngineFactory method getRootScope.
@SuppressWarnings("unchecked")
private Scriptable getRootScope() {
if (rootScope == null) {
final Context rhinoContext = Context.enter();
try {
rhinoContext.setOptimizationLevel(optimizationLevel);
Scriptable tmpScope = rhinoContext.initStandardObjects(new ImporterTopLevel(rhinoContext), false);
// default classes
addHostObjects(tmpScope, (Class<? extends ScriptableObject>[]) HOSTOBJECT_CLASSES);
// provided classes
for (RhinoHostObjectProvider provider : hostObjectProvider) {
addHostObjects(tmpScope, provider.getHostObjectClasses());
addImportedClasses(rhinoContext, tmpScope, provider.getImportedClasses());
addImportedPackages(rhinoContext, tmpScope, provider.getImportedPackages());
}
// only assign the root scope when complete set up
rootScope = tmpScope;
} finally {
// ensure the context is exited after setting up the
// the new root scope
Context.exit();
}
}
return rootScope;
}
use of org.mozilla.javascript.ScriptableObject in project druid by druid-io.
the class JavaScriptPostAggregator method compile.
private static Function compile(String function) {
final ContextFactory contextFactory = ContextFactory.getGlobal();
final Context context = contextFactory.enterContext();
context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
final ScriptableObject scope = context.initStandardObjects();
final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
Context.exit();
return new Function() {
public double apply(Object[] args) {
// ideally we need a close() function to discard the context once it is not used anymore
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
}
return Context.toNumber(fn.call(cx, scope, scope, args));
}
};
}
Aggregations