Search in sources :

Example 6 with RhinoEngine

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

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

the class JaggeryDeployerManager method addSessionCreatedListners.

private static void addSessionCreatedListners(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);
            List<String> jsListeners = new ArrayList<String>();
            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);
                jsListeners.add(path);
            }
            servletContext.setAttribute(JaggeryCoreConstants.JS_CREATED_LISTENERS, jsListeners);
        } finally {
            if (org.mozilla.javascript.Context.getCurrentContext() != null) {
                RhinoEngine.exitContext();
            }
        }
    }
}
Also used : ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine)

Example 8 with RhinoEngine

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

the class JaggeryDeployerManager method addSessionDestroyedListners.

private static void addSessionDestroyedListners(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);
            List<String> jsListeners = new ArrayList<String>();
            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);
                jsListeners.add(path);
            }
            servletContext.setAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS, jsListeners);
        } finally {
            if (org.mozilla.javascript.Context.getCurrentContext() != null) {
                RhinoEngine.exitContext();
            }
        }
    }
}
Also used : ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) JSONObject(org.json.simple.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine)

Example 9 with RhinoEngine

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

the class WebAppManager method executeScript.

private static ScriptableObject executeScript(JaggeryContext jaggeryContext, ScriptableObject scope, String fileURL, final boolean isJSON, boolean isBuilt, boolean isIncludeOnce) throws ScriptException {
    Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
    Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
    ServletContext context = (ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT);
    String parent = includesCallstack.lastElement();
    String[] keys = WebAppManager.getKeys(context.getContextPath(), parent, fileURL);
    fileURL = getNormalizedScriptPath(keys);
    if (includesCallstack.search(fileURL) != -1) {
        return scope;
    }
    if (isIncludeOnce && includedScripts.get(fileURL) != null) {
        return scope;
    }
    ScriptReader source;
    RhinoEngine engine = jaggeryContext.getEngine();
    if (isBuilt) {
        source = new ScriptReader(context.getResourceAsStream(fileURL)) {

            @Override
            protected void build() throws IOException {
                try {
                    if (isJSON) {
                        sourceReader = new StringReader("(" + HostObjectUtil.streamToString(sourceIn) + ")");
                    } else {
                        sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
                    }
                } catch (ScriptException e) {
                    throw new IOException(e);
                }
            }
        };
    } else {
        source = new ScriptReader(context.getResourceAsStream(fileURL));
    }
    ScriptCachingContext sctx = new ScriptCachingContext(jaggeryContext.getTenantDomain(), keys[0], keys[1], keys[2]);
    sctx.setSecurityDomain(new JaggerySecurityDomain(fileURL, context));
    long lastModified = WebAppManager.getScriptLastModified(context, fileURL);
    sctx.setSourceModifiedTime(lastModified);
    includedScripts.put(fileURL, true);
    includesCallstack.push(fileURL);
    if (isJSON) {
        scope = (ScriptableObject) engine.eval(source, scope, sctx);
    } else {
        engine.exec(source, scope, sctx);
    }
    includesCallstack.pop();
    return scope;
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ServletContext(javax.servlet.ServletContext) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 10 with RhinoEngine

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

the class WebAppSessionListener method sessionCreated.

@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
    ServletContext ctx = httpSessionEvent.getSession().getServletContext();
    List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_CREATED_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() }));
    session.setAttribute(ScriptableObject.READONLY);
    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)

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