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 2013/09/18 19:25:54 UTC

svn commit: r1524497 - in /myfaces/core/trunk: api/src/main/java/javax/faces/component/ impl/src/main/java/org/apache/myfaces/application/ impl/src/main/java/org/apache/myfaces/context/servlet/ impl/src/main/java/org/apache/myfaces/view/facelets/impl/ ...

Author: lu4242
Date: Wed Sep 18 17:25:54 2013
New Revision: 1524497

URL: http://svn.apache.org/r1524497
Log:
MYFACES-3777 [perf] minimize FacesContext.getCurrentInstance() in some new points 

Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java
    myfaces/core/trunk/api/src/main/java/javax/faces/component/UIViewRoot.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFacelet.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentSupport.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentTagHandlerDelegate.java
    myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/view/ViewDeclarationLanguageBase.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java Wed Sep 18 17:25:54 2013
@@ -217,7 +217,8 @@ public abstract class UIComponentBase ex
                 // Application.publishEvent(java.lang.Class, java.lang.Object)  must be called, passing 
                 // PostAddToViewEvent.class as the first argument and the newly added component as the second 
                 // argument.
-                FacesContext facesContext = getFacesContext();
+                FacesContext facesContext = parent.isCachedFacesContext() ?
+                    parent.getFacesContext() : getFacesContext();
                 if (facesContext.isProcessingEvents())
                 {
                     _publishPostAddToViewEvent(facesContext, this);

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/UIViewRoot.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/UIViewRoot.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/UIViewRoot.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/UIViewRoot.java Wed Sep 18 17:25:54 2013
@@ -225,7 +225,22 @@ public class UIViewRoot extends UICompon
                     UIComponent component = it.next();
                     if(componentId.equals(component.getId()) && componentResource != component)
                     {
-                        it.remove();
+                        if (!component.isCachedFacesContext())
+                        {
+                            try
+                            {
+                                component.setCachedFacesContext(context);
+                                it.remove();
+                            }
+                            finally
+                            {
+                                component.setCachedFacesContext(null);
+                            }
+                        }
+                        else
+                        {
+                            it.remove();
+                        }
                     }
                     else if (componentResource == component)
                     {
@@ -241,7 +256,22 @@ public class UIViewRoot extends UICompon
                 UIComponent component = it.next();
                 if(componentId.equals(component.getId()) && componentResource != component)
                 {
-                    it.remove();
+                    if (!component.isCachedFacesContext())
+                    {
+                        try
+                        {
+                            component.setCachedFacesContext(context);
+                            it.remove();
+                        }
+                        finally
+                        {
+                            component.setCachedFacesContext(null);
+                        }
+                    }
+                    else
+                    {
+                        it.remove();
+                    }
                 }
                 else if (componentResource == component)
                 {
@@ -253,7 +283,22 @@ public class UIViewRoot extends UICompon
         // Add the component resource to the list
         if (!alreadyAdded)
         {
-            componentResources.add(componentResource);
+            if (!componentResource.isCachedFacesContext())
+            {
+                try
+                {
+                    componentResource.setCachedFacesContext(context);
+                    componentResources.add(componentResource);
+                }
+                finally
+                {
+                    componentResource.setCachedFacesContext(context);
+                }
+            }
+            else
+            {
+                componentResources.add(componentResource);
+            }
         }
     }
 
@@ -622,7 +667,8 @@ public class UIViewRoot extends UICompon
         {
             // create the facet by calling context.getApplication().createComponent()
             // using javax.faces.Panel as the argument
-            facet = context.getApplication().createComponent("javax.faces.ComponentResourceContainer");
+            facet = context.getApplication().createComponent(context,
+                "javax.faces.ComponentResourceContainer", null);
 
             // Set the id of the facet to be target
             if (target.equals("head"))

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/NavigationHandlerImpl.java Wed Sep 18 17:25:54 2013
@@ -1554,7 +1554,7 @@ public class NavigationHandlerImpl
             else
             {
                 FlowHandler flowHandler = facesContext.getApplication().getFlowHandler();
-                return flowHandler.getCurrentFlow();
+                return flowHandler.getCurrentFlow(facesContext);
             }
         }
         

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImpl.java Wed Sep 18 17:25:54 2013
@@ -297,7 +297,7 @@ public class FacesContextImpl extends Fa
                     FactoryFinder.getFactory(FactoryFinder.PARTIAL_VIEW_CONTEXT_FACTORY);
             }
             // Put actual facesContext as param, not this - this can be wrapped
-            _partialViewContext = _partialViewContextFactory.getPartialViewContext(FacesContext.getCurrentInstance());
+            _partialViewContext = _partialViewContextFactory.getPartialViewContext(getCurrentFacesContext());
         }
         return _partialViewContext;
     }
@@ -380,7 +380,7 @@ public class FacesContextImpl extends Fa
         assertNotReleased();
 
         RenderKit renderKit = getRenderKit();
