ovidiu      2002/08/20 18:15:26

  Modified:    src/java/org/apache/cocoon/components/flow/javascript
                        JavaScriptInterpreter.java
  Log:
  Place thrScope in the session object, so that further callFunction()
  invocations re-use the same global scope for a given user.
  
  Revision  Changes    Path
  1.6       +67 -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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JavaScriptInterpreter.java        17 Aug 2002 01:20:58 -0000      1.5
  +++ JavaScriptInterpreter.java        21 Aug 2002 01:15:26 -0000      1.6
  @@ -46,6 +46,7 @@
   package org.apache.cocoon.components.flow.javascript;
   
   
  +
   import java.io.BufferedReader;
   import java.io.InputStream;
   import java.io.InputStreamReader;
  @@ -54,6 +55,7 @@
   import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.List;
  +import java.util.Map;
   import org.apache.avalon.excalibur.collections.ArrayEnumeration;
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.configuration.Configurable;
  @@ -64,6 +66,9 @@
   import org.apache.cocoon.components.flow.WebContinuation;
   import org.apache.cocoon.environment.Environment;
   import org.apache.cocoon.environment.ModifiableSource;
  +import org.apache.cocoon.environment.ObjectModelHelper;
  +import org.apache.cocoon.environment.Request;
  +import org.apache.cocoon.environment.Session;
   import org.apache.cocoon.environment.Source;
   import org.mozilla.javascript.Context;
   import org.mozilla.javascript.ErrorReporter;
  @@ -84,6 +89,8 @@
   public class JavaScriptInterpreter extends AbstractInterpreter
     implements Configurable, Initializable
   {
  +  public static final String USER_GLOBAL_SCOPE = "JavaScript GLOBAL SCOPE";
  +
     // This is the only optimization level that supports continuations
     // in the Christoper Oliver's Rhino JavaScript implementation
     static int OPTIMIZATION_LEVEL = -2;
  @@ -158,30 +165,70 @@
       }
     }
   
  -  protected Scriptable enterContext(Environment environment)
  +  /**
  +   * 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.
  +   *
  +   * @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)
       throws Exception
     {
       Context context = Context.enter();
       context.setOptimizationLevel(OPTIMIZATION_LEVEL);
       context.setCompileFunctionsWithDynamicScope(true);
       context.setErrorReporter(errorReporter);
  -    Scriptable thrScope = context.newObject(scope);
  +    Scriptable thrScope;
  +
  +    // Try to retrieve the scope object from the session instance. If
  +    // no scope is found, create a new one.
  +    Map objectModel = environment.getObjectModel();
  +    Request request = ObjectModelHelper.getRequest(objectModel);
  +    Session session = request.getSession(true);
  +    thrScope = (Scriptable)session.getAttribute(USER_GLOBAL_SCOPE);
  +
  +    // The Cocoon object exported to JavaScript needs to be setup here
  +    JSCocoon cocoon;
  +
  +    if (thrScope == null || createNew) {
  +      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(scope, "Cocoon", args);
  +      ((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);
   
  -    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[] = {};
  -    Scriptable cocoon = context.newObject(scope, "Cocoon", args);
  -    ((JSCocoon)cocoon).setInterpreter(this);
  -    ((JSCocoon)cocoon).setContext(manager, environment);
  -    ((JSCocoon)cocoon).setScope(thrScope);
  -    thrScope.put("cocoon", thrScope, cocoon);
  +    // We need to setup the JSCocoon object according to the current
  +    // request. Everything else remains the same.
  +    cocoon.setContext(manager, environment);
   
       return thrScope;
     }
  @@ -206,12 +253,13 @@
       System.out.println("Reading scripts");
   
       try {
  -      thrScope = enterContext(environment);
  +      thrScope = enterContext(environment, true);
         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();
  @@ -310,10 +358,9 @@
       checkForModifiedScripts(environment);
   
       try {
  -      thrScope = enterContext(environment);
  +      thrScope = enterContext(environment, false);
         Context cx = Context.getCurrentContext();
   
  -      compiledScript.exec(cx, thrScope);
         JSCocoon cocoon = (JSCocoon)thrScope.get("cocoon", thrScope);
   
         Object callFunction = thrScope.get("callFunction", thrScope);
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to