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 2012/10/27 19:08:51 UTC

svn commit: r1402824 - in /myfaces/core/branches/2.0.x: api/src/main/java/javax/faces/component/ impl/src/main/java/org/apache/myfaces/el/unified/resolver/ impl/src/main/java/org/apache/myfaces/view/facelets/el/ impl/src/main/java/org/apache/myfaces/vi...

Author: lu4242
Date: Sat Oct 27 17:08:51 2012
New Revision: 1402824

URL: http://svn.apache.org/viewvc?rev=1402824&view=rev
Log:
MYFACES-3635 [perf] Improvements over #{cc} and #{cc.attrs} evaluation 

Modified:
    myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/el/unified/resolver/CompositeComponentELResolver.java
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java
    myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentBeanInfo.java

Modified: myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java?rev=1402824&r1=1402823&r2=1402824&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java (original)
+++ myfaces/core/branches/2.0.x/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java Sat Oct 27 17:08:51 2012
@@ -69,6 +69,8 @@ class _ComponentAttributesMap implements
     
     private final static String COMPONENT_ADDED_BY_HANDLER_MARKER = "oam.vf.addedByHandler";
     
+    public static final String PROPERTY_DESCRIPTOR_MAP_KEY = "oam.cc.beanInfo.PDM";
+    
     /**
      * This variable works as a check to indicate the minimun lenght we need to check
      * for the special attributes, and save some time in get(), containsKey() and 
@@ -96,6 +98,8 @@ class _ComponentAttributesMap implements
     
     private boolean _isCompositeComponent;
     private boolean _isCompositeComponentSet;
+    
+    private BeanInfo _ccBeanInfo;
 
     /**
      * Create a map backed by the specified component.
@@ -338,17 +342,25 @@ class _ComponentAttributesMap implements
                     }
                     if (_isCompositeComponent)
                     {
-                        BeanInfo ccBeanInfo = (BeanInfo) getUnderlyingMap().get(UIComponent.BEANINFO_KEY);
+                        BeanInfo ccBeanInfo = _ccBeanInfo != null ? _ccBeanInfo :
+                            (BeanInfo) getUnderlyingMap().get(UIComponent.BEANINFO_KEY);
                         if (ccBeanInfo != null)
                         {
-                            for (PropertyDescriptor attribute : ccBeanInfo.getPropertyDescriptors())
+                            //Fast shortcut to allow fast lookup.
+                            Map<String, PropertyDescriptor> attributeMap = (Map<String, PropertyDescriptor>) 
+                                ccBeanInfo.getBeanDescriptor().getValue(
+                                    PROPERTY_DESCRIPTOR_MAP_KEY);
+                            if (attributeMap != null)
                             {
-                                if (attribute.getName().equals(key))
+                                PropertyDescriptor attribute = attributeMap.get(key);
+                                if (attribute != null)
                                 {
                                     String attributeName = attribute.getName();
-                                    boolean isKnownMethod = "action".equals(attributeName) || "actionListener".equals(attributeName)  
-                                            || "validator".equals(attributeName) || "valueChangeListener".equals(attributeName);
-                                    
+                                    boolean isKnownMethod = "action".equals(attributeName)
+                                            || "actionListener".equals(attributeName)
+                                            || "validator".equals(attributeName)
+                                            || "valueChangeListener".equals(attributeName);
+
                                     // <composite:attribute> method-signature attribute is 
                                     // ValueExpression that must evaluate to String
                                     ValueExpression methodSignatureExpression
@@ -357,11 +369,14 @@ class _ComponentAttributesMap implements
                                     if (methodSignatureExpression != null)
                                     {
                                         // Check if the value expression holds a method signature
-                                        // Note that it could be null, so in that case we don't have to do anything
-                                        methodSignature = (String) methodSignatureExpression.getValue(_component.getFacesContext().getELContext());
+                                        // Note that it could be null, so in that case we don't have to 
+                                        // do anything
+                                        methodSignature = (String) methodSignatureExpression.getValue(
+                                                                    _component.getFacesContext().getELContext());
                                     }
-                                    
-                                    // either the attributeName has to be a knownMethod or there has to be a method-signature
+
+                                    // either the attributeName has to be a knownMethod
+                                    // or there has to be a method-signature
                                     if (isKnownMethod || methodSignature != null)
                                     {
                                         //In this case it is expecting a ValueExpression
@@ -370,7 +385,48 @@ class _ComponentAttributesMap implements
                                     else
                                     {
                                         value = attribute.getValue("default");
-                                        break;
+                                    }
+                                }
+                            }
+                            else
+                            {
+                                // Failsafe if another implementation for composite components is set
+                                for (PropertyDescriptor attribute : ccBeanInfo.getPropertyDescriptors())
+                                {
+                                    if (attribute.getName().equals(key))
+                                    {
+                                        String attributeName = attribute.getName();
+                                        boolean isKnownMethod = "action".equals(attributeName)
+                                                || "actionListener".equals(attributeName)
+                                                || "validator".equals(attributeName)
+                                                || "valueChangeListener".equals(attributeName);
+
+                                        // <composite:attribute> method-signature attribute is 
+                                        // ValueExpression that must evaluate to String
+                                        ValueExpression methodSignatureExpression
+                                                = (ValueExpression) attribute.getValue("method-signature");
+                                        String methodSignature = null;
+                                        if (methodSignatureExpression != null)
+                                        {
+                                            // Check if the value expression holds a method signature
+                                            // Note that it could be null, so in that case we don't have to 
+                                            // do anything
+                                            methodSignature = (String) methodSignatureExpression.getValue(
+                                                                        _component.getFacesContext().getELContext());
+                                        }
+
+                                        // either the attributeName has to be a knownMethod
+                                        // or there has to be a method-signature
+                                        if (isKnownMethod || methodSignature != null)
+                                        {
+                                            //In this case it is expecting a ValueExpression
+                                            return attribute.getValue("default");
+                                        }
+                                        else
+                                        {
+                                            value = attribute.getValue("default");
+                                            break;
+                                        }
                                     }
                                 }
                             }
@@ -435,6 +491,11 @@ class _ComponentAttributesMap implements
                 _component.setOamVfFacetCreatedUIPanel(false);
                 return oldValue;
             }
+            else if (UIComponent.BEANINFO_KEY.length() == keyLength 
+                && UIComponent.BEANINFO_KEY.equals(key))
+            {
+                _ccBeanInfo = null;
+            }
         }
         _PropertyDescriptorHolder propertyDescriptor = getPropertyDescriptor((String) key);
         if (propertyDescriptor != null)
@@ -537,6 +598,11 @@ class _ComponentAttributesMap implements
             _isCompositeComponent = true;
             _isCompositeComponentSet = true;
         }
+        if (UIComponent.BEANINFO_KEY.length() == keyLength 
+            && UIComponent.BEANINFO_KEY.equals(key))
+        {
+            _ccBeanInfo = (BeanInfo) value;
+        }
         return _component.getStateHelper().put(UIComponentBase.PropertyKeys.attributesMap, key, value);
     }
 

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/el/unified/resolver/CompositeComponentELResolver.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/el/unified/resolver/CompositeComponentELResolver.java?rev=1402824&r1=1402823&r2=1402824&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/el/unified/resolver/CompositeComponentELResolver.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/el/unified/resolver/CompositeComponentELResolver.java Sat Oct 27 17:08:51 2012
@@ -37,6 +37,7 @@ import javax.faces.el.CompositeComponent
 
 import org.apache.myfaces.shared.config.MyfacesConfig;
 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
+import org.apache.myfaces.view.facelets.tag.composite.CompositeComponentBeanInfo;
 
 /**
  * Composite component attribute EL resolver.  See JSF spec, section 5.6.2.2.
@@ -275,6 +276,7 @@ public final class CompositeComponentELR
         private final BeanInfo _beanInfo;
         private final Map<String, Object> _originalMap;
         private final PropertyDescriptor [] _propertyDescriptors;
+        private final CompositeComponentBeanInfo _ccBeanInfo;
 
         private CompositeComponentAttributesMapWrapper(UIComponent component)
         {
@@ -282,6 +284,8 @@ public final class CompositeComponentELR
             this._originalMap = component.getAttributes();
             this._beanInfo = (BeanInfo) _originalMap.get(UIComponent.BEANINFO_KEY);
             this._propertyDescriptors = _beanInfo.getPropertyDescriptors();
+            this._ccBeanInfo = (this._beanInfo instanceof CompositeComponentBeanInfo) ?
+                (CompositeComponentBeanInfo) this._beanInfo : null;
         }
 
         public ValueExpression getExpression(String name)
@@ -327,12 +331,23 @@ public final class CompositeComponentELR
             }
             else
             {
-                for (PropertyDescriptor attribute : _propertyDescriptors)
+                if (_ccBeanInfo == null)
                 {
-                    if (attribute.getName().equals(key))
+                    for (PropertyDescriptor attribute : _propertyDescriptors)
+                    {
+                        if (attribute.getName().equals(key))
+                        {
+                            obj = attribute.getValue("default");
+                            break;
+                        }
+                    }
+                }
+                else
+                {
+                    PropertyDescriptor attribute = _ccBeanInfo.getPropertyDescriptorsMap().get(key);
+                    if (attribute != null)
                     {
                         obj = attribute.getValue("default");
-                        break;
                     }
                 }
                 // We have to check for a ValueExpression and also evaluate it

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java?rev=1402824&r1=1402823&r2=1402824&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java Sat Oct 27 17:08:51 2012
@@ -106,14 +106,6 @@ public final class CompositeComponentELU
             final Location location)
     {
         //1 Use getCurrentComponent and getCurrentCompositeComponent to look on the component stack
-        UIComponent currentComponent = UIComponent.getCurrentComponent(facesContext);
-        
-        if (currentComponent == null)
-        {
-            // Cannot found any component, because we don't have any reference!
-            return null;
-        }
-        
         UIComponent currentCompositeComponent = UIComponent.getCurrentCompositeComponent(facesContext);
         
         //1.1 Use getCurrentCompositeComponent first!
@@ -126,7 +118,15 @@ public final class CompositeComponentELU
                 return currentCompositeComponent;
             }
         }
+
+        UIComponent currentComponent = UIComponent.getCurrentComponent(facesContext);
         
+        if (currentComponent == null)
+        {
+            // Cannot found any component, because we don't have any reference!
+            return null;
+        }
+
         //2. Look on the stack using a recursive algorithm.
         UIComponent matchingCompositeComponent
                 = lookForCompositeComponentOnStack(facesContext, location, currentComponent);
@@ -265,14 +265,6 @@ public final class CompositeComponentELU
             final Location location, int ccLevel)
     {
         //1 Use getCurrentComponent and getCurrentCompositeComponent to look on the component stack
-        UIComponent currentComponent = UIComponent.getCurrentComponent(facesContext);
-        
-        if (currentComponent == null)
-        {
-            // Cannot found any component, because we don't have any reference!
-            return null;
-        }
-        
         UIComponent currentCompositeComponent = UIComponent.getCurrentCompositeComponent(facesContext);
         
         //1.1 Use getCurrentCompositeComponent first!
@@ -286,6 +278,14 @@ public final class CompositeComponentELU
                 return currentCompositeComponent;
             }
         }
+
+        UIComponent currentComponent = UIComponent.getCurrentComponent(facesContext);
+        
+        if (currentComponent == null)
+        {
+            // Cannot found any component, because we don't have any reference!
+            return null;
+        }
         
         //2. Look on the stack using a recursive algorithm.
         UIComponent matchingCompositeComponent

Modified: myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentBeanInfo.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentBeanInfo.java?rev=1402824&r1=1402823&r2=1402824&view=diff
==============================================================================
--- myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentBeanInfo.java (original)
+++ myfaces/core/branches/2.0.x/impl/src/main/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentBeanInfo.java Sat Oct 27 17:08:51 2012
@@ -26,10 +26,13 @@ import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.faces.view.AttachedObjectTarget;
 
@@ -72,6 +75,8 @@ import javax.faces.view.AttachedObjectTa
 public class CompositeComponentBeanInfo extends SimpleBeanInfo 
     implements Externalizable
 {
+    
+    public static final String PROPERTY_DESCRIPTOR_MAP_KEY = "oam.cc.beanInfo.PDM";
 
     /**
      * Most of the information here are filled on composite:interface tag.
@@ -95,6 +100,8 @@ public class CompositeComponentBeanInfo 
     
     private PropertyDescriptor[] _propertyDescriptorsArray;
     
+    private Map<String, PropertyDescriptor> _propertyDescriptorsMap;
+    
     /**
      * Used for Serialization
      */
