Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 57500 invoked by uid 500); 30 Aug 2002 00:55:14 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 57489 invoked by uid 500); 30 Aug 2002 00:55:14 -0000 Delivered-To: apmail-xml-cocoon2-cvs@apache.org Date: 30 Aug 2002 00:55:13 -0000 Message-ID: <20020830005513.25026.qmail@icarus.apache.org> From: ovidiu@apache.org To: xml-cocoon2-cvs@apache.org Subject: cvs commit: xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript JavaScriptInterpreter.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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. * - *

If createNew is set to true, a new Scriptable - * object is always created and setup in the session - * object. Otherwise, if createNew 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. + *

If you want to maintain the state of global variables across + * multiple invocations of <map:call + * function="...">, you need to invoke from the JavaScript + * script cocoon.createSession(). 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().

* * @param environment an Environment value * @param createNew a boolean value * @return a Scriptable 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: webmaster@xml.apache.org To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org For additional commands, e-mail: cocoon-cvs-help@xml.apache.org