You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2011/05/20 17:28:09 UTC

svn commit: r1125430 - in /myfaces/core/trunk: api/src/main/java/javax/faces/component/ impl/src/main/java/org/apache/myfaces/application/ impl/src/test/java/org/apache/myfaces/application/

Author: martinkoci
Date: Fri May 20 15:28:09 2011
New Revision: 1125430

URL: http://svn.apache.org/viewvc?rev=1125430&view=rev
Log:
MYFACES-3151 [perf] minimize ExternalContext.getInitParameter invocations in myfaces API

Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponent.java
    myfaces/core/trunk/api/src/main/java/javax/faces/component/UINamingContainer.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ApplicationImplAnnotationTest.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponent.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponent.java?rev=1125430&r1=1125429&r2=1125430&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponent.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponent.java Fri May 20 15:28:09 2011
@@ -171,6 +171,9 @@ public abstract class UIComponent implem
      * to be implemented from here and internally it depends from this property.
      */
     private boolean _initialStateMarked = false;
+    
+    /** Value of the {@link UIComponent#HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME} parameter */ 
+    private Boolean _honorCurrentComponentAttributes;
 
     public UIComponent() {
     }
@@ -406,9 +409,10 @@ public abstract class UIComponent implem
      * @since 2.0
      */
     public static UIComponent getCurrentComponent(FacesContext context) {
-        String param = context.getExternalContext().getInitParameter(HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME);
         
-        if (param != null && Boolean.valueOf(param).booleanValue())
+        Boolean honorCurrentComponentAttributes = _getHonorCurrentComponentAttributes(context);
+        
+        if (honorCurrentComponentAttributes == Boolean.TRUE)
         {
             return (UIComponent) context.getAttributes().get(UIComponent.CURRENT_COMPONENT);
         }
@@ -434,9 +438,10 @@ public abstract class UIComponent implem
      * @since 2.0
      */
     public static UIComponent getCurrentCompositeComponent(FacesContext context) {
-        String param = context.getExternalContext().getInitParameter(HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME);
         
-        if (param != null && Boolean.valueOf(param).booleanValue())
+        Boolean honorCurrentComponentAttributes = _getHonorCurrentComponentAttributes(context);
+        
+        if (honorCurrentComponentAttributes == Boolean.TRUE)
         {
             return (UIComponent) context.getAttributes().get(UIComponent.CURRENT_COMPOSITE_COMPONENT);
         }
@@ -929,9 +934,12 @@ public abstract class UIComponent implem
     public final void popComponentFromEL(FacesContext context) {
         Map<Object, Object> contextAttributes = context.getAttributes();        
         
-        String param = context.getExternalContext().getInitParameter(HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME);
+        if (_honorCurrentComponentAttributes == null)
+        {
+            _honorCurrentComponentAttributes = _getHonorCurrentComponentAttributes(context);
+        }
         
-        if (param != null && Boolean.valueOf(param).booleanValue())
+        if (_honorCurrentComponentAttributes == Boolean.TRUE)
         {
             // Pop the current UIComponent from the FacesContext attributes map so that the previous 
             // UIComponent, if any, becomes the current component.
@@ -1012,10 +1020,14 @@ public abstract class UIComponent implem
             component = this;
         }
 
-        Map<Object, Object> contextAttributes = context.getAttributes();        
-        String param = context.getExternalContext().getInitParameter(HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME);
+        Map<Object, Object> contextAttributes = context.getAttributes();
         
-        if (param != null && Boolean.valueOf(param).booleanValue())
+        if (_honorCurrentComponentAttributes == null)
+        {
+            _honorCurrentComponentAttributes = _getHonorCurrentComponentAttributes(context);
+        }
+        
+        if (_honorCurrentComponentAttributes == Boolean.TRUE)
         {
             UIComponent currentComponent = (UIComponent) contextAttributes.get(UIComponent.CURRENT_COMPONENT);
             
@@ -1072,6 +1084,29 @@ public abstract class UIComponent implem
         return UIComponent.isCompositeComponent(this);
     }
     
+    /**
+     * Gets value of "javax.faces.HONOR_CURRENT_COMPONENT_ATTRIBUTES" parameter cached in facesContext.attributes 
+     * or resolves that param and caches its value in facesContext.attributes.    
+     * 
+     * @return canonical Boolean value for parameter "javax.faces.HONOR_CURRENT_COMPONENT_ATTRIBUTES"
+     */
+    private static Boolean _getHonorCurrentComponentAttributes(FacesContext facesContext) {
+        // performance note: we cache value in facesContext.attributes because
+        // 1) methods pushComponentToEL, popComponentFromEl, getCurrentComponent a getCurrentCompositeComponent
+        // can use that value
+        // 2) getExternalContext().getInitParameter has undetermined performance. In typical JSF app, there
+        // are one or two wrappers around external context; servletContext.getInitParameter has also unknown 
+        // implementation and performance
+        Map<Object, Object> attributes = facesContext.getAttributes();
+        Boolean paramValue = (Boolean) attributes.get(HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME);
+        if (paramValue == null) {
+            String param = facesContext.getExternalContext().getInitParameter(HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME);
+            paramValue = Boolean.valueOf((param != null && Boolean.valueOf(param).booleanValue()));
+            attributes.put(HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME, paramValue);
+        }
+        return paramValue;
+    }
+    
     private static class BundleMap implements Map<String, String> {
 
         private ResourceBundle _bundle;

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/UINamingContainer.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/UINamingContainer.java?rev=1125430&r1=1125429&r2=1125430&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/UINamingContainer.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/UINamingContainer.java Fri May 20 15:28:09 2011
@@ -19,6 +19,7 @@
 package javax.faces.component;
 
 import java.util.Collection;
+import java.util.Map;
 
 import javax.faces.component.visit.VisitCallback;
 import javax.faces.component.visit.VisitContext;
@@ -97,22 +98,30 @@ public class UINamingContainer extends U
     @SuppressWarnings("deprecation")
     public static char getSeparatorChar(FacesContext context)
     {
-        ExternalContext eContext = context.getExternalContext();
-        
-        // The implementation must determine if there is a <context-param> with the value given by the 
-        // value of the symbolic constant SEPARATOR_CHAR_PARAM_NAME
-        String param = eContext.getInitParameter(SEPARATOR_CHAR_PARAM_NAME);
-        if (param == null || param.length() == 0)
-        {
-            // Otherwise, the value of the symbolic constant NamingContainer.SEPARATOR_CHAR must be returned.
-            return NamingContainer.SEPARATOR_CHAR;
-        }
-        else
-        {
-            // If there is a value for this param, the first character of the value must be returned from 
-            // this method
-            return param.charAt(0);
+        Map<Object, Object> attributes = context.getAttributes();
+        Character separatorChar = (Character) attributes.get(SEPARATOR_CHAR_PARAM_NAME);
+        if (separatorChar == null)
+        { // not cached yet for this request
+            ExternalContext eContext = context.getExternalContext();
+            
+            // The implementation must determine if there is a <context-param> with the value given by the 
+            // value of the symbolic constant SEPARATOR_CHAR_PARAM_NAME
+            String param = eContext.getInitParameter(SEPARATOR_CHAR_PARAM_NAME);
+            if (param == null || param.length() == 0)
+            {
+                // Otherwise, the value of the symbolic constant NamingContainer.SEPARATOR_CHAR must be returned.
+                separatorChar = NamingContainer.SEPARATOR_CHAR;
+            }
+            else
+            {
+                // If there is a value for this param, the first character of the value must be returned from 
+                // this method
+                separatorChar = param.charAt(0);
+            }
+            // Cache it under standard name
+            attributes.put(SEPARATOR_CHAR_PARAM_NAME, separatorChar);
         }
+        return separatorChar.charValue();
     }
     
     @JSFProperty(deferredValueType="java.lang.Boolean")

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java?rev=1125430&r1=1125429&r2=1125430&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ApplicationImpl.java Fri May 20 15:28:09 2011
@@ -196,6 +196,9 @@ public class ApplicationImpl extends App
     private List<Class<? extends Converter>> _noArgConstructorConverterClasses 
             = new ArrayList<Class<? extends Converter>>();
     
+    /** Value of javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE parameter */
+    private boolean _dateTimeConverterDefaultTimeZoneIsSystemTimeZone = false; 
+    
     /**
      * Represents semantic null in _componentClassMap. 
      */
@@ -242,6 +245,12 @@ public class ApplicationImpl extends App
 
         if (log.isLoggable(Level.FINEST))
             log.finest("New Application instance created");
+        
+        String configParam = getFaceContext().getExternalContext().getInitParameter(DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME);
+        if (configParam != null && configParam.toLowerCase().equals("true"))
+        {
+            _dateTimeConverterDefaultTimeZoneIsSystemTimeZone = true;
+        }
     }
 
     // ~ Methods
@@ -507,7 +516,7 @@ public class ApplicationImpl extends App
         {
             return;
         }
-
+        
         // spec: If this argument is null the return from source.getClass() must be used as the sourceBaseType. 
         if (sourceBaseType == null)
         {
@@ -1511,14 +1520,9 @@ public class ApplicationImpl extends App
                 .getConverterConfiguration(converterClass.getName());
         
         // if the converter is a DataTimeConverter, check the init param for the default timezone (since 2.0)
-        if (converter instanceof DateTimeConverter)
+        if (converter instanceof DateTimeConverter && _dateTimeConverterDefaultTimeZoneIsSystemTimeZone)
         {    
-            String configParam = getFaceContext().getExternalContext()
-                    .getInitParameter(DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE_PARAM_NAME);
-            if (configParam != null && configParam.toLowerCase().equals("true"))
-            {
-                ((DateTimeConverter) converter).setTimeZone(TimeZone.getDefault());
-            }
+            ((DateTimeConverter) converter).setTimeZone(TimeZone.getDefault());
         }
 
         if (converterConfig != null && converterConfig.getProperties().size() > 0)

Modified: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ApplicationImplAnnotationTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ApplicationImplAnnotationTest.java?rev=1125430&r1=1125429&r2=1125430&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ApplicationImplAnnotationTest.java (original)
+++ myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ApplicationImplAnnotationTest.java Fri May 20 15:28:09 2011
@@ -61,6 +61,8 @@ public class ApplicationImplAnnotationTe
     @Override
     protected void setFactories() throws Exception
     {
+        ((MockServletContext)servletContext).addInitParameter(
+                "javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE", "true");
         super.setFactories();
         FactoryFinder.setFactory(FactoryFinder.APPLICATION_FACTORY,
                 ApplicationFactoryImpl.class.getName());
@@ -345,8 +347,6 @@ public class ApplicationImplAnnotationTe
     @Test
     public void testDatetimeconverterDefaultTimezoneIsSystemTimezoneInitParameter()
     {
-        ((MockServletContext)servletContext).addInitParameter(
-                "javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE", "true");
         application.addConverter(java.util.Date.class, "javax.faces.convert.DateTimeConverter");
         Converter converter = application.createConverter(java.util.Date.class);
         Assert.assertEquals(((DateTimeConverter) converter).getTimeZone(), TimeZone.getDefault());