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.setTenantId(shared.getTenantId()); 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>()); CommonManager.setJaggeryContext(clone); return clone; }
protected static ScriptCachingContext getScriptCachingContext( HttpServletRequest request, String scriptPath) throws ScriptException { JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); String tenantId = jaggeryContext.getTenantId(); 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; }
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.getTenantId(), 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; }