You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2010/05/28 04:22:03 UTC

svn commit: r949070 - in /myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component: UIComponentBase.java UIData.java UIViewRoot.java _ComponentAttributesMap.java

Author: lu4242
Date: Fri May 28 02:22:03 2010
New Revision: 949070

URL: http://svn.apache.org/viewvc?rev=949070&view=rev
Log:
MYFACES-2737 Cache FacesContext on UIComponentBase instances

Modified:
    myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIComponentBase.java
    myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIData.java
    myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIViewRoot.java
    myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java

Modified: myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIComponentBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIComponentBase.java?rev=949070&r1=949069&r2=949070&view=diff
==============================================================================
--- myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIComponentBase.java (original)
+++ myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIComponentBase.java Fri May 28 02:22:03 2010
@@ -86,6 +86,7 @@ public abstract class UIComponentBase
     private UIComponent _parent = null;
     private boolean _transient = false;
 
+    private transient FacesContext _facesContext;
 
     public UIComponentBase()
     {
@@ -739,6 +740,8 @@ public abstract class UIComponentBase
     {
         if (context == null) throw new NullPointerException("context");
         try {
+            setCachedFacesContext(context);
+            
             if (!isRendered()) return;
             Renderer renderer = getRenderer(context);
             if (renderer != null)
@@ -748,17 +751,37 @@ public abstract class UIComponentBase
         } catch (Exception e) {
             throw new FacesException("Exception while calling encodeBegin on : "+getPathToComponent(this), e);
         }
+        finally
+        {
+            setCachedFacesContext(null);
+        }
     }
 
     public void encodeChildren(FacesContext context)
             throws IOException
     {
         if (context == null) throw new NullPointerException("context");
-        if (!isRendered()) return;
-        Renderer renderer = getRenderer(context);
-        if (renderer != null)
+        
+        boolean isCachedFacesContext = isCachedFacesContext();
+        try
+        {
+            if (!isCachedFacesContext)
+            {
+                setCachedFacesContext(context);
+            }
+            if (!isRendered()) return;
+            Renderer renderer = getRenderer(context);
+            if (renderer != null)
+            {
+                renderer.encodeChildren(context, this);
+            }
+        }
+        finally
         {
-            renderer.encodeChildren(context, this);
+            if (!isCachedFacesContext)
+            {
+                setCachedFacesContext(null);
+            }
         }
     }
 
@@ -767,6 +790,7 @@ public abstract class UIComponentBase
     {
         if (context == null) throw new NullPointerException("context");
         try {
+            setCachedFacesContext(context);
             if (!isRendered()) return;
 
             Renderer renderer = getRenderer(context);
@@ -777,6 +801,10 @@ public abstract class UIComponentBase
         } catch (Exception e) {
             throw new FacesException("Exception while calling encodeEnd on : "+getPathToComponent(this), e);
         }
+        finally
+        {
+            setCachedFacesContext(null);
+        }
     }
 
     protected void addFacesListener(FacesListener listener)
@@ -836,21 +864,33 @@ public abstract class UIComponentBase
 
     public void processDecodes(FacesContext context)
     {
-        if (context == null) throw new NullPointerException("context");
-                if (!isRendered()) return;
-        for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
-        {
-            UIComponent childOrFacet = (UIComponent)it.next();
-            childOrFacet.processDecodes(context);
-        }
+        if (context == null)
+            throw new NullPointerException("context");
+        
         try
         {
-            decode(context);
+            setCachedFacesContext(context);
+            
+            if (!isRendered()) return;
+            
+            for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
+            {
+                UIComponent childOrFacet = (UIComponent)it.next();
+                childOrFacet.processDecodes(context);
+            }
+            try
+            {
+                decode(context);
+            }
+            catch (RuntimeException e)
+            {
+                context.renderResponse();
+                throw e;
+            }
         }
-        catch (RuntimeException e)
+        finally
         {
-            context.renderResponse();
-            throw e;
+            setCachedFacesContext(null);
         }
     }
 
@@ -858,12 +898,21 @@ public abstract class UIComponentBase
     public void processValidators(FacesContext context)
     {
         if (context == null) throw new NullPointerException("context");
-        if (!isRendered()) return;
-
-        for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
+        
+        try
         {
-            UIComponent childOrFacet = (UIComponent)it.next();
-            childOrFacet.processValidators(context);
+            setCachedFacesContext(context);
+            if (!isRendered()) return;
+    
+            for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
+            {
+                UIComponent childOrFacet = (UIComponent)it.next();
+                childOrFacet.processValidators(context);
+            }
+        }
+        finally
+        {
+            setCachedFacesContext(null);
         }
     }
 
@@ -879,12 +928,22 @@ public abstract class UIComponentBase
     public void processUpdates(FacesContext context)
     {
         if (context == null) throw new NullPointerException("context");
-        if (!isRendered()) return;
-
-        for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
+        
+        try
         {
-            UIComponent childOrFacet = (UIComponent)it.next();
-            childOrFacet.processUpdates(context);
+            setCachedFacesContext(context);
+            
+            if (!isRendered()) return;
+    
+            for (Iterator it = getFacetsAndChildren(); it.hasNext(); )
+            {
+                UIComponent childOrFacet = (UIComponent)it.next();
+                childOrFacet.processUpdates(context);
+            }
+        }
+        finally
+        {
+            setCachedFacesContext(null);
         }
     }
 
@@ -983,7 +1042,14 @@ public abstract class UIComponentBase
 
     protected FacesContext getFacesContext()
     {
-        return FacesContext.getCurrentInstance();
+        if (_facesContext == null)
+        {
+            return FacesContext.getCurrentInstance();
+        }
+        else
+        {
+            return _facesContext;
+        }
     }
 
     protected Renderer getRenderer(FacesContext context)
@@ -1314,6 +1380,16 @@ public abstract class UIComponentBase
         }
     }
 
