You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2017/09/17 12:30:47 UTC

[myfaces-trinidad] 24/36: TRINIDAD-1669 Improve transient memory consumption of UIXComponentBase.getClientId()

This is an automated email from the ASF dual-hosted git repository.

deki pushed a commit to branch 1.2.12.2-branch
in repository https://gitbox.apache.org/repos/asf/myfaces-trinidad.git

commit e9a5acc1103a79ce44dfd5f4b0741cb8eb63cca6
Author: Blake Sullivan <bs...@apache.org>
AuthorDate: Fri Feb 26 01:26:43 2010 +0000

    TRINIDAD-1669 Improve transient memory consumption of UIXComponentBase.getClientId()
    
    Patch to test out client id caching using the flag org.apache.myfaces.trinidadinternal.ENABLE_CLIENT_ID_CACHING to enable the behavior
---
 .../trinidad/component/UIXComponentBase.java       | 71 ++++++++++++++++++----
 .../myfaces/trinidad/render/RenderUtilsTest.java   | 40 ++++++------
 2 files changed, 80 insertions(+), 31 deletions(-)

diff --git a/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java
index 672fda8..bf57526 100644
--- a/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java
+++ b/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java
@@ -30,6 +30,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import java.util.concurrent.atomic.AtomicReference;
+
 import javax.el.ELContext;
 import javax.el.ELException;
 import javax.el.MethodExpression;
@@ -349,25 +353,34 @@ abstract public class UIXComponentBase extends UIXComponent
   @Override
   public String getClientId(FacesContext context)
   {
-    return _calculateClientId(context);
-/* TODO put back in when we fix all of the clientID caching issues
-    String clientId = _clientId;
-    
-    if (clientId == null)
+    if (_isClientIdCachingEnabled(context))
     {
-      clientId = _calculateClientId(context);
+      String clientId = _clientId;
       
-      if (_usesFacesBeanImpl)
-        _clientId = clientId;
+      if (clientId == null)
+      {
+        clientId = _calculateClientId(context);
+        
+        if (_usesFacesBeanImpl)
+          _clientId = clientId;
+      }
+      else
+      {
+        // for now validate success by checking the cached result against the dynamically
+        // generated result
+        String realID = _calculateClientId(context);
+        
+        if (!clientId.equals(realID))
+          throw new IllegalStateException(
+        "cached client id " + clientId + " for " + this + " doesn't match client id:" + realID);
+      }
+    
+      return clientId;
     }
     else
     {
-      assert clientId.equals(_calculateClientId(context)) :
-      "cached client id " + _clientId + " for " + this + " doesn't match client id:" + _calculateClientId(context);
+      return _calculateClientId(context);
     }
-    
-    return clientId;
-*/
   }
 
 
@@ -1768,6 +1781,38 @@ abstract public class UIXComponentBase extends UIXComponent
   {
   }
 
+  private static boolean _isClientIdCachingEnabled(FacesContext context)
+  {
+    if (context == null)
+      throw new IllegalArgumentException("FacesContext is null");
+
+    Boolean cacheClientIds = _sClientIdCachingEnabled.get();
+    
+    if (cacheClientIds == null)
+    {
+      // see if client  is enabled for the application (the default is on)
+      boolean cachingEnabled = !Boolean.TRUE.equals(
+                          context.getExternalContext().
+                          getApplicationMap().get(_INIT_PROP_CLIENT_ID_CACHING_ENABLED));
+      
+      // cache the servlet initialization value
+      _sClientIdCachingEnabled.set(cachingEnabled ? Boolean.TRUE : Boolean.FALSE);
+
+      return cachingEnabled;
+    }
+    else
+    {
+      return cacheClientIds.booleanValue();
+    }
+  }
+  
+  private static AtomicReference<Boolean> _sClientIdCachingEnabled = 
+                                                                 new AtomicReference<Boolean>(null);
+  
+  // temporary servlet initialization flag controlling whether client ID caching is enabled
+  private static final String _INIT_PROP_CLIENT_ID_CACHING_ENABLED = 
+                                      "org.apache.myfaces.trinidadinternal.ENABLE_CLIENT_ID_CACHING";
+
   static private final LifecycleRenderer _UNDEFINED_LIFECYCLE_RENDERER =
                                                 new ExtendedRendererImpl();
   static private final Renderer _UNDEFINED_RENDERER = new RendererImpl();
diff --git a/trinidad-api/src/test/java/org/apache/myfaces/trinidad/render/RenderUtilsTest.java b/trinidad-api/src/test/java/org/apache/myfaces/trinidad/render/RenderUtilsTest.java
index 5c99990..cec224d 100644
--- a/trinidad-api/src/test/java/org/apache/myfaces/trinidad/render/RenderUtilsTest.java
+++ b/trinidad-api/src/test/java/org/apache/myfaces/trinidad/render/RenderUtilsTest.java
@@ -31,8 +31,9 @@ import org.apache.myfaces.trinidad.component.UIXForm;
 import org.apache.myfaces.trinidad.component.UIXInput;
 import org.apache.myfaces.trinidad.component.UIXPanel;
 
+import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
 
