Search in sources :

Example 1 with JavaScriptProperty

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

use of org.jaggeryjs.scriptengine.engine.JavaScriptProperty 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)

Example 3 with JavaScriptProperty

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

the class WebAppManager method defineProperties.

private static void defineProperties(Context cx, JaggeryContext context, ScriptableObject scope) {
    JavaScriptProperty request = new JavaScriptProperty("request");
    HttpServletRequest servletRequest = (HttpServletRequest) context.getProperty(SERVLET_REQUEST);
    request.setValue(cx.newObject(scope, "Request", new Object[] { servletRequest }));
    request.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, request);
    JavaScriptProperty response = new JavaScriptProperty("response");
    HttpServletResponse servletResponse = (HttpServletResponse) context.getProperty(SERVLET_RESPONSE);
    response.setValue(cx.newObject(scope, "Response", new Object[] { servletResponse }));
    response.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, response);
    JavaScriptProperty session = new JavaScriptProperty("session");
    session.setValue(cx.newObject(scope, "Session", new Object[] { servletRequest }));
    RhinoEngine.defineProperty(scope, session);
    JavaScriptProperty application = new JavaScriptProperty("application");
    ServletContext servletConext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT);
    application.setValue(cx.newObject(scope, "Application", new Object[] { servletConext }));
    application.setAttribute(ScriptableObject.READONLY);
    RhinoEngine.defineProperty(scope, application);
    if (isWebSocket(servletRequest)) {
        JavaScriptProperty websocket = new JavaScriptProperty("webSocket");
        websocket.setValue(cx.newObject(scope, "WebSocket", new Object[0]));
        websocket.setAttribute(ScriptableObject.READONLY);
        RhinoEngine.defineProperty(scope, websocket);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JavaScriptProperty(org.jaggeryjs.scriptengine.engine.JavaScriptProperty) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) JSONObject(org.json.simple.JSONObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) FileHostObject(org.jaggeryjs.hostobjects.file.FileHostObject)

Example 4 with JavaScriptProperty

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

ServletContext (javax.servlet.ServletContext)4 JavaScriptProperty (org.jaggeryjs.scriptengine.engine.JavaScriptProperty)4 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 List (java.util.List)2 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)2 LogHostObject (org.jaggeryjs.hostobjects.log.LogHostObject)2 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)2 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)2 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)2 JSONObject (org.json.simple.JSONObject)2 Context (org.mozilla.javascript.Context)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 WebAppFileManager (org.jaggeryjs.jaggery.core.plugins.WebAppFileManager)1 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)1