+    boolean isCachedFacesContext()
+    {
+        return _facesContext != null;
+    }
+    
+    void setCachedFacesContext(FacesContext facesContext)
+    {
+        _facesContext = facesContext;
+    }
+
     //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
 
     private static final boolean DEFAULT_RENDERED = true;

Modified: myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIData.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIData.java?rev=949070&r1=949069&r2=949070&view=diff
==============================================================================
--- myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIData.java (original)
+++ myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIData.java Fri May 28 02:22:03 2010
@@ -605,21 +605,29 @@ public class UIData extends UIComponentB
     {
         if (context == null)
             throw new NullPointerException("context");
-        if (!isRendered())
-            return;
-        setRowIndex(-1);
-        processFacets(context, PROCESS_DECODES);
-        processColumnFacets(context, PROCESS_DECODES);
-        processColumnChildren(context, PROCESS_DECODES);
-        setRowIndex(-1);
         try
         {
-            decode(context);
+            setCachedFacesContext(context);
+            if (!isRendered())
+                return;
+            setRowIndex(-1);
+            processFacets(context, PROCESS_DECODES);
+            processColumnFacets(context, PROCESS_DECODES);
+            processColumnChildren(context, PROCESS_DECODES);
+            setRowIndex(-1);
+            try
+            {
+                decode(context);
+            }
+            catch (RuntimeException e)
+            {
+                context.renderResponse();
+                throw e;
+            }
         }
-        catch (RuntimeException e)
+        finally
         {
-            context.renderResponse();
-            throw e;
+            setCachedFacesContext(null);
         }
     }
 
