Side navigation
#1454 closed enhancement (wontfix)
Opened August 01, 2007 09:44PM UTC
Closed January 24, 2008 06:21PM UTC
Last modified March 15, 2012 05:08PM UTC
Use Rhino scoping
| Reported by: | joern | Owned by: | joern |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.1.4 |
| Component: | web | Version: | 1.1.3 |
| Keywords: | Cc: | ||
| Blocked by: | Blocking: |
Description
Currently the complete global functions and properties for a Rhino context are created for each request. It should be possible to leverage Rhino scoping API to create shared globals only once (eg. at application start) and reuse them (as a sealed parent scope?) on each request.
This is the current initialiazation, executed for each request:
ScriptableObject scope = new ImporterTopLevel(Context.enter()); Context.getCurrentContext().setErrorReporter(new ToolErrorReporter(true, System.err)); Globals.init(scope, request.getContextPath(), realPath(), page(request), request.getMethod().toLowerCase()); eval(scope, "blog"); Object result = eval(scope, page(request)); response.getWriter().write(result.toString()); Context.exit();
Creating and exiting the thread-bound context should be kept, but perhaps the global-Scope can be created once and then passed to the request-scope creation as the parent.
A shared scope implemented in a servlet:
private ScriptableObject shared; public void init(ServletConfig config) throws ServletException { super.init(config); if(config.getInitParameter("views") != null) { views = config.getInitParameter("views"); } shared = new ImporterTopLevel(Context.enter()); Context.exit(); } protected void service(HttpServletRequest request, HttpServletResponse response) { log.warn("request!"); Request.set(request); Response.set(response); // TODO allow other content types, eg. xml for rss feed response.setContentType("text/html; charset=UTF-8"); Context cx = Context.enter(); //ScriptableObject scope = new ImporterTopLevel(Context.enter()); ScriptableObject scope = (ScriptableObject) cx.newObject(shared); scope.setPrototype(shared); scope.setParentScope(null); Context.getCurrentContext().setErrorReporter(new ToolErrorReporter(true, System.err)); Globals.init(scope, request.getContextPath(), realPath(), views + page(request), request.getMethod().toLowerCase()); try { Object result = eval(scope, views + page(request)); response.getWriter().write(result.toString()); } catch (Throwable e) { try { response.getWriter().write("<h1>The following exception occcured:</h1><pre>"); e.printStackTrace(response.getWriter()); response.getWriter().write("</pre>"); log.error(e.getMessage(), e); } catch (IOException ex) { e.printStackTrace(); } } finally { Context.exit(); } }