ovidiu 2002/09/24 01:18:41 Modified: src/java/org/apache/cocoon/components/flow/javascript JavaScriptInterpreter.java Log: Moved the code managing user sessions in here from JSCocoon, since it's easier to see what's going on. Place the global JavaScript scopes in a hash table in the servlet session, indexed by the URI prefix, to allow different scopes to be used. Revision Changes Path 1.9 +70 -10 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.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- JavaScriptInterpreter.java 6 Sep 2002 00:18:31 -0000 1.8 +++ JavaScriptInterpreter.java 24 Sep 2002 08:18:41 -0000 1.9 @@ -45,8 +45,6 @@ */ package org.apache.cocoon.components.flow.javascript; - - import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; @@ -54,6 +52,7 @@ import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.avalon.excalibur.collections.ArrayEnumeration; @@ -159,13 +158,78 @@ } catch (Exception e) { context.exit(); - System.out.println("problem initializing JavaScriptInterpreter: "); e.printStackTrace(); throw e; } } /** + * Returns the JavaScript scope, a Scriptable object, from the user + * session instance. Each URI prefix, as returned by the {@link + * org.apache.cocoon.environment.Environment#getURIPrefix} method, + * can have a scope associated with it. + * + * @param environment an <code>Environment</code> value + * @param createSession a <code>boolean</code> value + * @return a <code>Scriptable</code> value + */ + public Scriptable getSessionScope(Environment environment) + { + Map objectModel = environment.getObjectModel(); + Request request = ObjectModelHelper.getRequest(objectModel); + Session session = request.getSession(false); + + if (session == null) + return null; + + Scriptable scope; + HashMap userScopes = (HashMap)session.getAttribute(USER_GLOBAL_SCOPE); + + if (userScopes == null) + return null; + + scope = (Scriptable)userScopes.get(environment.getURIPrefix()); + + return scope; + } + + /** + * Associates a JavaScript scope, a Scriptable object, with the URI + * prefix of the current sitemap, as returned by the {@link + * org.apache.cocoon.environment.Environment#getURIPrefix} method. + * + * @param environment an <code>Environment</code> value + * @param scope a <code>Scriptable</code> value + */ + public void setSessionScope(Environment environment, Scriptable scope) + { + Map objectModel = environment.getObjectModel(); + Request request = ObjectModelHelper.getRequest(objectModel); + Session session = request.getSession(true); + + HashMap userScopes = (HashMap)session.getAttribute(USER_GLOBAL_SCOPE); + if (userScopes == null) { + userScopes = new HashMap(); + session.setAttribute(USER_GLOBAL_SCOPE, userScopes); + } + + userScopes.put(environment.getURIPrefix(), scope); + } + + public void removeSessionScope(Environment environment) + { + Map objectModel = environment.getObjectModel(); + Request request = ObjectModelHelper.getRequest(objectModel); + Session session = request.getSession(true); + + HashMap userScopes = (HashMap)session.getAttribute(USER_GLOBAL_SCOPE); + if (userScopes == null) + return; + + userScopes.remove(environment.getURIPrefix()); + } + + /** * Returns a new Scriptable object to be used as the global scope * when running the JavaScript scripts in the context of a request. * @@ -192,19 +256,15 @@ Scriptable thrScope = null; // Try to retrieve the scope object from the session instance. If - // no scope is found, we create a new one, but we don't place it - // in the session. + // 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. - Map objectModel = environment.getObjectModel(); - Request request = ObjectModelHelper.getRequest(objectModel); - Session session = request.getSession(false); - if (session != null) - thrScope = (Scriptable)session.getAttribute(USER_GLOBAL_SCOPE); + thrScope = getSessionScope(environment); // The Cocoon object exported to JavaScript needs to be setup here JSCocoon cocoon;
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]