Search in sources :

Example 1 with RhinoEngine

use of org.jaggeryjs.scriptengine.engine.RhinoEngine in project jaggery by wso2.

the class WebAppSessionListener method sessionDestroyed.

@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    ServletContext ctx = httpSessionEvent.getSession().getServletContext();
    List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS);
    if (jsListeners == null) {
        return;
    }
    JaggeryContext shared = WebAppManager.sharedJaggeryContext(ctx);
    Context cx = shared.getEngine().enterContext();
    JaggeryContext context = CommonManager.getJaggeryContext();
    if (CommonManager.getJaggeryContext() == null) {
        context = WebAppManager.clonedJaggeryContext(ctx);
        CommonManager.setJaggeryContext(context);
    }
    RhinoEngine engine = context.getEngine();
    ScriptableObject clonedScope = context.getScope();
    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(cx.newObject(clonedScope, "Session", new Object[] { httpSessionEvent.getSession() }));
    RhinoEngine.defineProperty(clonedScope, session);
    for (Object jsListener : jsListeners) {
        CommonManager.getCallstack(context).push((String) jsListener);
        try {
            ScriptReader sr = new ScriptReader(ctx.getResourceAsStream((String) jsListener)) {

                @Override
                protected void build() throws IOException {
                    try {
                        sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                    } catch (ScriptException e) {
                        throw new IOException(e);
                    }
                }
            };
            engine.exec(sr, clonedScope, null);
        } catch (ScriptException e) {
            log.error(e.getMessage(), e);
        } finally {
            CommonManager.getCallstack(context).pop();
        }
    }
    Context.exit();
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) Context(org.mozilla.javascript.Context) ServletContext(javax.servlet.ServletContext) 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) JavaScriptProperty(org.jaggeryjs.scriptengine.engine.JavaScriptProperty) StringReader(java.io.StringReader) ServletContext(javax.servlet.ServletContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) List(java.util.List) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 2 with RhinoEngine

use of org.jaggeryjs.scriptengine.engine.RhinoEngine in project jaggery by wso2.

the class WebAppManager method exec.

private static void exec(HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStream sourceIn;
    Context cx;
    JaggeryContext context;
    RhinoEngine engine = null;
    ServletContext servletContext = request.getServletContext();
    try {
        engine = CommonManager.getInstance().getEngine();
        cx = engine.enterContext();
        String scriptPath = getScriptPath(request);
        OutputStream out = response.getOutputStream();
        context = createJaggeryContext(cx, out, scriptPath, request, response);
        context.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(request.getServletContext()));
        if (ModuleManager.isModuleRefreshEnabled()) {
            // reload init scripts
            refreshServletContext(servletContext);
            InputStream jaggeryConf = servletContext.getResourceAsStream(JaggeryCoreConstants.JAGGERY_CONF_FILE);
            if (jaggeryConf != null) {
                StringWriter writer = new StringWriter();
                IOUtils.copy(jaggeryConf, writer, null);
                String jsonString = writer.toString();
                JSONObject conf = (JSONObject) JSONValue.parse(jsonString);
                JSONArray initScripts = (JSONArray) conf.get(JaggeryCoreConstants.JaggeryConfigParams.INIT_SCRIPTS);
                if (initScripts != null) {
                    JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(servletContext);
                    ScriptableObject sharedScope = sharedContext.getScope();
                    Object[] scripts = initScripts.toArray();
                    for (Object script : scripts) {
                        if (!(script instanceof String)) {
                            log.error("Invalid value for initScripts in jaggery.conf : " + script);
                            continue;
                        }
                        String path = (String) script;
                        path = path.startsWith("/") ? path : "/" + path;
                        Stack<String> callstack = CommonManager.getCallstack(sharedContext);
                        callstack.push(path);
                        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, null);
                        callstack.pop();
                    }
                }
            }
        }
        Object serveFunction = request.getServletContext().getAttribute(SERVE_FUNCTION_JAGGERY);
        if (serveFunction != null) {
            Function function = (Function) serveFunction;
            ScriptableObject scope = context.getScope();
            function.call(cx, scope, scope, new Object[] { scope.get("request", scope), scope.get("response", scope), scope.get("session", scope) });
        } else {
            // resource rendering model proceeding
            sourceIn = request.getServletContext().getResourceAsStream(scriptPath);
            if (sourceIn == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
                return;
            }
            CommonManager.getInstance().getEngine().exec(new ScriptReader(sourceIn), context.getScope(), getScriptCachingContext(request, scriptPath));
        }
    } catch (ScriptException e) {
        String msg = e.getMessage();
        log.error(msg, e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
    } catch (Error e) {
        // Rhino doesn't catch Error instances and it is been used to stop the script execution
        // from any specific place. Hence, Error exception propagates up to Java and we silently
        // ignore it, assuming it was initiated by an exit() method call.
        log.debug("Script has called exit() method", e);
    } finally {
        // Exiting from the context
        if (engine != null) {
            RhinoEngine.exitContext();
        }
    }
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) ServletContext(javax.servlet.ServletContext) WebAppFileManager(org.jaggeryjs.jaggery.core.plugins.WebAppFileManager) JSONArray(org.json.simple.JSONArray) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JSONObject(org.json.simple.JSONObject) ServletContext(javax.servlet.ServletContext) JSONObject(org.json.simple.JSONObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 3 with RhinoEngine

use of org.jaggeryjs.scriptengine.engine.RhinoEngine in project jaggery by wso2.

the class WebAppManager method clonedJaggeryContext.

public static JaggeryContext clonedJaggeryContext(ServletContext context) {
    JaggeryContext shared = sharedJaggeryContext(context);
    RhinoEngine engine = shared.getEngine();
    Scriptable sharedScope = shared.getScope();
    Context cx = Context.getCurrentContext();
    ScriptableObject instanceScope = (ScriptableObject) cx.newObject(sharedScope);
    instanceScope.setPrototype(sharedScope);
    instanceScope.setParentScope(null);
    JaggeryContext clone = new JaggeryContext();
    clone.setEngine(engine);
    clone.setTenantDomain(shared.getTenantDomain());
    clone.setScope(instanceScope);
    clone.addProperty(Constants.SERVLET_CONTEXT, shared.getProperty(Constants.SERVLET_CONTEXT));
    clone.addProperty(LogHostObject.LOG_LEVEL, shared.getProperty(LogHostObject.LOG_LEVEL));
    clone.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, shared.getProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER));
    clone.addProperty(Constants.JAGGERY_CORE_MANAGER, shared.getProperty(Constants.JAGGERY_CORE_MANAGER));
    clone.addProperty(Constants.JAGGERY_INCLUDED_SCRIPTS, new HashMap<String, Boolean>());
    clone.addProperty(Constants.JAGGERY_INCLUDES_CALLSTACK, new Stack<String>());
    clone.addProperty(Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());
    return clone;
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine)

