cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leszek Gawron <lgaw...@mobilebox.pl>
Subject Re: JavaScript expressions in JXTG (was Re: svn commit: r325889)
Date Mon, 17 Oct 2005 19:29:24 GMT
Sylvain Wallez wrote:
> Leszek Gawron wrote:
> 
>> Sylvain Wallez wrote:
>>
>>> Actually, I'm more and more annoyed by Jexl: JXTemplate is the *only* 
>>> place in Cocoon where Jexl is used, and I would love to see it 
>>> replaced by JavaScript expressions. Script/expression language 
>>> consistency throughout Cocoon...
>>
>> hmmm... we have touched the subject once. At the time you have pointed 
>> me with some javascript helper class to get the basics of rhino but I 
>> lost that message. Do you happen to remember what class it was?
>>
>> This shouldn't be 'that' hard.
> 
> 
> Oh no, damn easy! See JavaScriptHelper in CForms.
Hmm.. looking at JavaScriptHelper I see you broke it again :)

> Index: C:/dev/apache-projects/cocoon-2.2.x/src/blocks/forms/trunk/java/org/apache/cocoon/forms/util/JavaScriptHelper.java
> ===================================================================
> --- C:/dev/apache-projects/cocoon-2.2.x/src/blocks/forms/trunk/java/org/apache/cocoon/forms/util/JavaScriptHelper.java
(revision 289537)
> +++ C:/dev/apache-projects/cocoon-2.2.x/src/blocks/forms/trunk/java/org/apache/cocoon/forms/util/JavaScriptHelper.java
(revision 289538)
> @@ -23,7 +23,6 @@
>  import org.apache.avalon.framework.CascadingRuntimeException;
>  import org.apache.cocoon.components.flow.FlowHelper;
>  import org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptFlowHelper;
> -import org.apache.cocoon.environment.TemplateObjectModelHelper;
>  import org.mozilla.javascript.Context;
>  import org.mozilla.javascript.Function;
>  import org.mozilla.javascript.JavaScriptException;
> @@ -39,7 +38,13 @@
>   * @version $Id$
>   */
>  public class JavaScriptHelper {
> +
>      /**
> +     * A shared root scope, avoiding to recreate a new one each time.
> +     */
> +    private static Scriptable _rootScope = null;
> +
> +    /**
>       * Build a script with the content of a DOM element.
>       * 
>       * @param element the element containing the script
> @@ -111,7 +116,20 @@
>       * @return an appropriate root scope
>       */
>      public static Scriptable getRootScope() {
> -        return TemplateObjectModelHelper.getScope();
> +        // FIXME: TemplateOMH should be used in 2.2
> +        //return TemplateObjectModelHelper.getScope();
> +        
> +        
> +        if (_rootScope == null) {
> +            // Create it if never used up to now
> +            Context ctx = Context.enter();
> +            try {
> +                _rootScope = ctx.initStandardObjects(null);
> +            } finally {
> +                Context.exit();
> +            }
> +        }
> +        return _rootScope;
>      }
>  
>      /**

this is the former logic of JavaScriptHelper which is invalid because of 
the fact that _rootScope is not thread safe. Please have look at how 
TemplateOMH does it:

     public static Scriptable getScope() {
         Context ctx = Context.enter();
         try {
             // Create it if never used up to now
             if (rootScope == null)
                 rootScope = ctx.initStandardObjects(null);

             Scriptable scope = ctx.newObject(rootScope);
             scope.setPrototype(rootScope);
             scope.setParentScope(null);
             return scope;
         } finally {
             Context.exit();
         }
     }

(shouldn't we wrap this code with synchronized block?)

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Mime
View raw message