@@ -107,6 +114,7 @@ public class CompositeComponentBeanInfo 
     {
         super();
         _descriptor = descriptor;
+        getBeanDescriptor().setValue(PROPERTY_DESCRIPTOR_MAP_KEY, new PropertyDescriptorMap());
     }
     
     @Override
@@ -170,6 +178,7 @@ public class CompositeComponentBeanInfo 
         _descriptor.setName((String) in.readObject());
         _descriptor.setPreferred(in.readBoolean());
         _descriptor.setShortDescription((String) in.readObject());
+        _descriptor.setValue(PROPERTY_DESCRIPTOR_MAP_KEY, new PropertyDescriptorMap());
         
         Map<String,Object> map = (Map) in.readObject();
         
@@ -200,7 +209,8 @@ public class CompositeComponentBeanInfo 
             // we only use it when VDL.retargetAttachedObjects() is called and this only
             // happen when the view is built. Also, try to serialize this instances could
             // cause unwanted exceptions.
-            if (!AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY.equals(name))
+            if (!AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY.equals(name) &&
+                !PROPERTY_DESCRIPTOR_MAP_KEY.equals(name))
             {
                 map.put(name, _descriptor.getValue(name));
             }
@@ -209,4 +219,122 @@ public class CompositeComponentBeanInfo 
         out.writeObject(_propertyDescriptors);
         
     }
