coliver     2003/07/19 13:16:27

  Modified:    src/java/org/apache/cocoon/components/flow/javascript/fom
                        FOM_Cocoon.java FOM_JavaScriptInterpreter.java
  Added:       src/java/org/apache/cocoon/components/flow/javascript/fom
                        FOM_WebContinuation.java
  Log:
  Cleaned up Java FOM API, fixed bug in script loading, and enusred session 
isn't created unnecessarily
  
  Revision  Changes    Path
  1.5       +104 -76   
cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java
  
  Index: FOM_Cocoon.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FOM_Cocoon.java   17 Jul 2003 17:12:52 -0000      1.4
  +++ FOM_Cocoon.java   19 Jul 2003 20:16:27 -0000      1.5
  @@ -54,6 +54,7 @@
   import java.util.Enumeration;
   import java.util.Iterator;
   import java.util.LinkedList;
  +import java.util.ArrayList;
   import java.util.List;
   import java.util.Map;
   
  @@ -70,7 +71,7 @@
   import org.apache.cocoon.environment.Session;
   import org.mozilla.javascript.*;
   import org.mozilla.javascript.continuations.Continuation;
  -
  +import org.apache.cocoon.components.flow.Interpreter.Argument;
   /**
    * Implementation of FOM (Flow Object Model).
    *
  @@ -154,9 +155,13 @@
           if(! uri.startsWith( "cocoon://" ) ) {
               redUri = "cocoon://" + this.environment.getURIPrefix() + uri;
           }
  -        
  +        FOM_WebContinuation fom_wk = 
  +            new FOM_WebContinuation(wk);
  +        fom_wk.setParentScope(getParentScope());
  +        fom_wk.setPrototype(getClassPrototype(getParentScope(), 
  +                                              fom_wk.getClassName()));
           interpreter.forwardTo(getParentScope(), this, redUri,
  -                              bizData, wk, environment);
  +                              bizData, fom_wk, environment);
   
           FOM_WebContinuation result = null;
           if (wk != null) {
  @@ -674,70 +679,6 @@
           }
       }
   
  -    public static class FOM_WebContinuation extends ScriptableObject {
  -        
  -        WebContinuation wk;
  -
  -        public FOM_WebContinuation() {
  -        }
  -
  -        public FOM_WebContinuation(Object wk) {
  -            this.wk = (WebContinuation)unwrap(wk);
  -        }
  -
  -        public String getClassName() {
  -            return "FOM_WebContinuation";
  -        }
  -
  -        public String jsGet_id() {
  -            return wk.getId();
  -        }
  -
  -        public FOM_WebContinuation jsFunction_getParent() {
  -            WebContinuation parent = wk.getParentContinuation();
  -            if (parent == null) return null;
  -            FOM_WebContinuation pwk = new FOM_WebContinuation(parent);
  -            pwk.setParentScope(getParentScope());
  -            pwk.setPrototype(getClassPrototype(getParentScope(), 
  -                                               pwk.getClassName()));
  -            return pwk;
  -        }
  -
  -        public NativeArray jsFunction_getChildren() throws Exception {
  -            List list = wk.getChildren();
  -            NativeArray arr = 
  -                
(NativeArray)org.mozilla.javascript.Context.getCurrentContext().newObject(getParentScope(),
 
  -                                               "Array",
  -                                               new Object[]{new 
Integer(list.size())});
  -            Iterator iter = list.iterator();
  -            for (int i = 0; iter.hasNext(); i++) {
  -                WebContinuation child = (WebContinuation)iter.next();
  -                FOM_WebContinuation cwk = new FOM_WebContinuation(child);
  -                cwk.setParentScope(getParentScope());
  -                cwk.setPrototype(getClassPrototype(getParentScope(), 
  -                                                   cwk.getClassName()));
  -                arr.put(i, arr, cwk);
  -            }
  -            return arr;
  -        }
  -
  -        public void jsFunction_invalidate() throws Exception {
  -            ContinuationsManager contMgr = null;
  -            FOM_Cocoon cocoon = 
  -                (FOM_Cocoon)getProperty(getTopLevelScope(this),
  -                                        "cocoon");
  -            ComponentManager componentManager = 
  -                cocoon.getComponentManager();
  -            contMgr = (ContinuationsManager)
  -                componentManager.lookup(ContinuationsManager.ROLE);
  -            contMgr.invalidateWebContinuation(wk);
  -        }
  -
  -        public WebContinuation getWebContinuation() {
  -            return wk;
  -        }
  -    }
  -
       public FOM_Request jsGet_request() {
           if (request != null) {
               return request;
  @@ -814,11 +755,11 @@
        * the Sitemap parameters from <map:call>
        */
       public Scriptable jsGet_parameters() {
  -     return parameters;
  +        return parameters;
       }
   
       void setParameters(Scriptable value) {
  -     parameters = value;
  +        parameters = value;
       }
   
       // unwrap Wrapper's and convert undefined to null
  @@ -833,36 +774,123 @@
   
       // Make everything available to JavaScript objects implemented in Java:
   
  +    /**
  +     * Get the current request
  +     * @return The request
  +     */
       public Request getRequest() {
           return jsGet_request().request;
       }
   
  +    /**
  +     * Get the current session
  +     * @return The session (may be null)
  +     */
       public Session getSession() {
  +        if (getRequest().getSession(false) == null) {
  +            return null;
  +        }
           return jsGet_session().session;
       }
   
  +    /**
  +     * Get the current response
  +     * @return The response
  +     */
       public Response getResponse() {
           return jsGet_response().response;
       }
   
  +    /**
  +     * Get the current context
  +     * @return The context
  +     */
       public org.apache.cocoon.environment.Context getContext() {
           return jsGet_context().context;
       }
   
  -    public Environment getEnvironment() {
  -        return environment;
  +    /**
  +     * Get the current object model
  +     * @return The object model
  +     */
  +    public Map getObjectModel() {
  +        return environment.getObjectModel();
       }
   
  +    /**
  +     * Get the current Sitemap's component manager
  +     * @return The component manager
  +     */
  +
       public ComponentManager getComponentManager() {
           return componentManager;
       }
   
  -    public FOM_JavaScriptInterpreter getInterpreter() {
  -        return interpreter;
  +    /**
  +     * Call the Cocoon Sitemap to process a page
  +     * @param uri Uri to match
  +     * @param bean Input to page
  +     * @param fom_wk Current Web continuation (may be null)
  +     */
  +
  +    public void forwardTo(String uri,
  +                          Object bean,
  +                          FOM_WebContinuation fom_wk) 
  +        throws Exception {
  +        interpreter.forwardTo(getTopLevelScope(this),
  +                              this, 
  +                              "cocoon://" + environment.getURIPrefix() + uri,
  +                              bean,
  +                              fom_wk,
  +                              environment);
  +    }
  +
  +    /**
  +     * Perform the behavior of <map:call continuation="blah">
  +     * This can be used in cases where the continuation id is not encoded 
  +     * in the request in a form convenient to access in the sitemap.
  +     * Your script can extract the id from the request and then call 
  +     * this method to process it as normal.
  +     * @param kontId The continuation id
  +     * @param parameters Any parameters you want to pass to the continuation 
(may be null)
  +     */
  +    public void handleContinuation(String kontId, Scriptable parameters) 
  +        throws Exception {
  +        List list = null;
  +        if (parameters == null || parameters == Undefined.instance) {
  +            parameters = this.parameters;
  +        }
  +        Object[] ids = parameters.getIds();
  +        list = new ArrayList();
  +        for (int i = 0; i < ids.length; i++) {
  +            String name = ids[i].toString();
  +            Argument arg = new Argument(name,
  +                                        
org.mozilla.javascript.Context.toString(getProperty(parameters, name)));
  +            list.add(arg);
  +        }
  +        interpreter.handleContinuation(kontId, list, environment);
       }
   
  -    public FOM_WebContinuation makeWebContinuation(WebContinuation wk) {
  -        if (wk == null) return null;
  +    /**
  +     * Create a Web Continuation from a JS Continuation
  +     * @param k The JS continuation (may be null - null will be returned in 
that case)
  +     * @param parent The parent of this continuation (may be null)
  +     * @param timeToLive Lifetime for this continuation (zero means no limit)
  +     */
  +
  +
  +    public FOM_WebContinuation makeWebContinuation(Continuation k,
  +                                                   FOM_WebContinuation 
parent,
  +                                                   int timeToLive) 
  +        throws Exception {
  +        if (k == null) return null;
  +        WebContinuation wk;
  +        ContinuationsManager contMgr;
  +        contMgr = (ContinuationsManager)
  +            componentManager.lookup(ContinuationsManager.ROLE);
  +        wk = contMgr.createWebContinuation(unwrap(k),
  +                                           (WebContinuation)(parent == null 
? null : parent.getWebContinuation()),
  +                                           timeToLive);
           FOM_WebContinuation result = new FOM_WebContinuation(wk);
           result.setParentScope(getParentScope());
           result.setPrototype(getClassPrototype(getParentScope(), 
  
  
  
  1.4       +31 -17    
cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java
  
  Index: FOM_JavaScriptInterpreter.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FOM_JavaScriptInterpreter.java    17 Jul 2003 17:12:52 -0000      1.3
  +++ FOM_JavaScriptInterpreter.java    19 Jul 2003 20:16:27 -0000      1.4
  @@ -85,7 +85,7 @@
   import org.mozilla.javascript.continuations.Continuation;
   import org.mozilla.javascript.tools.ToolErrorReporter;
   import org.mozilla.javascript.tools.shell.Global;
  -import 
org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon.FOM_WebContinuation;
  +
   /**
    * Interface with the JavaScript interpreter.
    *
  @@ -344,7 +344,7 @@
           org.mozilla.javascript.Context context = 
               org.mozilla.javascript.Context.getCurrentContext();
   
  -        Scriptable thrScope = new ThreadScope();
  +        ThreadScope thrScope = new ThreadScope();
   
           thrScope.setPrototype(scope);
           // We want 'thrScope' to be a new top-level scope, so set its
  @@ -365,6 +365,7 @@
                                                       
ScriptableObject.DONTENUM |
                                                       
ScriptableObject.PERMANENT);
           
  +        thrScope.reset();
           return thrScope;
       }
   
  @@ -400,15 +401,15 @@
   
           FOM_Cocoon cocoon = (FOM_Cocoon)thrScope.get("cocoon", thrScope);
           long lastExecTime = ((Long)thrScope.get(LAST_EXEC_TIME,
  -                                           thrScope)).longValue();
  +                                                thrScope)).longValue();
           // We need to setup the FOM_Cocoon object according to the current
           // request. Everything else remains the same.
           cocoon.setup(this, environment, manager, getLogger());
  -
  +        
           // Check if we need to compile and/or execute scripts
           synchronized (compiledScripts) {
               List execList = new ArrayList();
  -            boolean needsRefresh = false;
  +            boolean needsRefresh = lastExecTime == 0;
               if (reloadScripts) {
                   long now = System.currentTimeMillis();
                   if (now >= lastTimeCheck + checkTime) {
  @@ -468,9 +469,9 @@
        * @param fileName resource uri
        * @return compiled script
        */
  -    public Script compileScript(Context cx,
  -                                Environment environment,
  -                                String fileName) throws Exception {
  +    Script compileScript(Context cx,
  +                         Environment environment,
  +                         String fileName) throws Exception {
           Source src = environment.resolveURI(fileName);
           if (src == null) {
               throw new ResourceNotFoundException(fileName + ": not found");
  @@ -621,8 +622,14 @@
                   }
               }
               cocoon.setParameters(parameters);
  -            Object[] args = new Object[] {k, 
  -                                          cocoon.makeWebContinuation(wk)};
  +            FOM_WebContinuation fom_wk = 
  +                new FOM_WebContinuation(wk);
  +            fom_wk.setParentScope(kScope);
  +            fom_wk.setPrototype(ScriptableObject.getClassPrototype(kScope, 
  +                                                                   
fom_wk.getClassName()));
  +                                  
  +            Object[] args = new Object[] {k, fom_wk};
  +
               try {
                   ScriptableObject.callMethod(cocoon, 
                                               "handleContinuation", 
  @@ -670,12 +677,14 @@
   
       public void forwardTo(Scriptable scope, FOM_Cocoon cocoon,
                             String uri, Object bizData,
  -                          WebContinuation continuation,
  +                          FOM_WebContinuation fom_wk,
                             Environment environment)
           throws Exception {
  -        setupView(scope, cocoon , environment, 
  -                  cocoon.makeWebContinuation(continuation));
  -        super.forwardTo(uri, bizData, continuation, environment);
  +        setupView(scope, cocoon, environment, fom_wk);
  +        super.forwardTo(uri, bizData, 
  +                        fom_wk == null ? null :
  +                           fom_wk.getWebContinuation(), 
  +                        environment);
       }
   
       // package access as this is called by FOM_Cocoon
  @@ -704,8 +713,13 @@
                                               cocoon.jsGet_request());
           FOM_JavaScriptFlowHelper.setFOM_Response(objectModel,
                                                cocoon.jsGet_response());
  -        FOM_JavaScriptFlowHelper.setFOM_Session(objectModel,
  -                                            cocoon.jsGet_session());
  +        Request request = ObjectModelHelper.getRequest(objectModel);
  +        Scriptable session = null;
  +        if (request.getSession(false) != null) {
  +            session = cocoon.jsGet_session();
  +        }
  +        FOM_JavaScriptFlowHelper.setFOM_Session(objectModel, session);
  +
           FOM_JavaScriptFlowHelper.setFOM_Context(objectModel,
                                                   cocoon.jsGet_context());
           if (kont != null) {
  
  
  
  1.1                  
cocoon-2.1/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_WebContinuation.java
  
  Index: FOM_WebContinuation.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.flow.javascript.fom;
  
  import java.util.List;
  import java.util.Iterator;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.cocoon.components.flow.ContinuationsManager;
  import org.apache.cocoon.components.flow.WebContinuation;
  import org.mozilla.javascript.*;
  import org.mozilla.javascript.continuations.Continuation;
  
  public class FOM_WebContinuation extends ScriptableObject {
      
      WebContinuation wk;
      
      public FOM_WebContinuation() {
      }
      
      public FOM_WebContinuation(Object wk) {
          if (wk instanceof Wrapper) {
              wk = ((Wrapper)wk).unwrap();
          }
          this.wk = (WebContinuation)wk;
      }
      
      public String getClassName() {
          return "FOM_WebContinuation";
      }
      
      public String jsGet_id() {
          return wk.getId();
      }
      
      public FOM_WebContinuation jsFunction_getParent() {
          WebContinuation parent = wk.getParentContinuation();
          if (parent == null) return null;
          FOM_WebContinuation pwk = new FOM_WebContinuation(parent);
          pwk.setParentScope(getParentScope());
          pwk.setPrototype(getClassPrototype(getParentScope(), 
                                             pwk.getClassName()));
          return pwk;
      }
      
      public NativeArray jsFunction_getChildren() throws Exception {
          List list = wk.getChildren();
          NativeArray arr = 
              
(NativeArray)org.mozilla.javascript.Context.getCurrentContext().newObject(getParentScope(),
                                                                                
        "Array",
                                                                                
        new Object[]{new Integer(list.size())});
          Iterator iter = list.iterator();
          for (int i = 0; iter.hasNext(); i++) {
              WebContinuation child = (WebContinuation)iter.next();
              FOM_WebContinuation cwk = new FOM_WebContinuation(child);
              cwk.setParentScope(getParentScope());
              cwk.setPrototype(getClassPrototype(getParentScope(), 
                                                 cwk.getClassName()));
              arr.put(i, arr, cwk);
          }
          return arr;
      }
      
      public void jsFunction_invalidate() throws Exception {
          ContinuationsManager contMgr = null;
          FOM_Cocoon cocoon = 
              (FOM_Cocoon)getProperty(getTopLevelScope(this),
                                      "cocoon");
          ComponentManager componentManager = 
              cocoon.getComponentManager();
          contMgr = (ContinuationsManager)
              componentManager.lookup(ContinuationsManager.ROLE);
          contMgr.invalidateWebContinuation(wk);
      }
      
      public WebContinuation getWebContinuation() {
          return wk;
      }
  }
  
  
  

Reply via email to