-        FacesContext facesContext = FacesContext.getCurrentInstance();
+        FacesContext facesContext = getCurrentFacesContext();
         if (renderKit == null)
         {
             // NullPointerException with StateManager, because

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/FacesContextImplBase.java Wed Sep 18 17:25:54 2013
@@ -71,6 +71,7 @@ public abstract class FacesContextImplBa
     
     private List<String> _resourceLibraryContracts;
     private Character _separatorChar;
+    private FacesContext _currentFacesContext;
 
     /**
      * Base constructor.
@@ -111,7 +112,7 @@ public abstract class FacesContextImplBa
     public void release()
     {
         _applicationFactory = null;
-
+        _currentFacesContext = null;
         if (_defaultExternalContext != null)
         {
             _defaultExternalContext.release();
@@ -215,7 +216,7 @@ public abstract class FacesContextImplBa
             return _elContext;
         }
 
-        _elContext = new FacesELContext(getApplication().getELResolver(), FacesContext.getCurrentInstance());
+        _elContext = new FacesELContext(getApplication().getELResolver(), getCurrentFacesContext());
 
         ELContextEvent event = new ELContextEvent(_elContext);
         for (ELContextListener listener : getApplication().getELContextListeners())
@@ -311,7 +312,7 @@ public abstract class FacesContextImplBa
             {
                 _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
             }
-            _cachedRenderKit = _renderKitFactory.getRenderKit(FacesContext.getCurrentInstance(), renderKitId);
+            _cachedRenderKit = _renderKitFactory.getRenderKit(getCurrentFacesContext(), renderKitId);
         }
         
         return _cachedRenderKit;
@@ -372,4 +373,12 @@ public abstract class FacesContextImplBa
         }
     }
     
+    protected FacesContext getCurrentFacesContext()
+    {
+        if (_currentFacesContext == null)
+        {
+            _currentFacesContext = FacesContext.getCurrentInstance();
+        }
+        return _currentFacesContext;
+    }
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/context/servlet/ServletExternalContextImpl.java Wed Sep 18 17:25:54 2013
@@ -86,6 +86,7 @@ public final class ServletExternalContex
     private String _requestPathInfo;
     private FlashFactory _flashFactory;
     private Flash _flash;
+    private FacesContext _currentFacesContext;
 
     public ServletExternalContextImpl(final ServletContext servletContext, 
             final ServletRequest servletRequest,
@@ -128,6 +129,7 @@ public final class ServletExternalContex
     {
         super.release(); // releases fields on ServletExternalContextImplBase
         
+        _currentFacesContext = null;
         _servletRequest = null;
         _servletResponse = null;
         _sessionMap = null;
@@ -352,7 +354,7 @@ public final class ServletExternalContex
     
     private String encodeWindowId(String encodedUrl)
     {
-        FacesContext facesContext = FacesContext.getCurrentInstance();
+        FacesContext facesContext = getCurrentFacesContext();
         ClientWindow window = facesContext.getExternalContext().getClientWindow();
         if (window != null)
         {
@@ -507,7 +509,7 @@ public final class ServletExternalContex
     @Override
     public void redirect(final String url) throws IOException
     {
-        FacesContext facesContext = FacesContext.getCurrentInstance();
+        FacesContext facesContext = getCurrentFacesContext();
         PartialViewContext partialViewContext = facesContext.getPartialViewContext(); 
         if (partialViewContext.isPartialRequest())
         {
@@ -846,7 +848,7 @@ public final class ServletExternalContex
             }
         }
         
-        FacesContext facesContext = FacesContext.getCurrentInstance();
+        FacesContext facesContext = getCurrentFacesContext();
         ClientWindow window = facesContext.getExternalContext().getClientWindow();
         if (window != null)
         {
@@ -965,4 +967,13 @@ public final class ServletExternalContex
         HttpSession session = _httpServletRequest.getSession();
         session.setMaxInactiveInterval(interval);
     }
+    
+    protected FacesContext getCurrentFacesContext()
+    {
+        if (_currentFacesContext == null)
+        {
+            _currentFacesContext = FacesContext.getCurrentInstance();
+        }
+        return _currentFacesContext;
+    }
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFacelet.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFacelet.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFacelet.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/impl/DefaultFacelet.java Wed Sep 18 17:25:54 2013
@@ -54,6 +54,7 @@ import org.apache.myfaces.view.facelets.
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 import org.apache.myfaces.view.facelets.FaceletFactory;
 import org.apache.myfaces.view.facelets.compiler.EncodingHandler;
+import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
 
 
 /**
@@ -156,6 +157,10 @@ final class DefaultFacelet extends Abstr
                     myFaceletContext.setUniqueIdsIterator(uniqueIdList.iterator());
                 }
             }
+            if (parent instanceof UIViewRoot)
+            {
+                ComponentSupport.setCachedFacesContext((UIViewRoot)parent, facesContext);
+            }
         }
         DefaultFaceletContext ctx = new DefaultFaceletContext(facesContext, this, myFaceletContext);
         
@@ -212,6 +217,10 @@ final class DefaultFacelet extends Abstr
             
             if (faceletCompositionContextInitialized)
             {
+                if (parent instanceof UIViewRoot)
+                {
+                    ComponentSupport.setCachedFacesContext((UIViewRoot)parent, null);
+                }
                 myFaceletContext.release(facesContext);
                 List<String> uniqueIdList = ((EncodingHandler)_root).getUniqueIdList();
                 if (recordUniqueIds &&  uniqueIdList == null)

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentSupport.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentSupport.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentSupport.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentSupport.java Wed Sep 18 17:25:54 2013
@@ -19,6 +19,8 @@
 package org.apache.myfaces.view.facelets.tag.jsf;
 
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Locale;
@@ -49,6 +51,30 @@ import org.apache.myfaces.view.facelets.
  */
 public final class ComponentSupport
 {
+    private static final Method SET_CACHED_FACES_CONTEXT;
+        
+    static
+    {
+        Method method = null;
+        try
+        {
+            // The trick here is define this method in UIComponent as a class scoped method
+            // and get it from there. Even if the real implementation is in UIComponentBase,
+            // the jvm will always call the right one and in case of a instance not extending
+            // from UIComponentBase it will call a dummy method.
+            method = UIComponent.class.getDeclaredMethod("setCachedFacesContext", FacesContext.class);
+            method.setAccessible(true);
+        }
+        catch (NoSuchMethodException ex)
+        {
+            method = null;
+        }
+        catch (SecurityException ex)
+        {
+            method = null;
+        }
+        SET_CACHED_FACES_CONTEXT = method;
+    }
 
     private final static String MARK_DELETED = "oam.vf.MARK_DELETED";
     public final static String MARK_CREATED = "oam.vf.MARK_ID";
@@ -706,5 +732,25 @@ public final class ComponentSupport
             map.putState(uniqueId, value);
         }
     }