Example 4 with RhinoEngine

use of org.jaggeryjs.scriptengine.engine.RhinoEngine 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 5 with RhinoEngine

use of org.jaggeryjs.scriptengine.engine.RhinoEngine in project jaggery by wso2.

the class CommandLineExecutor method parseJaggeryScript.

/**
 * Parse Jaggery scripts resides in the file path
 *
 * @param fileURL url of the file
 */
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
static void parseJaggeryScript(final String fileURL) {
    FileInputStream fstream = null;
    try {
        // Initialize the Rhino context
        RhinoEngine.enterGlobalContext();
        fstream = new FileInputStream(fileURL);
        final RhinoEngine engine = CommandLineManager.getCommandLineEngine();
        final ScriptableObject scope = engine.getRuntimeScope();
        // initialize JaggeryContext
        final JaggeryContext jaggeryContext = new JaggeryContext();
        jaggeryContext.setTenantDomain(DEFAULT_TENANTDOMAIN);
        jaggeryContext.setEngine(engine);
        jaggeryContext.setScope(scope);
        jaggeryContext.addProperty(CommonManager.JAGGERY_OUTPUT_STREAM, System.out);
        RhinoEngine.putContextProperty("jaggeryContext", jaggeryContext);
        // Parsing the script
        final Reader source = new ScriptReader(new BufferedInputStream(fstream));
        out.println("\n");
        ShellUtilityService.initializeUtilityServices();
        engine.exec(source, scope, null);
        ShellUtilityService.destroyUtilityServices();
        out.flush();
        out.println("\n");
    } catch (Exception e) {
        out.println("\n");
        out.println("Error: " + e.getMessage());
        out.println("\n");
    } finally {
        if (fstream != null) {
            try {
                fstream.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)10 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)9 ServletContext (javax.servlet.ServletContext)8 ScriptableObject (org.mozilla.javascript.ScriptableObject)7 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)5 LogHostObject (org.jaggeryjs.hostobjects.log.LogHostObject)4 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)4 JSONObject (org.json.simple.JSONObject)4 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 List (java.util.List)2 JavaScriptProperty (org.jaggeryjs.scriptengine.engine.JavaScriptProperty)2 Context (org.mozilla.javascript.Context)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)1 WebAppFileManager (org.jaggeryjs.jaggery.core.plugins.WebAppFileManager)1 JSONArray (org.json.simple.JSONArray)1