-public class RenderUtilsTest extends TestCase
+public class RenderUtilsTest extends FacesTestCase
 {
   public static final Test suite()
   {
@@ -89,6 +90,8 @@ public class RenderUtilsTest extends TestCase
   @SuppressWarnings("unchecked")
   public void testButtonAndNamingContainerSiblings()
   {
+    FacesContext context = FacesContext.getCurrentInstance();
+
       // rootPanel
       //     button1
       //     table1
@@ -105,41 +108,41 @@ public class RenderUtilsTest extends TestCase
       table1.getChildren().add(tableChild);
     
     String relativeId =
-      RenderUtils.getRelativeId(null, button1, "table1");
+      RenderUtils.getRelativeId(context, button1, "table1");
     assertEquals("table1", relativeId);
     
     relativeId =
-      RenderUtils.getRelativeId(null, button1, ":table1");
+      RenderUtils.getRelativeId(context, button1, ":table1");
     assertEquals("table1", relativeId);
     
     // new way would find nothing, so we'd have to get something logical
     relativeId =
-      RenderUtils.getRelativeId(null, table1, "someRandomId");
+      RenderUtils.getRelativeId(context, table1, "someRandomId");
     assertEquals("table1_Client:someRandomId", relativeId);
     
     relativeId =
-      RenderUtils.getRelativeId(null, table1, ":commandButton1");
+      RenderUtils.getRelativeId(context, table1, ":commandButton1");
     assertEquals("commandButton1", relativeId);
 
     // to get to the commandButton from the table, you need to pop out of the
     // table
     relativeId =
-      RenderUtils.getRelativeId(null, table1, "::commandButton1");
+      RenderUtils.getRelativeId(context, table1, "::commandButton1");
     assertEquals("commandButton1", relativeId);
     
     // backward compatibility test -- this was the old syntax for siblings to the table.
     // this should be found by looking at the nc's parent from findRelativeComponent
     relativeId =
-      RenderUtils.getRelativeId(null, table1, "commandButton1");
+      RenderUtils.getRelativeId(context, table1, "commandButton1");
     assertEquals("commandButton1", relativeId);
        
     // backward compatibility test -- this was the old syntax for children to the table.
     relativeId =
-      RenderUtils.getRelativeId(null, table1, "table1:tableChildId");
+      RenderUtils.getRelativeId(context, table1, "table1:tableChildId");
     assertEquals("table1:tableChildId", relativeId);
      // this is the new syntax for children to the table
     relativeId =
-      RenderUtils.getRelativeId(null, table1, "tableChildId");
+      RenderUtils.getRelativeId(context, table1, "tableChildId");
     assertEquals("table1:tableChildId", relativeId);
 
   }
@@ -150,6 +153,7 @@ public class RenderUtilsTest extends TestCase
   @SuppressWarnings("unchecked")
   public void testRelativeSearch()
   {
+    FacesContext context = FacesContext.getCurrentInstance();
 
 
       // set up component hierarchy
@@ -195,13 +199,13 @@ public class RenderUtilsTest extends TestCase
      */
       
     String relativeId =
-      RenderUtils.getRelativeId(null, input1, "::button1");
+      RenderUtils.getRelativeId(context, input1, "::button1");
     // new way should pop OUT of ONE naming container and will find it
     assertEquals("ncRoot:button1", relativeId);
 
     
     relativeId =
-      RenderUtils.getRelativeId(null, input1, ":::button1");
+      RenderUtils.getRelativeId(context, input1, ":::button1");
     // new way should pop OUT of TWO naming containers and will find not find it
     // since it is in ncRoot and the base is now the view root.
     // so it goes to the old findRelativeComponent, and this will find it.
@@ -209,27 +213,27 @@ public class RenderUtilsTest extends TestCase
 
 
     relativeId =
-      RenderUtils.getRelativeId(null, input1, "randomPeer");
+      RenderUtils.getRelativeId(context, input1, "randomPeer");
     // randomPeer doesn't exist, so new way won't find it.
     // uses code that doesn't need to find the component to return this:
     assertEquals("nc1_Client:randomPeer", relativeId);
     
     relativeId =
-      RenderUtils.getRelativeId(null, input1, "::randomPeer");
+      RenderUtils.getRelativeId(context, input1, "::randomPeer");
     // randomPeer doesn't exist, so new way won't find it.
     // uses code that doesn't need to find the component to return this:
     assertEquals("ncRoot_Client:randomPeer", relativeId);
  
     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
     relativeId =
-      RenderUtils.getRelativeId(null, input1, ":::rootButton");
+      RenderUtils.getRelativeId(context, input1, ":::rootButton");
     // new way should pop OUT of both NC with ::: and will find it
     assertEquals("rootButton", relativeId);
 
  
     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
     relativeId =
-      RenderUtils.getRelativeId(null, input1, "::rootButton");
+      RenderUtils.getRelativeId(context, input1, "::rootButton");
     // new way should pop OUT of one NC with ::, so it can't find it
     // the 'old' findRelativeComponent can't find it either.
     // so it returns what the old getRelativeId would have returned
@@ -240,19 +244,19 @@ public class RenderUtilsTest extends TestCase
     
     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
     relativeId =
-      RenderUtils.getRelativeId(null, input1, "::::rootButton");
+      RenderUtils.getRelativeId(context, input1, "::::rootButton");
     // new way should pop OUT of ALL NCs and will find it.
     assertEquals("rootButton", relativeId);
     
 
     
     relativeId =
-      RenderUtils.getRelativeId(null, input1, "::::button1");
+      RenderUtils.getRelativeId(context, input1, "::::button1");
     // new way should return this
     assertEquals("button1", relativeId);
     
     relativeId =
-      RenderUtils.getRelativeId(null, input1, ":::::button1");
+      RenderUtils.getRelativeId(context, input1, ":::::button1");
     assertEquals("button1", relativeId);
   }
 

-- 
To stop receiving notification emails like this one, please contact
"commits@myfaces.apache.org" <co...@myfaces.apache.org>.