Search in sources :

Example 11 with JaggeryContext

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

the class WebAppManager method createJaggeryContext.

private static JaggeryContext createJaggeryContext(Context cx, OutputStream out, String scriptPath, HttpServletRequest request, HttpServletResponse response) {
    ServletContext servletContext = request.getServletContext();
    JaggeryContext context = clonedJaggeryContext(servletContext);
    CommonManager.setJaggeryContext(context);
    context.addProperty(SERVLET_REQUEST, request);
    context.addProperty(SERVLET_RESPONSE, response);
    CommonManager.getCallstack(context).push(scriptPath);
    CommonManager.getIncludes(context).put(scriptPath, true);
    context.addProperty(CommonManager.JAGGERY_OUTPUT_STREAM, out);
    defineProperties(cx, context, context.getScope());
    return context;
}
Also used : ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 12 with JaggeryContext

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

Example 14 with JaggeryContext

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

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

the class WebAppManager method require.

public static ScriptableObject require(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
    String functionName = "require";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
    }
    String moduleId = (String) args[0];
    int dotIndex = moduleId.lastIndexOf(".");
    if (moduleId.length() == dotIndex + 1) {
        String msg = "Invalid file path for require method : " + moduleId;
        log.error(msg);
        throw new ScriptException(msg);
    }
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    Map<String, ScriptableObject> requiredModules = (Map<String, ScriptableObject>) jaggeryContext.getProperty(Constants.JAGGERY_REQUIRED_MODULES);
    ScriptableObject object = requiredModules.get(moduleId);
    if (object != null) {
        return object;
    }
    if (dotIndex == -1) {
        object = CommonManager.require(cx, thisObj, args, funObj);
        initModule(cx, jaggeryContext, moduleId, object);
    } else {
        object = (ScriptableObject) cx.newObject(thisObj);
        object.setPrototype(thisObj);
        object.setParentScope(thisObj);
        //sharedJaggeryContext((ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT)).getScope()
        //object.setParentScope(sharedJaggeryContext((ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT)).getScope());
        String ext = moduleId.substring(dotIndex + 1);
        if (ext.equalsIgnoreCase("json")) {
            object = executeScript(jaggeryContext, object, moduleId, true, true, false);
        } else if (ext.equalsIgnoreCase("js")) {
            object = executeScript(jaggeryContext, object, moduleId, false, true, false);
        } else if (ext.equalsIgnoreCase("jag")) {
            object = executeScript(jaggeryContext, object, moduleId, false, false, false);
        } else {
            String msg = "Unsupported file type for require() method : ." + ext;
            log.error(msg);
            throw new ScriptException(msg);
        }
    }
    requiredModules.put(moduleId, object);
    return object;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) 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