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(HOST_OBJECT_NAME, functionName, argsCount, false); } if (!(args[0] instanceof String)) { HostObjectUtil.invalidArgsError( HOST_OBJECT_NAME, functionName, "1", "string", args[0], false); } String moduleName = (String) args[0]; JaggeryContext context = getJaggeryContext(); // RhinoEngine engine = context.getEngine(); // ScriptableObject scope = context.getScope(); CommonManager manager = (CommonManager) context.getProperty(Constants.JAGGERY_CORE_MANAGER); ModuleManager moduleManager = manager.getModuleManager(); JavaScriptModule module = moduleManager.getModule(moduleName); if (module == null) { String msg = "A module cannot be found with the specified name : " + moduleName; log.error(msg); throw new ScriptException(msg); } ScriptableObject object = (ScriptableObject) cx.newObject(thisObj); object.setPrototype(thisObj); object.setParentScope(thisObj); exposeModule(cx, object, module); return object; }
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(null); 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; }
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; }
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 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 param = (String) args[0]; int dotIndex = param.lastIndexOf("."); if (param.length() == dotIndex + 1) { String msg = "Invalid file path for require method : " + param; log.error(msg); throw new ScriptException(msg); } if (dotIndex == -1) { ScriptableObject object = CommonManager.require(cx, thisObj, args, funObj); initModule(cx, CommonManager.getJaggeryContext(), param, object); return object; } JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); ScriptableObject object = (ScriptableObject) cx.newObject(thisObj); object.setPrototype(thisObj); object.setParentScope(null); String ext = param.substring(dotIndex + 1); if (ext.equalsIgnoreCase("json")) { return executeScript(jaggeryContext, object, param, true, true, false); } else if (ext.equalsIgnoreCase("js")) { return executeScript(jaggeryContext, object, param, false, true, false); } else if (ext.equalsIgnoreCase("jag")) { return executeScript(jaggeryContext, object, param, false, false, false); } else { String msg = "Unsupported file type for require() method : ." + ext; log.error(msg); throw new ScriptException(msg); } }
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])); }
/** JaggeryMethod responsible of writing to the output stream */ public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { if (!isWebSocket) { JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); // If the script itself havent set the content type we set the default content type to be // text/html HttpServletResponse servletResponse = (HttpServletResponse) jaggeryContext.getProperty(SERVLET_RESPONSE); if (servletResponse.getContentType() == null) { servletResponse.setContentType(DEFAULT_CONTENT_TYPE); } if (servletResponse.getCharacterEncoding() == null) { servletResponse.setCharacterEncoding(DEFAULT_CHAR_ENCODING); } CommonManager.print(cx, thisObj, args, funObj); } }
/** JaggeryMethod responsible of writing to the output stream */ public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { if (!isWebSocket) { JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); // If the script itself havent set the content type we set the default content type to be // text/html if (((WebAppContext) jaggeryContext).getServletResponse().getContentType() == null) { ((WebAppContext) CommonManager.getJaggeryContext()) .getServletResponse() .setContentType(DEFAULT_CONTENT_TYPE); } if (((WebAppContext) jaggeryContext).getServletResponse().getCharacterEncoding() == null) { ((WebAppContext) CommonManager.getJaggeryContext()) .getServletResponse() .setCharacterEncoding(DEFAULT_CHAR_ENCODING); } CommonManager.print(cx, thisObj, args, funObj); } }
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 webAppContext = createJaggeryContext(out, scriptPath, request, response); initContext(cx, webAppContext); RhinoEngine.putContextProperty( FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(request.getServletContext())); CommonManager.getInstance() .getEngine() .exec( new ScriptReader(sourceIn), webAppContext.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(); } } }
public static void include_once(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException { String functionName = "include_once"; 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); } JaggeryContext jaggeryContext = CommonManager.getJaggeryContext(); Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext); String parent = includesCallstack.lastElement(); String fileURL = (String) args[0]; if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) { CommonManager.include_once(cx, thisObj, args, funObj); return; } executeScript(jaggeryContext, jaggeryContext.getScope(), fileURL, false, false, true); }
public static String setInterval( 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(); String taskId = RhinoTopLevel.setInterval(cx, thisObj, args, funObj); List<String> taskIds = intervals.get(contextPath); if (taskIds == null) { taskIds = new ArrayList<String>(); intervals.put(contextPath, taskIds); } taskIds.add(taskId); return taskId; }
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; }
@Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { ServletContext ctx = httpSessionEvent.getSession().getServletContext(); List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS); JaggeryContext clonedContext = WebAppManager.clonedJaggeryContext(ctx); RhinoEngine engine = clonedContext.getEngine(); Context cx = engine.enterContext(); ScriptableObject clonedScope = clonedContext.getScope(); JavaScriptProperty session = new JavaScriptProperty("session"); session.setValue( cx.newObject(clonedScope, "Session", new Object[] {httpSessionEvent.getSession()})); session.setAttribute(ScriptableObject.READONLY); RhinoEngine.defineProperty(clonedScope, session); if (jsListeners != null) { for (Object jsListener : jsListeners) { CommonManager.getCallstack(clonedContext).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); } } } }
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); }
static { try { String jaggeryDir = System.getProperty("jaggery.home"); if (jaggeryDir == null) { jaggeryDir = System.getProperty("carbon.home"); } if (jaggeryDir == null) { log.error("Unable to find jaggery.home or carbon.home system properties"); } String modulesDir = jaggeryDir + File.separator + JAGGERY_MODULES_DIR; CommonManager.getInstance() .initialize( modulesDir, new RhinoSecurityController() { @Override protected void updatePermissions( PermissionCollection permissions, RhinoSecurityDomain securityDomain) { JaggerySecurityDomain domain = (JaggerySecurityDomain) securityDomain; ServletContext context = domain.getServletContext(); String docBase = context.getRealPath("/"); // Create a file read permission for web app context directory if (!docBase.endsWith(File.separator)) { permissions.add(new FilePermission(docBase, "read")); docBase = docBase + File.separator; } else { permissions.add( new FilePermission(docBase.substring(0, docBase.length() - 1), "read")); } docBase = docBase + "-"; permissions.add(new FilePermission(docBase, "read")); } }); } catch (ScriptException e) { log.error(e.getMessage(), e); } }
public static void initContext(Context cx, JaggeryContext context) throws ScriptException { CommonManager.initContext(context); defineProperties(cx, context, context.getScope()); }
public static RhinoEngine getEngine() throws ScriptException { return CommonManager.getInstance().getEngine(); }
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; }