You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2010/10/03 12:03:14 UTC

svn commit: r1003943 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

Author: jleroux
Date: Sun Oct  3 10:03:14 2010
New Revision: 1003943

URL: http://svn.apache.org/viewvc?rev=1003943&view=rev
Log:
A patch from Sascha Rodekamp "Additional parameter for FreeMarker to tell if it should load ftl files from cache or not" (https://issues.apache.org/jira/browse/OFBIZ-3970) - OFBIZ-3970

This patch allows you to tell FreeMarker if it should load the ftl files from cache or not as extension for the method description. If you have a few screens which shouldn't use the cache you can configure them separately. The use case is loading content with templates which are stored in the DB and can be modified during the runtime (CMS). 

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=1003943&r1=1003942&r2=1003943&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java Sun Oct  3 10:03:14 2010
@@ -155,10 +155,22 @@ public class FreeMarkerWorker {
      * @param outWriter The Writer to render to
      */
     public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
+        renderTemplate(templateLocation, templateString, context, outWriter, true);
+    }
+    
+    /**
+     * Renders a template contained in a String.
+     * @param templateLocation A unique ID for this template - used for caching
+     * @param templateString The String containing the template
+     * @param context The context Map
+     * @param outWriter The Writer to render to
+     * @param useCache try to get template from cache
+     */
+    public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable outWriter, boolean useCache) throws TemplateException, IOException {
         if (UtilValidate.isEmpty(templateString)) {
             renderTemplate(templateLocation, context, outWriter);
         } else {
-            renderTemplateFromString(templateString, templateLocation, context, outWriter);
+            renderTemplateFromString(templateString, templateLocation, context, outWriter, useCache);
         }
     }
 