@@ -627,18 +635,26 @@ public class UIData extends UIComponentB
     {
         if (context == null)
             throw new NullPointerException("context");
-        if (!isRendered())
-            return;
-        setRowIndex(-1);
-        processFacets(context, PROCESS_VALIDATORS);
-        processColumnFacets(context, PROCESS_VALIDATORS);
-        processColumnChildren(context, PROCESS_VALIDATORS);
-        setRowIndex(-1);
-
-        // check if an validation error forces the render response for our data
-        if (context.getRenderResponse())
+        try
         {
-            _isValidChilds = false;
+            setCachedFacesContext(context);
+            if (!isRendered())
+                return;
+            setRowIndex(-1);
+            processFacets(context, PROCESS_VALIDATORS);
+            processColumnFacets(context, PROCESS_VALIDATORS);
+            processColumnChildren(context, PROCESS_VALIDATORS);
+            setRowIndex(-1);
+    
+            // check if an validation error forces the render response for our data
+            if (context.getRenderResponse())
+            {
+                _isValidChilds = false;
+            }
+        }
+        finally
+        {
+            setCachedFacesContext(null);
         }
     }
 
@@ -646,17 +662,25 @@ public class UIData extends UIComponentB
     {
         if (context == null)
             throw new NullPointerException("context");
-        if (!isRendered())
-            return;
-        setRowIndex(-1);
-        processFacets(context, PROCESS_UPDATES);
-        processColumnFacets(context, PROCESS_UPDATES);
-        processColumnChildren(context, PROCESS_UPDATES);
-        setRowIndex(-1);
-
-        if (context.getRenderResponse())
+        try
+        {
+            setCachedFacesContext(context);
+            if (!isRendered())
+                return;
+            setRowIndex(-1);
+            processFacets(context, PROCESS_UPDATES);
+            processColumnFacets(context, PROCESS_UPDATES);
+            processColumnChildren(context, PROCESS_UPDATES);
+            setRowIndex(-1);
+        
+            if (context.getRenderResponse())
+            {
+                _isValidChilds = false;
+            }
+        }
+        finally
         {
-            _isValidChilds = false;
+            setCachedFacesContext(null);
         }
     }
 

Modified: myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIViewRoot.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIViewRoot.java?rev=949070&r1=949069&r2=949070&view=diff
==============================================================================
--- myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIViewRoot.java (original)
+++ myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/UIViewRoot.java Fri May 28 02:22:03 2010
@@ -207,7 +207,7 @@ public class UIViewRoot extends UICompon
      */
     public String createUniqueId()
     {
-        ExternalContext extCtx = FacesContext.getCurrentInstance().getExternalContext();
+        ExternalContext extCtx = getFacesContext().getExternalContext();
         return extCtx.encodeNamespace(UNIQUE_ID_PREFIX + _uniqueIdCounter++);
     }
 

Modified: myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java?rev=949070&r1=949069&r2=949070&view=diff
==============================================================================
--- myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java (original)
+++ myfaces/core/branches/1.1.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java Fri May 28 02:22:03 2010
@@ -246,7 +246,7 @@ class _ComponentAttributesMap
         ValueBinding vb = _component.getValueBinding((String) key);
         if (vb != null)
         {
-            return vb.getValue(FacesContext.getCurrentInstance());
+            return vb.getValue(_component.getFacesContext());
         }
 
         // no value found
@@ -395,7 +395,7 @@ class _ComponentAttributesMap
         }
         catch (Exception e)
         {
-            FacesContext facesContext = FacesContext.getCurrentInstance();
+            FacesContext facesContext = _component.getFacesContext();
             throw new FacesException("Could not get property " + propertyDescriptor.getName() + " of component " + _component.getClientId(facesContext), e);
         }
     }
@@ -422,7 +422,7 @@ class _ComponentAttributesMap
         }
         catch (Exception e)
         {
-            FacesContext facesContext = FacesContext.getCurrentInstance();
+            FacesContext facesContext = _component.getFacesContext();
             throw new FacesException("Could not set property " + propertyDescriptor.getName() +
                     " of component " + _component.getClientId(facesContext) +" to value : "+value+" with type : "+
                     (value==null?"null":value.getClass().getName()), e);