Please also review the following commit because it is another pattern I am 
going to apply to other code:


On May 28, 2012, at 5:25 PM, [email protected] wrote:

> Author: jacopoc
> Date: Mon May 28 15:25:46 2012
> New Revision: 1343278
> 
> URL: http://svn.apache.org/viewvc?rev=1343278&view=rev
> Log:
> Improved code that manages the cache:
> * protected the UtilCache object (static field) by making it private and final
> * instead of using the "put" method, use the "putIfAbsentAndGet" method to 
> prevent the risk of adding the new value if another concurrent thread added 
> it in the meantime
> 
> 
> Modified:
>    
> ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/XslTransform.java
> 
> Modified: 
> ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/XslTransform.java
> URL: 
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/XslTransform.java?rev=1343278&r1=1343277&r2=1343278&view=diff
> ==============================================================================
> --- 
> ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/XslTransform.java 
> (original)
> +++ 
> ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/XslTransform.java 
> Mon May 28 15:25:46 2012
> @@ -56,7 +56,7 @@ import javax.xml.transform.stream.Stream
> public final class XslTransform {
> 
>     public static final String module = XslTransform.class.getName();
> -    public static UtilCache<String, Templates> xslTemplatesCache = 
> UtilCache.createUtilCache("XsltTemplates", 0, 0);
> +    private static final UtilCache<String, Templates> xslTemplatesCache = 
> UtilCache.createUtilCache("XsltTemplates", 0, 0);
> 
>     /**
>      * @param template the content or url of the xsl template
> @@ -116,7 +116,7 @@ public final class XslTransform {
>             Source templateSource = getSource(templateDocument, templateUrl, 
> templateString);
>             translet = tFactory.newTemplates(templateSource);
>             if (UtilValidate.isNotEmpty(templateName)) {
> -                    xslTemplatesCache.put(templateName, translet);
> +                translet = xslTemplatesCache.putIfAbsentAndGet(templateName, 
> translet);
>             }
>         }
>         if (translet != null) {
> 
> 

Jacopo

Reply via email to