@@ -167,34 +179,46 @@ public class FreeMarkerWorker {
      * @param templateLocation A unique ID for this template - used for caching
      * @param context The context Map
      * @param outWriter The Writer to render to
+     * @param useCache try to get template from cache
      */
     public static void renderTemplate(String templateLocation, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
         Template template = getTemplate(templateLocation);
         renderTemplate(template, context, outWriter);
     }
 
-    public static void clearTemplateFromCache(String templateLocation) {
-        synchronized (cachedTemplates) {
-            cachedTemplates.remove(templateLocation);
-        }
-    }
 
-    public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
-        Template template = cachedTemplates.get(templateLocation);
+    public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object> context, Appendable outWriter, boolean useCache) throws TemplateException, IOException {
+        Template template = null;
+        if (useCache){
+            template = cachedTemplates.get(templateLocation);
+        }
         if (template == null) {
-            synchronized (cachedTemplates) {
-                template = cachedTemplates.get(templateLocation);
-                if (template == null) {
-                    Reader templateReader = new StringReader(templateString);
-                    template = new Template(templateLocation, templateReader, defaultOfbizConfig);
-                    templateReader.close();
-                    cachedTemplates.put(templateLocation, template);
+            if (useCache){
+                synchronized (cachedTemplates) {
+                    template = cachedTemplates.get(templateLocation);
+                    if (template == null) {
+                        Reader templateReader = new StringReader(templateString);
+                        template = new Template(templateLocation, templateReader, defaultOfbizConfig);
+                        templateReader.close();
+                        cachedTemplates.put(templateLocation, template);
+                    }
                 }
+            } else {
+                Reader templateReader = new StringReader(templateString);
+                template = new Template(templateLocation, templateReader, defaultOfbizConfig);
+                templateReader.close();
             }
         }
+
         return renderTemplate(template, context, outWriter);
     }
 
+    public static void clearTemplateFromCache(String templateLocation) {
+        synchronized (cachedTemplates) {
+            cachedTemplates.remove(templateLocation);
+        }
+    }
+
     /**
      * Renders a Template instance.
      * @param template A Template instance



Re: svn commit: r1003943 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

Posted by Jacques Le Roux <ja...@les7arts.com>.
Scott Gray wrote:
> Inline
>
> Regards
> Scott
> On 3/10/2010, at 11:03 PM, jleroux@apache.org wrote:
>
>> Author: jleroux
>> Date: Sun Oct  3 10:03:14 2010
>> New Revision: 1003943
>>
>> URL: http://svn.apache.org/viewvc?rev=1003943&view=rev
>> Log:
>> A patch from Sascha Rodekamp "Additional parameter for FreeMarker to tell if it should load ftl files from cache or not"
>> (https://issues.apache.org/jira/browse/OFBIZ-3970) - OFBIZ-3970
>>
>> This patch allows you to tell FreeMarker if it should load the ftl files from cache or not as extension for the method
>> description. If you have a few screens which shouldn't use the cache you can configure them separately. The use case is loading
>> content with templates which are stored in the DB and can be modified during the runtime (CMS).
>>
>> Modified:
>>    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
>>
>> Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
>> URL:
>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=1003943&r1=1003942&r2=1003943&view=diff
>> ============================================================================== ---
>> ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java (original) +++
>> ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java Sun Oct  3 10:03:14 2010 @@ -155,10 +155,22 @@
>> public class FreeMarkerWorker {
>>      * @param outWriter The Writer to render to
>>      */
>>     public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable
>> outWriter) throws TemplateException, IOException { +        renderTemplate(templateLocation, templateString, context, outWriter,
>> true); +    }
>> +
>> +    /**
>> +     * Renders a template contained in a String.
>> +     * @param templateLocation A unique ID for this template - used for caching
>> +     * @param templateString The String containing the template
>> +     * @param context The context Map
>> +     * @param outWriter The Writer to render to
>> +     * @param useCache try to get template from cache
>> +     */
>> +    public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable
>>         outWriter, boolean useCache) throws TemplateException, IOException { if (UtilValidate.isEmpty(templateString)) {
>>             renderTemplate(templateLocation, context, outWriter);
>>         } else {
>> -            renderTemplateFromString(templateString, templateLocation, context, outWriter);
>> +            renderTemplateFromString(templateString, templateLocation, context, outWriter, useCache);
>>         }
>>     }
>>
>> @@ -167,34 +179,46 @@ public class FreeMarkerWorker {
>>      * @param templateLocation A unique ID for this template - used for caching
>>      * @param context The context Map
>>      * @param outWriter The Writer to render to
>> +     * @param useCache try to get template from cache
>>      */
>>     public static void renderTemplate(String templateLocation, Map<String, Object> context, Appendable outWriter) throws
>>         TemplateException, IOException { Template template = getTemplate(templateLocation);
>>         renderTemplate(template, context, outWriter);
>>     }
>>
>> -    public static void clearTemplateFromCache(String templateLocation) {
>> -        synchronized (cachedTemplates) {
>> -            cachedTemplates.remove(templateLocation);
>> -        }
>> -    }
>>
>> -    public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object>
>> context, Appendable outWriter) throws TemplateException, IOException {
>> -        Template template = cachedTemplates.get(templateLocation);
>
> As I mentioned in the jira issue, the above method should have been deprecated and not replaced.

Right Scott, I pointed it out also and finally Sascha convinced me because I then looked at renderTemplate and not
renderTemplateFromString, no excuses anyway :/

Done at r1004148

Thanks

Jacques

>
>> +    public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object>
>> context, Appendable outWriter, boolean useCache) throws TemplateException, IOException { +        Template template = null;
>> +        if (useCache){
>> +            template = cachedTemplates.get(templateLocation);
>> +        }
>>         if (template == null) {
>> -            synchronized (cachedTemplates) {
>> -                template = cachedTemplates.get(templateLocation);
>> -                if (template == null) {
>> -                    Reader templateReader = new StringReader(templateString);
>> -                    template = new Template(templateLocation, templateReader, defaultOfbizConfig);
>> -                    templateReader.close();
>> -                    cachedTemplates.put(templateLocation, template);
>> +            if (useCache){
>> +                synchronized (cachedTemplates) {
>> +                    template = cachedTemplates.get(templateLocation);
>> +                    if (template == null) {
>> +                        Reader templateReader = new StringReader(templateString);
>> +                        template = new Template(templateLocation, templateReader, defaultOfbizConfig);
>> +                        templateReader.close();
>> +                        cachedTemplates.put(templateLocation, template);
>> +                    }
>>                 }
>> +            } else {
>> +                Reader templateReader = new StringReader(templateString);
>> +                template = new Template(templateLocation, templateReader, defaultOfbizConfig);
>> +                templateReader.close();
>>             }
>>         }
>> +
>>         return renderTemplate(template, context, outWriter);
>>     }
>>
>> +    public static void clearTemplateFromCache(String templateLocation) {
>> +        synchronized (cachedTemplates) {
>> +            cachedTemplates.remove(templateLocation);
>> +        }
>> +    }
>> +
>>     /**
>>      * Renders a Template instance.
>>      * @param template A Template instance



Re: svn commit: r1003943 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

Posted by Scott Gray <sc...@hotwaxmedia.com>.
Inline

Regards
Scott
On 3/10/2010, at 11:03 PM, jleroux@apache.org wrote:

> Author: jleroux
> Date: Sun Oct  3 10:03:14 2010
> New Revision: 1003943
> 
> URL: http://svn.apache.org/viewvc?rev=1003943&view=rev
> Log:
> A patch from Sascha Rodekamp "Additional parameter for FreeMarker to tell if it should load ftl files from cache or not" (https://issues.apache.org/jira/browse/OFBIZ-3970) - OFBIZ-3970
> 
> This patch allows you to tell FreeMarker if it should load the ftl files from cache or not as extension for the method description. If you have a few screens which shouldn't use the cache you can configure them separately. The use case is loading content with templates which are stored in the DB and can be modified during the runtime (CMS). 
> 
> Modified:
>    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
> 
> Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=1003943&r1=1003942&r2=1003943&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java (original)
> +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java Sun Oct  3 10:03:14 2010
> @@ -155,10 +155,22 @@ public class FreeMarkerWorker {
>      * @param outWriter The Writer to render to
>      */
>     public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
> +        renderTemplate(templateLocation, templateString, context, outWriter, true);
> +    }
> +    
> +    /**
> +     * Renders a template contained in a String.
> +     * @param templateLocation A unique ID for this template - used for caching
> +     * @param templateString The String containing the template
> +     * @param context The context Map
> +     * @param outWriter The Writer to render to
> +     * @param useCache try to get template from cache
> +     */
> +    public static void renderTemplate(String templateLocation, String templateString, Map<String, Object> context, Appendable outWriter, boolean useCache) throws TemplateException, IOException {
>         if (UtilValidate.isEmpty(templateString)) {
>             renderTemplate(templateLocation, context, outWriter);
>         } else {
> -            renderTemplateFromString(templateString, templateLocation, context, outWriter);
> +            renderTemplateFromString(templateString, templateLocation, context, outWriter, useCache);
>         }
>     }
> 
> @@ -167,34 +179,46 @@ public class FreeMarkerWorker {
>      * @param templateLocation A unique ID for this template - used for caching
>      * @param context The context Map
>      * @param outWriter The Writer to render to
> +     * @param useCache try to get template from cache
>      */
>     public static void renderTemplate(String templateLocation, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
>         Template template = getTemplate(templateLocation);
>         renderTemplate(template, context, outWriter);
>     }
> 
> -    public static void clearTemplateFromCache(String templateLocation) {
> -        synchronized (cachedTemplates) {
> -            cachedTemplates.remove(templateLocation);
> -        }
> -    }
> 
> -    public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object> context, Appendable outWriter) throws TemplateException, IOException {
> -        Template template = cachedTemplates.get(templateLocation);

As I mentioned in the jira issue, the above method should have been deprecated and not replaced.

> +    public static Environment renderTemplateFromString(String templateString, String templateLocation, Map<String, Object> context, Appendable outWriter, boolean useCache) throws TemplateException, IOException {
> +        Template template = null;
> +        if (useCache){
> +            template = cachedTemplates.get(templateLocation);
> +        }
>         if (template == null) {
> -            synchronized (cachedTemplates) {
> -                template = cachedTemplates.get(templateLocation);
> -                if (template == null) {
> -                    Reader templateReader = new StringReader(templateString);
> -                    template = new Template(templateLocation, templateReader, defaultOfbizConfig);
> -                    templateReader.close();
> -                    cachedTemplates.put(templateLocation, template);
> +            if (useCache){
> +                synchronized (cachedTemplates) {
> +                    template = cachedTemplates.get(templateLocation);
> +                    if (template == null) {
> +                        Reader templateReader = new StringReader(templateString);
> +                        template = new Template(templateLocation, templateReader, defaultOfbizConfig);
> +                        templateReader.close();
> +                        cachedTemplates.put(templateLocation, template);
> +                    }
>                 }
> +            } else {
> +                Reader templateReader = new StringReader(templateString);
> +                template = new Template(templateLocation, templateReader, defaultOfbizConfig);
> +                templateReader.close();
>             }
>         }
> +
>         return renderTemplate(template, context, outWriter);
>     }
> 
> +    public static void clearTemplateFromCache(String templateLocation) {
> +        synchronized (cachedTemplates) {
> +            cachedTemplates.remove(templateLocation);
> +        }
> +    }
> +
>     /**
>      * Renders a Template instance.
>      * @param template A Template instance
> 
>