Search in sources :

Example 1 with WebAppFileManager

use of org.jaggeryjs.jaggery.core.plugins.WebAppFileManager 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 2 with WebAppFileManager

use of org.jaggeryjs.jaggery.core.plugins.WebAppFileManager in project jaggery by wso2.

the class WebAppManager method deploy.

public static void deploy(org.apache.catalina.Context context) throws ScriptException {
    ServletContext ctx = context.getServletContext();
    JaggeryContext sharedContext = new JaggeryContext();
    Context cx = Context.getCurrentContext();
    CommonManager.initContext(sharedContext);
    sharedContext.addProperty(Constants.SERVLET_CONTEXT, ctx);
    sharedContext.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(ctx));
    sharedContext.addProperty(Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());
    String logLevel = (String) ctx.getAttribute(LogHostObject.LOG_LEVEL);
    if (logLevel != null) {
        sharedContext.addProperty(LogHostObject.LOG_LEVEL, logLevel);
    }
    ScriptableObject sharedScope = sharedContext.getScope();
    JavaScriptProperty application = new JavaScriptProperty("application");
    application.setValue(cx.newObject(sharedScope, "Application", new Object[] { ctx }));
    application.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(sharedScope, application);
    ctx.setAttribute(SHARED_JAGGERY_CONTEXT, sharedContext);
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) ServletContext(javax.servlet.ServletContext) JavaScriptProperty(org.jaggeryjs.scriptengine.engine.JavaScriptProperty) WebAppFileManager(org.jaggeryjs.jaggery.core.plugins.WebAppFileManager) ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) JSONObject(org.json.simple.JSONObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject)

Aggregations

ServletContext (javax.servlet.ServletContext)2 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)2 LogHostObject (org.jaggeryjs.hostobjects.log.LogHostObject)2 WebAppFileManager (org.jaggeryjs.jaggery.core.plugins.WebAppFileManager)2 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)2 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)2 JSONObject (org.json.simple.JSONObject)2 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)1 JavaScriptProperty (org.jaggeryjs.scriptengine.engine.JavaScriptProperty)1 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 JSONArray (org.json.simple.JSONArray)1