-
+    
+    public static void setCachedFacesContext(UIComponent component,
+        FacesContext context)
+    {
+        if (SET_CACHED_FACES_CONTEXT != null)
+        {
+            try
+            {
+                SET_CACHED_FACES_CONTEXT.invoke(component, context);
+            }
+            catch (IllegalAccessException ex)
+            {
+            }
+            catch (IllegalArgumentException ex)
+            {
+            }
+            catch (InvocationTargetException ex)
+            {
+            }
+        }
+    }
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentTagHandlerDelegate.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentTagHandlerDelegate.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentTagHandlerDelegate.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/jsf/ComponentTagHandlerDelegate.java Wed Sep 18 17:25:54 2013
@@ -333,6 +333,7 @@ public class ComponentTagHandlerDelegate
                         // Replace parent with the relocated parent.
                         parent = c.getParent();
                     }
+                    ComponentSupport.setCachedFacesContext(c, facesContext);
                 }
                 if (facetName == null)
                 {
@@ -344,6 +345,7 @@ public class ComponentTagHandlerDelegate
                 }
                 if (mctx.isRefreshingSection())
                 {
+                    ComponentSupport.setCachedFacesContext(c, null);
                     facesContext.setProcessingEvents(oldProcessingEvents);
                 }
             //}
@@ -377,6 +379,7 @@ public class ComponentTagHandlerDelegate
         if (componentFound && mctx.isRefreshingSection())
         {
             facesContext.setProcessingEvents(false);
+            ComponentSupport.setCachedFacesContext(c, facesContext);
         }
         if (facetName == null)
         {
@@ -388,6 +391,7 @@ public class ComponentTagHandlerDelegate
         }
         if (componentFound && mctx.isRefreshingSection())
         {
+            ComponentSupport.setCachedFacesContext(c, null);
             facesContext.setProcessingEvents(oldProcessingEvents);
         }
 

Modified: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/view/ViewDeclarationLanguageBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/view/ViewDeclarationLanguageBase.java?rev=1524497&r1=1524496&r2=1524497&view=diff
==============================================================================
--- myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/view/ViewDeclarationLanguageBase.java (original)
+++ myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/view/ViewDeclarationLanguageBase.java Wed Sep 18 17:25:54 2013
@@ -53,7 +53,8 @@ public abstract class ViewDeclarationLan
             Application application = context.getApplication();
 
             // Create a new UIViewRoot object instance using Application.createComponent(UIViewRoot.COMPONENT_TYPE).
-            UIViewRoot newViewRoot = (UIViewRoot) application.createComponent(UIViewRoot.COMPONENT_TYPE);
+            UIViewRoot newViewRoot = (UIViewRoot) application.createComponent(
+                context, UIViewRoot.COMPONENT_TYPE, null);
             UIViewRoot oldViewRoot = context.getViewRoot();
             if (oldViewRoot == null)
             {