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 2011/09/21 17:27:40 UTC

svn commit: r1173711 - in /myfaces/core/trunk/api/src/main/java/javax/faces/component: _ComponentAttributesMap.java _PropertyDescriptorHolder.java

Author: lu4242
Date: Wed Sep 21 15:27:40 2011
New Revision: 1173711

URL: http://svn.apache.org/viewvc?rev=1173711&view=rev
Log:
MYFACES-3262 [perf] [concurrency] Cache read and write method in _ComponentAttributesMap

Added:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/_PropertyDescriptorHolder.java
Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java?rev=1173711&r1=1173710&r2=1173711&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentAttributesMap.java Wed Sep 21 15:27:40 2011
@@ -73,11 +73,11 @@ class _ComponentAttributesMap implements
     // the javabean properties of the associated component. This is built by
     // introspection on the associated UIComponent. Don't serialize this as
     // it can always be recreated when needed.
-    private transient Map<String, PropertyDescriptor> _propertyDescriptorMap = null;
+    private transient Map<String, _PropertyDescriptorHolder> _propertyDescriptorMap = null;
 
     // Cache for component property descriptors
-    private static Map<Class<?>, Map<String, PropertyDescriptor>> _propertyDescriptorCache = 
-        new WeakHashMap<Class<?>, Map<String, PropertyDescriptor>>();
+    private static Map<Class<?>, Map<String, _PropertyDescriptorHolder>> _propertyDescriptorCache = 
+        new WeakHashMap<Class<?>, Map<String, _PropertyDescriptorHolder>>();
     
     private boolean _isCompositeComponent;
     private boolean _isCompositeComponentSet;
@@ -250,7 +250,7 @@ class _ComponentAttributesMap implements
         Object value;
 
         // is there a javabean property to read?
-        PropertyDescriptor propertyDescriptor = getPropertyDescriptor((String) key);
+        _PropertyDescriptorHolder propertyDescriptor = getPropertyDescriptor((String) key);
         if (propertyDescriptor != null)
         {
             value = getComponentProperty(propertyDescriptor);
@@ -342,7 +342,7 @@ class _ComponentAttributesMap implements
     public Object remove(Object key)
     {
         checkKey(key);
-        PropertyDescriptor propertyDescriptor = getPropertyDescriptor((String) key);
+        _PropertyDescriptorHolder propertyDescriptor = getPropertyDescriptor((String) key);
         if (propertyDescriptor != null)
         {
             throw new IllegalArgumentException("Cannot remove component property attribute");
@@ -385,7 +385,7 @@ class _ComponentAttributesMap implements
             throw new NullPointerException("key");
         }
 
-        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(key);
+        _PropertyDescriptorHolder propertyDescriptor = getPropertyDescriptor(key);
         if (propertyDescriptor == null)
         {
             if (value == null)
@@ -427,7 +427,7 @@ class _ComponentAttributesMap implements
      * currently 100 UIInputText components means performing introspection
      * on the UIInputText component 100 times.
      */
-    private PropertyDescriptor getPropertyDescriptor(String key)
+    private _PropertyDescriptorHolder getPropertyDescriptor(String key)
     {
         if (_propertyDescriptorMap == null)
         {
@@ -447,14 +447,15 @@ class _ComponentAttributesMap implements
                     throw new FacesException(e);
                 }
                 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
-                _propertyDescriptorMap = new HashMap<String, PropertyDescriptor>();
+                _propertyDescriptorMap = new HashMap<String, _PropertyDescriptorHolder>();
                 for (int i = 0; i < propertyDescriptors.length; i++)
                 {
                     PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
-                    if (propertyDescriptor.getReadMethod() != null)
+                    Method readMethod = propertyDescriptor.getReadMethod();
+                    if (readMethod != null)
                     {
                         _propertyDescriptorMap.put(propertyDescriptor.getName(),
-                                propertyDescriptor);
+                                new _PropertyDescriptorHolder(propertyDescriptor, readMethod));
                     }
                 }
                 // ... and put it in cache
@@ -483,7 +484,7 @@ class _ComponentAttributesMap implements
      * @throws FacesException           if any other problem occurs while invoking
      *                                  the getter method.
      */
-    private Object getComponentProperty(PropertyDescriptor propertyDescriptor)
+    private Object getComponentProperty(_PropertyDescriptorHolder propertyDescriptor)
     {
         Method readMethod = propertyDescriptor.getReadMethod();
         if (readMethod == null)
@@ -510,7 +511,7 @@ class _ComponentAttributesMap implements
      * @throws FacesException           if any other problem occurs while invoking
      *                                  the getter method.
      */
-    private void setComponentProperty(PropertyDescriptor propertyDescriptor, Object value)
+    private void setComponentProperty(_PropertyDescriptorHolder propertyDescriptor, Object value)
     {
         Method writeMethod = propertyDescriptor.getWriteMethod();
         if (writeMethod == null)

Added: myfaces/core/trunk/api/src/main/java/javax/faces/component/_PropertyDescriptorHolder.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/_PropertyDescriptorHolder.java?rev=1173711&view=auto
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/_PropertyDescriptorHolder.java (added)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/_PropertyDescriptorHolder.java Wed Sep 21 15:27:40 2011
@@ -0,0 +1,70 @@
+/*
+ * 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 javax.faces.component;
+
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Method;
+
+class _PropertyDescriptorHolder
+{
+    private final PropertyDescriptor _descriptor;
+    private final Method _readMethod;
+    private Method _writeMethod;
+
+    public _PropertyDescriptorHolder(PropertyDescriptor descriptor)
+    {
+        _descriptor = descriptor;
+        _readMethod = _descriptor.getReadMethod();
+    }
+
+    public _PropertyDescriptorHolder(PropertyDescriptor descriptor, Method readMethod)
+    {
+        _descriptor = descriptor;
+        _readMethod = readMethod;
+    }
+    
+    public String getName()
+    {
+        return _descriptor.getName();
+    }
+    
+    public Method getReadMethod()
+    {
+        return _readMethod;
+    }
+    
+    public Method getWriteMethod()
+    {
+        // In facelets, the Method instance used to write the variable is stored
+        // in a variable (see org.apache.myfaces.view.facelets.tag.BeanPropertyTagRule),
+        // so the impact of this synchronized call at the end is minimal compared with 
+        // getReadMethod. That's the reason why cache it here in a lazy way is enough
+        // instead retrieve it as soon as this holder is created.
+        if (_writeMethod == null)
+        {
+            _writeMethod = _descriptor.getWriteMethod(); 
+        }
+        return _writeMethod;
+    }
+    
+    public PropertyDescriptor getPropertyDescriptor()
+    {
+        return _descriptor;
+    }
+}