EXTVAL-80 alternative to ExtValRendererProxy (for #getConvertedValue)

git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/validator/trunk@981353 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ConvertedValueCache.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ConvertedValueCache.java
new file mode 100644
index 0000000..ead681a
--- /dev/null
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ConvertedValueCache.java
@@ -0,0 +1,61 @@
+/*

+ * Licensed to the Apache Software Foundation (ASF) under one

+ * or more contributor license agreements.  See the NOTICE file

+ * distributed with this work for additional information

+ * regarding copyright ownership.  The ASF licenses this file

+ * to you under the Apache License, Version 2.0 (the

+ * "License"); you may not use this file except in compliance

+ * with the License.  You may obtain a copy of the License at

+ *

+ *   http://www.apache.org/licenses/LICENSE-2.0

+ *

+ * Unless required by applicable law or agreed to in writing,

+ * software distributed under the License is distributed on an

+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

+ * KIND, either express or implied.  See the License for the

+ * specific language governing permissions and limitations

+ * under the License.

+ */

+package org.apache.myfaces.extensions.validator.core.renderkit;

+

+/**

+ * @author Gerhard Petracek

+ */

+public class ConvertedValueCache

+{

+    private static ThreadLocal<ConvertedValueCacheEntry> value = new ThreadLocal<ConvertedValueCacheEntry>();

+

+    public static void reset()

+    {

+        value.set(null);

+        value.remove();

+    }

+

+    //needed because null is a valid value

+    static boolean isCachedValueAvailable()

+    {

+        return getCacheEntry().isCachedValueAvailable();

+    }

+

+    static Object getCachedValue()

+    {

+        return getCacheEntry().getCachedValue();

+    }

+

+    static void setCachedValue(Object convertedObject)

+    {

+        getCacheEntry().setCachedValue(convertedObject);

+    }

+

+    private static ConvertedValueCacheEntry getCacheEntry()

+    {

+        ConvertedValueCacheEntry entry = value.get();

+

+        if(entry == null)

+        {

+            entry = new ConvertedValueCacheEntry();

+            value.set(entry);

+        }

+        return entry;

+    }

+}

diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ConvertedValueCacheEntry.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ConvertedValueCacheEntry.java
new file mode 100644
index 0000000..3ef2da6
--- /dev/null
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ConvertedValueCacheEntry.java
@@ -0,0 +1,44 @@
+/*

+ * Licensed to the Apache Software Foundation (ASF) under one

+ * or more contributor license agreements.  See the NOTICE file

+ * distributed with this work for additional information

+ * regarding copyright ownership.  The ASF licenses this file

+ * to you under the Apache License, Version 2.0 (the

+ * "License"); you may not use this file except in compliance

+ * with the License.  You may obtain a copy of the License at

+ *

+ *   http://www.apache.org/licenses/LICENSE-2.0

+ *

+ * Unless required by applicable law or agreed to in writing,

+ * software distributed under the License is distributed on an

+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

+ * KIND, either express or implied.  See the License for the

+ * specific language governing permissions and limitations

+ * under the License.

+ */

+package org.apache.myfaces.extensions.validator.core.renderkit;

+

+/**

+ * @author Gerhard Petracek

+ */

+class ConvertedValueCacheEntry

+{

+    private boolean cachedValueAvailable;

+    private Object cachedValue;

+

+    void setCachedValue(Object cachedValue)

+    {

+        this.cachedValue = cachedValue;

+        this.cachedValueAvailable = true;

+    }

+

+    public Object getCachedValue()

+    {

+        return cachedValue;

+    }

+

+    public boolean isCachedValueAvailable()

+    {

+        return cachedValueAvailable;

+    }

+}

diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValLazyRendererProxy.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValLazyRendererProxy.java
index 0191f92..68551c4 100644
--- a/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValLazyRendererProxy.java
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValLazyRendererProxy.java
@@ -92,17 +92,35 @@
     public Object getCachedConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object o)
         throws ConverterException
     {
-        if(getLazyRenderer() instanceof RendererProxy)
+        Renderer renderer = getLazyRenderer();
+        if(renderer instanceof RendererProxy)
         {
             return ((RendererProxy)getLazyRenderer()).getCachedConvertedValue(facesContext, uiComponent, o);
         }
-        return getLazyRenderer().getConvertedValue(facesContext, uiComponent, o);
+
+        //by default there is no proxy - so we use a local cache
+        if(ConvertedValueCache.isCachedValueAvailable())
+        {
+            return ConvertedValueCache.getCachedValue();
+        }
+
+        Object result = renderer.getConvertedValue(facesContext, uiComponent, o);
+        ConvertedValueCache.setCachedValue(result);
+        return result;
     }
 
     @Override
     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object o)
         throws ConverterException
     {
+        if(getLazyRenderer() == this.wrapped)
+        {
+            if(ConvertedValueCache.isCachedValueAvailable())
+            {
+                return ConvertedValueCache.getCachedValue();
+            }
+        }
+
         return getLazyRenderer().getConvertedValue(facesContext, uiComponent, o);
     }
 
