Bug Tracker

Ticket #1454 (closed enhancement: wontfix)

Opened 1 year ago

Last modified 1 year ago

Use Rhino scoping

Reported by: joern Assigned to: joern
Type: enhancement Priority: minor
Milestone: 1.1.4 Component: web
Version: 1.1.3 Keywords:
Cc: Needs: Review

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.

Attachments

Change History

Changed 1 year ago by joern

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();
	}
}

Changed 1 year ago by joern

  • status changed from new to closed
  • resolution set to wontfix
Note: See TracTickets for help on using tickets.