Search in sources :

Example 1 with JaggeryContext

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

the class WebSocketHostObject method jsConstructor.

public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws Exception {
    int argsCount = args.length;
    if (argsCount != 0) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
    }
    WebSocketHostObject who = new WebSocketHostObject();
    who.contextFactory = cx.getFactory();
    JaggeryContext currentContext = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
    JaggeryContext asyncContext = new JaggeryContext();
    asyncContext.setEngine(currentContext.getEngine());
    asyncContext.setScope(currentContext.getScope());
    asyncContext.setTenantDomain(currentContext.getTenantDomain());
    asyncContext.addProperty(Constants.SERVLET_CONTEXT, currentContext.getProperty(Constants.SERVLET_CONTEXT));
    asyncContext.addProperty(LogHostObject.LOG_LEVEL, currentContext.getProperty(LogHostObject.LOG_LEVEL));
    asyncContext.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, currentContext.getProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER));
    asyncContext.addProperty(Constants.JAGGERY_CORE_MANAGER, currentContext.getProperty(Constants.JAGGERY_CORE_MANAGER));
    asyncContext.addProperty(Constants.JAGGERY_INCLUDED_SCRIPTS, new HashMap<String, Boolean>());
    asyncContext.addProperty(Constants.JAGGERY_INCLUDES_CALLSTACK, new Stack<String>());
    asyncContext.addProperty(Constants.JAGGERY_REQUIRED_MODULES, new HashMap<String, ScriptableObject>());
    who.asyncContext = asyncContext;
    return who;
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 2 with JaggeryContext

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

the class LogHostObject method jsConstructor.

/**
 * Creates a new log object for the requested resource.
 * logger name will be the resource name separated by a . (dot)
 * i.e if resource is /foo/bar/mar.jag
 * the loger name will be
 * JAGGERY.foo.bar.mar
 * <p/>
 * by default the log level is set to debug
 *
 * @param cx
 * @param args
 * @param ctorObj
 * @param inNewExpr
 * @return
 * @throws ScriptException
 */
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
    int argsCount = args.length;
    if (argsCount > 1) {
        HostObjectUtil.invalidNumberOfArgs(HOSTOBJECT_NAME, HOSTOBJECT_NAME, argsCount, true);
    }
    String loggerName;
    JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
    if (argsCount == 1 && (args[0] instanceof String)) {
        loggerName = (String) args[0];
    } else {
        String requestString = ((Stack<String>) context.getProperty(JAGGERY_INCLUDES_CALLSTACK)).peek();
        loggerName = ROOT_LOGGER + requestString.replace(".jag", ":jag").replace(".js", ":js").replace("/", ".");
    }
    LogHostObject logObj = new LogHostObject();
    Logger currentLogger = Logger.getLogger(loggerName);
    String appLogLevel = (String) context.getProperty(LOG_LEVEL);
    if (currentLogger.getLevel() == null) {
        currentLogger.setLevel(Level.toLevel(appLogLevel));
    }
    logObj.logger = currentLogger;
    return logObj;
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) Logger(org.apache.log4j.Logger) Stack(java.util.Stack)

Example 3 with JaggeryContext

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

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

the class WebAppManager method getScriptCachingContext.

protected static ScriptCachingContext getScriptCachingContext(HttpServletRequest request, String scriptPath) throws ScriptException {
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    String tenantId = jaggeryContext.getTenantDomain();
    String[] parts = getKeys(request.getContextPath(), scriptPath, scriptPath);
    /**
     * tenantId = tenantId
     * context = webapp context
     * path = relative path to the directory of *.js file
     * cacheKey = name of the *.js file being cached
     */
    ScriptCachingContext sctx = new ScriptCachingContext(tenantId, parts[0], parts[1], parts[2]);
    ServletContext servletContext = request.getServletContext();
    sctx.setSecurityDomain(new JaggerySecurityDomain(getNormalizedScriptPath(parts), servletContext));
    long lastModified = getScriptLastModified(servletContext, scriptPath);
    sctx.setSourceModifiedTime(lastModified);
    return sctx;
}
Also used : ScriptCachingContext(org.jaggeryjs.scriptengine.cache.ScriptCachingContext) ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 5 with JaggeryContext

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

the class WebAppManager method clearInterval.

public static void clearInterval(final Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    JaggeryContext context = CommonManager.getJaggeryContext();
    ServletContext servletContext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT);
    String contextPath = servletContext.getContextPath();
    RhinoTopLevel.clearTimeout(cx, thisObj, args, funObj);
    List<String> taskIds = intervals.get(contextPath);
    taskIds.remove(String.valueOf(args[0]));
}
Also used : ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Aggregations

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