@@ -137,4 +155,9 @@
     {
         return this.wrapped;
     }
+
+    void resetConvertedValueCache()
+    {
+        ConvertedValueCache.reset();
+    }
 }
\ No newline at end of file
diff --git a/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValRendererWrapper.java b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValRendererWrapper.java
index 52fdfc1..1da840e 100644
--- a/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValRendererWrapper.java
+++ b/core/src/main/java/org/apache/myfaces/extensions/validator/core/renderkit/ExtValRendererWrapper.java
@@ -350,59 +350,71 @@
 
         try
         {
-            for(RendererInterceptor rendererInterceptor : extValContext.getRendererInterceptors())
+            try
             {
-                logger.finest("start beforeGetConvertedValue of " + rendererInterceptor.getClass().getName());
-
-                try
+                for(RendererInterceptor rendererInterceptor : extValContext.getRendererInterceptors())
                 {
-                    rendererInterceptor.beforeGetConvertedValue(facesContext, uiComponent, o, this.wrapped);
-                }
-                catch (SkipRendererDelegationException e)
-                {
-                    convertedObject = e.getReturnValueOnException(convertedObject);
+                    logger.finest("start beforeGetConvertedValue of " + rendererInterceptor.getClass().getName());
 
-                    logger.log(Level.FINEST, "getConvertedValue delegation canceled", e);
-
-                    delegateToWrappedRenderer = false;
-
-                    if(e.isSkipOtherInterceptors())
+                    try
                     {
-                        break;
+                        rendererInterceptor.beforeGetConvertedValue(facesContext, uiComponent, o, this.wrapped);
                     }
+                    catch (SkipRendererDelegationException e)
+                    {
+                        convertedObject = e.getReturnValueOnException(convertedObject);
+
+                        logger.log(Level.FINEST, "getConvertedValue delegation canceled", e);
+
+                        delegateToWrappedRenderer = false;
+
+                        if(e.isSkipOtherInterceptors())
+                        {
+                            break;
+                        }
+                    }
+
+                    logger.finest("beforeGetConvertedValue of " +
+                        rendererInterceptor.getClass().getName() + " finished");
                 }
-
-                logger.finest("beforeGetConvertedValue of " +
-                    rendererInterceptor.getClass().getName() + " finished");
             }
-        }
-        catch (SkipBeforeInterceptorsException e)
-        {
-            logger.log(Level.FINEST, "beforeGetConvertedValue interceptors canceled", e);
-        }
-
-        /*
-         * delegate
-         */
-        if(delegateToWrappedRenderer)
-        {
-            convertedObject = wrapped.getConvertedValue(facesContext, uiComponent, o);
-        }
-
-        try
-        {
-            for(RendererInterceptor rendererInterceptor : extValContext.getRendererInterceptors())
+            catch (SkipBeforeInterceptorsException e)
             {
-                logger.finest("start afterGetConvertedValue of " + rendererInterceptor.getClass().getName());
+                logger.log(Level.FINEST, "beforeGetConvertedValue interceptors canceled", e);
+            }
 
-                rendererInterceptor.afterGetConvertedValue(facesContext, uiComponent, o, this.wrapped);
+            /*
+             * delegate
+             */
+            if(delegateToWrappedRenderer)
+            {
+                convertedObject = wrapped.getConvertedValue(facesContext, uiComponent, o);
+            }
 
-                logger.finest("afterGetConvertedValue of " + rendererInterceptor.getClass().getName() + " finished");
+            try
+            {
+                for(RendererInterceptor rendererInterceptor : extValContext.getRendererInterceptors())
+                {
+                    logger.finest(
+                            "start afterGetConvertedValue of " + rendererInterceptor.getClass().getName());
+
+                    rendererInterceptor.afterGetConvertedValue(facesContext, uiComponent, o, this.wrapped);
+
+                    logger.finest(
+                            "afterGetConvertedValue of " + rendererInterceptor.getClass().getName() + " finished");
+                }
+            }
+            catch (SkipAfterInterceptorsException e)
+            {
+                logger.log(Level.FINEST, "afterGetConvertedValue interceptors canceled", e);
             }
         }
-        catch (SkipAfterInterceptorsException e)
+        finally
         {
-            logger.log(Level.FINEST, "afterGetConvertedValue interceptors canceled", e);
+            if(this.wrapped instanceof ExtValLazyRendererProxy)
+            {
+                ((ExtValLazyRendererProxy)this.wrapped).resetConvertedValueCache();
+            }
         }
 
         return convertedObject;