You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2003/10/15 19:04:50 UTC

cvs commit: cocoon-2.2/src/java/org/apache/cocoon/components/flow/javascript/fom FOM_JavaScriptInterpreter.java FOM_Cocoon.java

cziegeler    2003/10/15 10:04:50

  Modified:    src/java/org/apache/cocoon/components/flow
                        AbstractInterpreter.java
               src/java/org/apache/cocoon/components/flow/javascript
                        JavaScriptInterpreter.java
               src/java/org/apache/cocoon/components/flow/javascript/fom
                        FOM_JavaScriptInterpreter.java FOM_Cocoon.java
  Added:       src/java/org/apache/cocoon/components/flow
                        CompilingInterpreter.java
  Log:
  Syncing
  
  Revision  Changes    Path
  1.10      +20 -11    cocoon-2.2/src/java/org/apache/cocoon/components/flow/AbstractInterpreter.java
  
  Index: AbstractInterpreter.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/flow/AbstractInterpreter.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- AbstractInterpreter.java	8 Oct 2003 20:18:34 -0000	1.9
  +++ AbstractInterpreter.java	15 Oct 2003 17:04:50 -0000	1.10
  @@ -45,6 +45,7 @@
   */
   package org.apache.cocoon.components.flow;
   
  +import org.apache.avalon.framework.activity.Disposable;
   import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
  @@ -82,7 +83,7 @@
    */
   public abstract class AbstractInterpreter extends AbstractLogEnabled
     implements Component, Composable, Contextualizable, Interpreter,
  -             SingleThreaded, Configurable
  +             SingleThreaded, Configurable, Disposable
   {
       /**
        * List of source locations that need to be resolved.
  @@ -92,7 +93,7 @@
       protected org.apache.cocoon.environment.Context context;
       protected ComponentManager manager;
       protected ContinuationsManager continuationsMgr;
  -
  +    
       /**
        * Whether reloading of scripts should be done. Specified through
        * the "reload-scripts" attribute in <code>flow.xmap</code>.
  @@ -110,20 +111,28 @@
           checkTime = config.getChild("check-time").getValueAsLong(1000L);
       }
   
  +    /**
  +     * Composable
  +     */
       public void compose(ComponentManager manager) throws ComponentException {
           this.manager = manager;
  -
  -        // FIXME: Why is the manager null here?
  -        if (manager != null) {
  -            continuationsMgr
  -                    = (ContinuationsManager)manager.lookup(ContinuationsManager.ROLE);
  -        }
  +        this.continuationsMgr = (ContinuationsManager)manager.lookup(ContinuationsManager.ROLE);
       }
   
       public void contextualize(org.apache.avalon.framework.context.Context context)
  -            throws ContextException
  -    {
  +    throws ContextException{
           this.context = (Context)context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.avalon.framework.activity.Disposable#dispose()
  +     */
  +    public void dispose() {
  +        if ( this.manager != null ) {
  +            this.manager.release( (Component)this.continuationsMgr );
  +            this.continuationsMgr = null;
  +            this.manager = null;
  +        }
       }
   
       /**
  
  
  
  1.1                  cocoon-2.2/src/java/org/apache/cocoon/components/flow/CompilingInterpreter.java
  
  Index: CompilingInterpreter.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
      apache@apache.org.
  
   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.
  
  */
  package org.apache.cocoon.components.flow;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceResolver;
  import org.mozilla.javascript.Context;
  import org.mozilla.javascript.Script;
  import org.mozilla.javascript.Scriptable;
  
  /**
   * This is a base class for all interpreters compiling the scripts
   *
   * @author <a href="mailto:ovidiu@apache.org">Ovidiu Predescu</a>
   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Id: CompilingInterpreter.java,v 1.1 2003/10/15 17:04:50 cziegeler Exp $
   */
  public abstract class CompilingInterpreter 
  extends AbstractInterpreter {
  
      /** A source resolver */
      protected SourceResolver sourceresolver;
      /**
       * Mapping of String objects (source uri's) to ScriptSourceEntry's
       *
       */
      protected Map compiledScripts = new HashMap();
  
      /**
       * Composable
       */
      public void compose(ComponentManager manager) throws ComponentException {
          super.compose(manager);
          this.sourceresolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE);
      }
  
      /* (non-Javadoc)
       * @see org.apache.avalon.framework.activity.Disposable#dispose()
       */
      public void dispose() {
          if ( this.compiledScripts != null ) {
              Iterator iter = this.compiledScripts.values().iterator();
              while (iter.hasNext()) {
                  ScriptSourceEntry current = (ScriptSourceEntry)iter.next();
                  this.sourceresolver.release(current.getSource());
              }
              this.compiledScripts = null;
          }
          if ( this.manager != null ) {
              this.manager.release( this.sourceresolver );
              this.sourceresolver = null;
          }
          super.dispose();
      }
      
      /**
       * TODO - This is a little bit strange, the interpreter calls
       * get Script on the ScriptSourceEntry which in turn
       * calls compileScript on the interpreter. I think we
       * need more refactoring here.
       */
      protected abstract Script compileScript(Context context, 
                                             Scriptable scope, 
                                             Source source) throws Exception;
      
      protected class ScriptSourceEntry {
          final private Source source;
          private Script script;
          private long compileTime;
  
          public ScriptSourceEntry(Source source) {
              this.source = source;
          }
  
          public ScriptSourceEntry(Source source, Script script, long t) {
              this.source = source;
              this.script = script;
              this.compileTime = t;
          }
  
          public Source getSource() {
              return source;
          }
  
          public Script getScript(Context context, Scriptable scope,
                                  boolean refresh, CompilingInterpreter interpreter)
              throws Exception {
              if (refresh) {
                  source.refresh();
              }
              if (script == null || compileTime < source.getLastModified()) {
                  script = interpreter.compileScript(context, scope, source);
                  compileTime = source.getLastModified();
              }
              return script;
          }
      }
      
  }
  
  
  
  1.22      +16 -63    cocoon-2.2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java
  
  Index: JavaScriptInterpreter.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- JavaScriptInterpreter.java	24 Sep 2003 20:38:09 -0000	1.21
  +++ JavaScriptInterpreter.java	15 Oct 2003 17:04:50 -0000	1.22
  @@ -1,4 +1,4 @@
  -/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  +/* 
   
    ============================================================================
                      The Apache Software License, Version 1.1
  @@ -59,9 +59,9 @@
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  -import org.apache.cocoon.ResourceNotFoundException;
   import org.apache.cocoon.ProcessingException;
  -import org.apache.cocoon.components.flow.AbstractInterpreter;
  +import org.apache.cocoon.ResourceNotFoundException;
  +import org.apache.cocoon.components.flow.CompilingInterpreter;
   import org.apache.cocoon.components.flow.Interpreter;
   import org.apache.cocoon.components.flow.InvalidContinuationException;
   import org.apache.cocoon.components.flow.WebContinuation;
  @@ -72,19 +72,7 @@
   import org.apache.commons.jxpath.JXPathIntrospector;
   import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
   import org.apache.excalibur.source.Source;
  -
  -import org.mozilla.javascript.Context;
  -import org.mozilla.javascript.EcmaError;
  -import org.mozilla.javascript.EvaluatorException;
  -import org.mozilla.javascript.Function;
  -import org.mozilla.javascript.JavaScriptException;
  -import org.mozilla.javascript.NativeArray;
  -import org.mozilla.javascript.PropertyException;
  -import org.mozilla.javascript.Script;
  -import org.mozilla.javascript.ScriptRuntime;
  -import org.mozilla.javascript.Scriptable;
  -import org.mozilla.javascript.ScriptableObject;
  -import org.mozilla.javascript.Wrapper;
  +import org.mozilla.javascript.*;
   import org.mozilla.javascript.tools.ToolErrorReporter;
   
   /**
  @@ -95,7 +83,7 @@
    * @since March 25, 2002
    * @version CVS $Id$
    */
  -public class JavaScriptInterpreter extends AbstractInterpreter
  +public class JavaScriptInterpreter extends CompilingInterpreter
       implements Configurable, Initializable
   {
   
  @@ -134,44 +122,6 @@
        */
       List topLevelScripts = new ArrayList();
   
  -    class ScriptSourceEntry {
  -        final private Source source;
  -        private Script script;
  -        private long compileTime;
  -
  -        public ScriptSourceEntry(Source source) {
  -            this.source = source;
  -        }
  -
  -        public ScriptSourceEntry(Source source, Script script, long t) {
  -            this.source = source;
  -            this.script = script;
  -            this.compileTime = t;
  -        }
  -
  -        public Source getSource() {
  -            return source;
  -        }
  -
  -        public Script getScript(Context context, Scriptable scope,
  -                                             boolean refresh)
  -            throws Exception {
  -            if (refresh) {
  -                source.refresh();
  -            }
  -            if (script == null || compileTime < source.getLastModified()) {
  -                script = compileScript(context, scope, source);
  -                compileTime = source.getLastModified();
  -            }
  -            return script;
  -        }
  -    }
  -    /**
  -     * Mapping of String objects (source uri's) to ScriptSourceEntry's
  -     *
  -     */
  -    Map compiledScripts = new HashMap();
  -
       JSErrorReporter errorReporter;
       boolean enableDebugger = false;
       /**
  @@ -452,12 +402,12 @@
                   ScriptSourceEntry entry =
                       (ScriptSourceEntry)compiledScripts.get(sourceURI);
                   if (entry == null) {
  -                    Source src = environment.resolveURI(sourceURI);
  +                    Source src = this.sourceresolver.resolveURI(sourceURI);
                       entry = new ScriptSourceEntry(src);
                       compiledScripts.put(sourceURI, entry);
                   }
                   // Compile the script if necessary
  -                entry.getScript(context, this.scope, needsRefresh);
  +                entry.getScript(context, this.scope, needsRefresh, this);
               }
               // Execute the scripts if necessary
               for (int i = 0, size = execList.size(); i < size; i++) {
  @@ -465,7 +415,7 @@
                   ScriptSourceEntry entry =
                       (ScriptSourceEntry)compiledScripts.get(sourceURI);
                   long lastMod = entry.getSource().getLastModified();
  -                Script script = entry.getScript(context, this.scope, false);
  +                Script script = entry.getScript(context, this.scope, false, this);
                   if (lastExecTime == 0 || lastMod > lastExecTime) {
                       script.exec(context, thrScope);
                   }
  @@ -501,7 +451,7 @@
       public Script compileScript(Context cx,
                                   Environment environment,
                                   String fileName) throws Exception {
  -        Source src = environment.resolveURI(fileName);
  +        Source src = this.sourceresolver.resolveURI(fileName);
           if (src == null) {
               throw new ResourceNotFoundException(fileName + ": not found");
           }
  @@ -512,14 +462,16 @@
               if (entry == null) {
                   compiledScripts.put(src.getURI(),
                                       entry = new ScriptSourceEntry(src));
  +            } else {
  +                this.sourceresolver.release(src);
               }
  -            compiledScript = entry.getScript(cx, this.scope, false);
  +            compiledScript = entry.getScript(cx, this.scope, false, this);
               return compiledScript;
           }
       }
   
  -    private Script compileScript(Context cx, Scriptable scope,
  -                                 Source src) throws Exception {
  +    protected Script compileScript(Context cx, Scriptable scope,
  +                                  Source src) throws Exception {
           InputStream is = src.getInputStream();
           if (is == null) {
               throw new ResourceNotFoundException(src.getURI() + ": not found");
  @@ -719,4 +671,5 @@
                                                                      "java"));
           super.forwardTo(uri, bizData, continuation, environment);
       }
  +    
   }
  
  
  
  1.11      +12 -48    cocoon-2.2/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java
  
  Index: FOM_JavaScriptInterpreter.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_JavaScriptInterpreter.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- FOM_JavaScriptInterpreter.java	29 Sep 2003 12:56:05 -0000	1.10
  +++ FOM_JavaScriptInterpreter.java	15 Oct 2003 17:04:50 -0000	1.11
  @@ -67,7 +67,7 @@
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.ResourceNotFoundException;
  -import org.apache.cocoon.components.flow.AbstractInterpreter;
  +import org.apache.cocoon.components.flow.CompilingInterpreter;
   import org.apache.cocoon.components.flow.Interpreter;
   import org.apache.cocoon.components.flow.InvalidContinuationException;
   import org.apache.cocoon.components.flow.WebContinuation;
  @@ -94,7 +94,7 @@
    * @since March 25, 2002
    * @version CVS $Id$
    */
  -public class FOM_JavaScriptInterpreter extends AbstractInterpreter
  +public class FOM_JavaScriptInterpreter extends CompilingInterpreter
       implements Configurable, Initializable
   {
   
  @@ -134,44 +134,6 @@
        */
       List topLevelScripts = new ArrayList();
   
  -    class ScriptSourceEntry {
  -        final private Source source;
  -        private Script script;
  -        private long compileTime;
  -
  -        public ScriptSourceEntry(Source source) {
  -            this.source = source;
  -        }
  -
  -        public ScriptSourceEntry(Source source, Script script, long t) {
  -            this.source = source;
  -            this.script = script;
  -            this.compileTime = t;
  -        }
  -
  -        public Source getSource() {
  -            return source;
  -        }
  -
  -        public Script getScript(Context context, Scriptable scope,
  -                                boolean refresh)
  -            throws Exception {
  -            if (refresh) {
  -                source.refresh();
  -            }
  -            if (script == null || compileTime < source.getLastModified()) {
  -                script = compileScript(context, scope, source);
  -                compileTime = source.getLastModified();
  -            }
  -            return script;
  -        }
  -    }
  -
  -    /**
  -     * Mapping of String objects (source uri's) to ScriptSourceEntry's
  -     */
  -    Map compiledScripts = new HashMap();
  -
       JSErrorReporter errorReporter;
       boolean enableDebugger = false;
   
  @@ -435,12 +397,12 @@
                   ScriptSourceEntry entry =
                       (ScriptSourceEntry)compiledScripts.get(sourceURI);
                   if (entry == null) {
  -                    Source src = environment.resolveURI(sourceURI);
  +                    Source src = this.sourceresolver.resolveURI(sourceURI);
                       entry = new ScriptSourceEntry(src);
                       compiledScripts.put(sourceURI, entry);
                   }
                   // Compile the script if necessary
  -                entry.getScript(context, this.scope, needsRefresh);
  +                entry.getScript(context, this.scope, needsRefresh, this);
               }
               // Execute the scripts if necessary
               for (int i = 0, size = execList.size(); i < size; i++) {
  @@ -448,7 +410,7 @@
                   ScriptSourceEntry entry =
                       (ScriptSourceEntry)compiledScripts.get(sourceURI);
                   long lastMod = entry.getSource().getLastModified();
  -                Script script = entry.getScript(context, this.scope, false);
  +                Script script = entry.getScript(context, this.scope, false, this);
                   if (lastExecTime == 0 || lastMod > lastExecTime) {
                       script.exec(context, thrScope);
                       thrScope.put(LAST_EXEC_TIME, thrScope,
  @@ -470,7 +432,7 @@
       Script compileScript(Context cx,
                            Environment environment,
                            String fileName) throws Exception {
  -        Source src = environment.resolveURI(fileName);
  +        Source src = this.sourceresolver.resolveURI(fileName);
           if (src == null) {
               throw new ResourceNotFoundException(fileName + ": not found");
           }
  @@ -481,14 +443,16 @@
               if (entry == null) {
                   compiledScripts.put(src.getURI(),
                                       entry = new ScriptSourceEntry(src));
  +            } else {
  +                this.sourceresolver.release(src);
               }
  -            compiledScript = entry.getScript(cx, this.scope, false);
  +            compiledScript = entry.getScript(cx, this.scope, false, this);
               return compiledScript;
           }
       }
   
  -    private Script compileScript(Context cx, Scriptable scope,
  -                                 Source src) throws Exception {
  +    protected Script compileScript(Context cx, Scriptable scope,
  +                                   Source src) throws Exception {
           InputStream is = src.getInputStream();
           if (is == null) {
               throw new ResourceNotFoundException(src.getURI() + ": not found");
  
  
  
  1.13      +5 -5      cocoon-2.2/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java
  
  Index: FOM_Cocoon.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/flow/javascript/fom/FOM_Cocoon.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- FOM_Cocoon.java	29 Sep 2003 12:56:05 -0000	1.12
  +++ FOM_Cocoon.java	15 Oct 2003 17:04:50 -0000	1.13
  @@ -122,9 +122,9 @@
       }
   
       void setup(FOM_JavaScriptInterpreter interp,
  -                      Environment env, 
  -                      ComponentManager manager,
  -                      Logger logger) {
  +               Environment env, 
  +               ComponentManager manager,
  +               Logger logger) {
           this.interpreter = interp;
           this.environment = env;
           this.componentManager = manager;
  @@ -987,7 +987,7 @@
           contMgr = (ContinuationsManager)
               componentManager.lookup(ContinuationsManager.ROLE);
           wk = contMgr.createWebContinuation(unwrap(k),
  -                                           (WebContinuation)(parent == null ? null : parent.getWebContinuation()),
  +                                           (parent == null ? null : parent.getWebContinuation()),
                                              timeToLive,
                                              null);
           FOM_WebContinuation result = new FOM_WebContinuation(wk);