public static void initContext(JaggeryContext context) throws ScriptException { context.setEngine(manager.engine); context.setScope(manager.engine.getRuntimeScope()); context.setTenantId( Integer.toString(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId())); context.addProperty(Constants.JAGGERY_CORE_MANAGER, manager); context.addProperty(Constants.JAGGERY_INCLUDED_SCRIPTS, new HashMap<String, Boolean>()); context.addProperty(Constants.JAGGERY_INCLUDES_CALLSTACK, new Stack<String>()); }
private static JaggeryContext createJaggeryContext( Context cx, OutputStream out, String scriptPath, HttpServletRequest request, HttpServletResponse response) { ServletContext servletContext = request.getServletContext(); JaggeryContext context = clonedJaggeryContext(servletContext); 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; }
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; }
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); }
public static void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String scriptPath = getScriptPath(request); InputStream sourceIn = request.getServletContext().getResourceAsStream(scriptPath); if (sourceIn == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); return; } RhinoEngine engine = null; try { engine = CommonManager.getInstance().getEngine(); Context cx = engine.enterContext(); // Creating an OutputStreamWritter to write content to the servletResponse OutputStream out = response.getOutputStream(); JaggeryContext context = createJaggeryContext(cx, out, scriptPath, request, response); context.addProperty( FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(request.getServletContext())); CommonManager.getInstance() .getEngine() .exec( new ScriptReader(sourceIn), context.getScope(), getScriptCachingContext(request, scriptPath)); // out.flush(); } catch (ScriptException e) { String msg = e.getMessage(); log.error(msg, e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg); } finally { // Exiting from the context if (engine != null) { RhinoEngine.exitContext(); } } }