Context context = Context.enter();
context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setGeneratingDebug(true);
context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
Scriptable thrScope = null;
// Try to retrieve the scope object from the session instance. If
// no scope is found, we create a new one, but don't place it in
// the session.
//
// When a user script "creates" a session using
// cocoon.createSession() in JavaScript, the thrScope is placed in
// the session object, where it's later retrieved from here. This
// behaviour allows multiple JavaScript functions to share the
// same global scope.
thrScope = getSessionScope(environment);
// The Cocoon object exported to JavaScript needs to be setup here
JSCocoon cocoon;
boolean newScope = false;
long lastExecTime = 0;
if (thrScope == null) {
newScope = true;
thrScope = context.newObject(scope);
thrScope.setPrototype(scope);
// We want 'thrScope' to be a new top-level scope, so set its
// parent scope to null. This means that any variables created
// by assignments will be properties of "thrScope".
thrScope.setParentScope(null);
// Put in the thread scope the Cocoon object, which gives access
// to the interpreter object, and some Cocoon objects. See
// JSCocoon for more details.
Object args[] = {};
cocoon = (JSCocoon)context.newObject(thrScope, "Cocoon", args);
cocoon.setInterpreter(this);
cocoon.setParentScope(thrScope);
thrScope.put("cocoon", thrScope, cocoon);
((ScriptableObject)thrScope).defineProperty(LAST_EXEC_TIME,
new Long(0),
ScriptableObject.DONTENUM |
ScriptableObject.PERMANENT);
} else {
cocoon = (JSCocoon)thrScope.get("cocoon", thrScope);
lastExecTime = ((Long)thrScope.get(LAST_EXEC_TIME,
thrScope)).longValue();
}
// We need to setup the JSCocoon object according to the current
// request. Everything else remains the same.
cocoon.setContext(manager, environment);
// Check if we need to compile and/or execute scripts
synchronized (compiledScripts) {
List execList = new ArrayList();
boolean needsRefresh = false;
if (reloadScripts) {
long now = System.currentTimeMillis();
if (now >= lastTimeCheck + checkTime) {
needsRefresh = true;
}
lastTimeCheck = now;
}
// If we've never executed scripts in this scope or
// if reload-scripts is true and the check interval has expired
// or if new scripts have been specified in the sitemap,
// then create a list of scripts to compile/execute
if (lastExecTime == 0 || needsRefresh || needResolve.size() > 0) {
topLevelScripts.addAll(needResolve);
if (!newScope && !needsRefresh) {
execList.addAll(needResolve);
} else {
execList.addAll(topLevelScripts);
}
needResolve.clear();
}
thrScope.put(LAST_EXEC_TIME, thrScope,
new Long(System.currentTimeMillis()));
// Compile all the scripts first. That way you can set breakpoints
// in the debugger before they execute.
for (int i = 0, size = execList.size(); i < size; i++) {
String sourceURI = (String)execList.get(i);