ovidiu 2002/08/29 17:55:12
Modified: src/java/org/apache/cocoon/components/flow/javascript
JavaScriptInterpreter.java
Log:
Don't create a session unconditionally, leave it instead to the user's
flow script to decide when is the time to create the session. Execute
the compiled script in a newly created scope.
Revision Changes Path
1.7 +25 -20
xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java
Index: JavaScriptInterpreter.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- JavaScriptInterpreter.java 21 Aug 2002 01:15:26 -0000 1.6
+++ JavaScriptInterpreter.java 30 Aug 2002 00:55:12 -0000 1.7
@@ -169,39 +169,47 @@
* Returns a new Scriptable object to be used as the global scope
* when running the JavaScript scripts in the context of a request.
*
- * <p>If <code>createNew</code> is set to true, a new Scriptable
- * object is always created and setup in the session
- * object. Otherwise, if <code>createNew</code> is false, a
- * Scriptable object is looked up in the session object and
- * returned, if it exists. If no Scriptable object is found, a new
- * one is created and returned.
+ * <p>If you want to maintain the state of global variables across
+ * multiple invocations of <code><map:call
+ * function="..."></code>, you need to invoke from the JavaScript
+ * script <code>cocoon.createSession()</code>. This will place the
+ * newly create Scriptable object in the user's session, where it
+ * will be retrieved from at the next invocation of
+ * callFunction().</p>
*
* @param environment an <code>Environment</code> value
* @param createNew a <code>boolean</code> value
* @return a <code>Scriptable</code> value
* @exception Exception if an error occurs
*/
- protected Scriptable enterContext(Environment environment,
- boolean createNew)
+ protected Scriptable enterContext(Environment environment)
throws Exception
{
Context context = Context.enter();
context.setOptimizationLevel(OPTIMIZATION_LEVEL);
context.setCompileFunctionsWithDynamicScope(true);
context.setErrorReporter(errorReporter);
- Scriptable thrScope;
+ Scriptable thrScope = null;
// Try to retrieve the scope object from the session instance. If
- // no scope is found, create a new one.
+ // no scope is found, we create a new one, but we 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.
Map objectModel = environment.getObjectModel();
Request request = ObjectModelHelper.getRequest(objectModel);
- Session session = request.getSession(true);
- thrScope = (Scriptable)session.getAttribute(USER_GLOBAL_SCOPE);
+ Session session = request.getSession(false);
+ if (session != null)
+ thrScope = (Scriptable)session.getAttribute(USER_GLOBAL_SCOPE);
// The Cocoon object exported to JavaScript needs to be setup here
JSCocoon cocoon;
- if (thrScope == null || createNew) {
+ if (thrScope == null) {
thrScope = context.newObject(scope);
thrScope.setPrototype(scope);
@@ -218,10 +226,6 @@
((JSCocoon)cocoon).setInterpreter(this);
((JSCocoon)cocoon).setScope(thrScope);
thrScope.put("cocoon", thrScope, cocoon);
-
- // Set the newly created Scope object in the session object of
- // this user.
- session.setAttribute(USER_GLOBAL_SCOPE, thrScope);
}
else
cocoon = (JSCocoon)thrScope.get("cocoon", thrScope);
@@ -253,13 +257,12 @@
System.out.println("Reading scripts");
try {
- thrScope = enterContext(environment, true);
+ thrScope = enterContext(environment);
Reader reader = new BufferedReader(new InputStreamReader(is));
Context ctx = Context.getCurrentContext();
compiledScript = ctx.compileReader(thrScope, reader,
"(combined)", 1, null);
- compiledScript.exec(ctx, thrScope);
}
catch (Exception ex) {
ex.printStackTrace();
@@ -358,8 +361,10 @@
checkForModifiedScripts(environment);
try {
- thrScope = enterContext(environment, false);
+ thrScope = enterContext(environment);
Context cx = Context.getCurrentContext();
+
+ compiledScript.exec(cx, thrScope);
JSCocoon cocoon = (JSCocoon)thrScope.get("cocoon", thrScope);
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]