+    
+    public Map<String, PropertyDescriptor> getPropertyDescriptorsMap()
+    {
+        if (_propertyDescriptors == null)
+        {
+            return Collections.emptyMap();
+        }
+        else
+        {
+            if (_propertyDescriptors.isEmpty())
+            {
+                return Collections.emptyMap();
+            }
+            else if (_propertyDescriptorsMap == null)
+            {
+                int initCapacity = (_propertyDescriptors.size() * 4 + 3) / 3;
+                _propertyDescriptorsMap = new HashMap<String, PropertyDescriptor>(initCapacity);
+                for (PropertyDescriptor p : _propertyDescriptors)
+                {
+                    if (!_propertyDescriptorsMap.containsKey(p.getName()))
+                    {
+                        _propertyDescriptorsMap.put(p.getName(), p);
+                    }
+                }
+            }
+            else if (_propertyDescriptorsMap.size() != _propertyDescriptors.size())
+            {
+                for (PropertyDescriptor p : _propertyDescriptors)
+                {
+                    if (!_propertyDescriptorsMap.containsKey(p.getName()))
+                    {
+                        _propertyDescriptorsMap.put(p.getName(), p);
+                    }
+                }
+                if (_propertyDescriptorsMap.size() != _propertyDescriptors.size())
+                {
+                    // PropertyDescriptor was removed
+                    _propertyDescriptorsMap.clear();
+                    for (PropertyDescriptor p : _propertyDescriptors)
+                    {
+                        if (!_propertyDescriptorsMap.containsKey(p.getName()))
+                        {
+                            _propertyDescriptorsMap.put(p.getName(), p);
+                        }
+                    }
+                }
+            }
+            return _propertyDescriptorsMap;
+        }
+    }
+    
+    /**
+     * Read only map for fast access. It works as an indirection over the real list.
+     */
+    public class PropertyDescriptorMap implements Map<String, PropertyDescriptor>
+    {
+        
+        public int size()
+        {
+            return getPropertyDescriptorsMap().size();
+        }
+
+        public boolean isEmpty()
+        {
+            return getPropertyDescriptorsMap().isEmpty();
+        }
+
+       
+        public boolean containsKey(Object key)
+        {
+            return getPropertyDescriptorsMap().containsKey(key);
+        }
+
+        public boolean containsValue(Object value)
+        {
+            return getPropertyDescriptorsMap().containsValue(value);
+        }
+
+        public PropertyDescriptor get(Object key)
+        {
+            return getPropertyDescriptorsMap().get(key);
+        }
+
+        public PropertyDescriptor put(String key, PropertyDescriptor value)
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public PropertyDescriptor remove(Object key)
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public void putAll(Map<? extends String, ? extends PropertyDescriptor> m)
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public void clear()
+        {
+            throw new UnsupportedOperationException();
+        }
+
+        public Set<String> keySet()
+        {
+            return getPropertyDescriptorsMap().keySet();
+        }
+
+        public Collection<PropertyDescriptor> values()
+        {
+            return getPropertyDescriptorsMap().values();
+        }
+
+        public Set<Entry<String, PropertyDescriptor>> entrySet()
+        {
+            return getPropertyDescriptorsMap().entrySet();
+        }
+    }
 }