Search in sources :

Example 16 with ScriptableObject

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());
    }
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine)

Example 17 with ScriptableObject

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();
            }
        }
    }
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) IOException(java.io.IOException) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) StringReader(java.io.StringReader) ServletContext(javax.servlet.ServletContext) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 18 with ScriptableObject

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();
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) StringWriter(java.io.StringWriter) ScriptableObject(org.mozilla.javascript.ScriptableObject) IOException(java.io.IOException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 19 with ScriptableObject

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;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) BundleContext(org.osgi.framework.BundleContext) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoHostObjectProvider(org.apache.sling.scripting.javascript.RhinoHostObjectProvider) NativeJavaClass(org.mozilla.javascript.NativeJavaClass) Scriptable(org.mozilla.javascript.Scriptable)

Example 20 with ScriptableObject

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));
        }
    };
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Aggregations

ScriptableObject (org.mozilla.javascript.ScriptableObject)44 Context (org.mozilla.javascript.Context)20 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)8 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)8 ContextAction (org.mozilla.javascript.ContextAction)6 ContextFactory (org.mozilla.javascript.ContextFactory)6 Scriptable (org.mozilla.javascript.Scriptable)6 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)5 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 IOException (java.io.IOException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 ServletContext (javax.servlet.ServletContext)3 Function (org.mozilla.javascript.Function)3 Function (com.google.common.base.Function)2 PrintWriter (java.io.PrintWriter)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2