myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu4...@apache.org
Subject svn commit: r1003666 - in /myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared: application/DefaultViewHandlerSupport.java resource/BaseResourceHandlerSupport.java resource/ResourceHandlerCache.java util/WebConfigParamUtils.java
Date Fri, 01 Oct 2010 22:00:44 GMT
Author: lu4242
Date: Fri Oct  1 22:00:43 2010
New Revision: 1003666

URL: http://svn.apache.org/viewvc?rev=1003666&view=rev
Log:
MYFACES-2936 Add new utility class for init parameters WebConfigParamUtils

Added:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/WebConfigParamUtils.java
Modified:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java?rev=1003666&r1=1003665&r2=1003666&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java
(original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java
Fri Oct  1 22:00:43 2010
@@ -31,6 +31,7 @@ import javax.faces.context.ExternalConte
 import javax.faces.context.FacesContext;
 
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
+import org.apache.myfaces.shared.util.WebConfigParamUtils;
 
 /**
  * A ViewHandlerSupport implementation for use with standard Java Servlet engines,
@@ -490,10 +491,9 @@ public class DefaultViewHandlerSupport i
             }
 
             //if in production, make sure that the cache is not explicitly disabled via context
param
-            String configParam = context.getExternalContext().getInitParameter(
-                    CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE);
-            _checkedViewIdCacheEnabled = configParam == null ? CHECKED_VIEWID_CACHE_ENABLED_DEFAULT
-                    : Boolean.parseBoolean(configParam);
+            _checkedViewIdCacheEnabled = WebConfigParamUtils.getBooleanInitParameter(context.getExternalContext(),

+                    CHECKED_VIEWID_CACHE_ENABLED_ATTRIBUTE,
+                    CHECKED_VIEWID_CACHE_ENABLED_DEFAULT);
 
             if (log.isLoggable(Level.FINE))
             {
@@ -508,10 +508,8 @@ public class DefaultViewHandlerSupport i
     {
         ExternalContext externalContext = context.getExternalContext();
 
-        String configParam = externalContext == null ? null : externalContext
-                .getInitParameter(CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE);
-        return configParam == null ? CHECKED_VIEWID_CACHE_DEFAULT_SIZE
-                : Integer.parseInt(configParam);
+        return WebConfigParamUtils.getIntegerInitParameter(externalContext,
+                CHECKED_VIEWID_CACHE_SIZE_ATTRIBUTE, CHECKED_VIEWID_CACHE_DEFAULT_SIZE);
     }
 
     private class _CheckedViewIDMap<K, V> extends LinkedHashMap<K, V>

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java?rev=1003666&r1=1003665&r2=1003666&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java
(original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/BaseResourceHandlerSupport.java
Fri Oct  1 22:00:43 2010
@@ -25,6 +25,7 @@ import javax.faces.context.FacesContext;
 
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 import org.apache.myfaces.shared.application.FacesServletMapping;
+import org.apache.myfaces.shared.util.WebConfigParamUtils;
 
 /**
  * A ResourceHandlerSupport implementation for use with standard Java Servlet engines,
@@ -219,22 +220,9 @@ public class BaseResourceHandlerSupport 
     {
         if (_maxTimeExpires == null)
         {
-            String time = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(RESOURCE_MAX_TIME_EXPIRES);
-            if (time != null && time.length() > 0)
-            {
-                try
-                {
-                    _maxTimeExpires = Long.parseLong(time);
-                }
-                catch (NumberFormatException e)
-                {
-                    _maxTimeExpires = 604800000L;
-                }
-            }
-            else
-            {
-                _maxTimeExpires = 604800000L;
-            }
+            _maxTimeExpires = WebConfigParamUtils.getLongInitParameter(
+                    FacesContext.getCurrentInstance().getExternalContext(), 
+                    RESOURCE_MAX_TIME_EXPIRES, 604800000L);
         }
         return _maxTimeExpires;
     }

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java?rev=1003666&r1=1003665&r2=1003666&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java
(original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java
Fri Oct  1 22:00:43 2010
@@ -29,6 +29,7 @@ import javax.faces.context.ExternalConte
 import javax.faces.context.FacesContext;
 
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
+import org.apache.myfaces.shared.util.WebConfigParamUtils;
 
 public class ResourceHandlerCache
 {
@@ -112,10 +113,9 @@ public class ResourceHandlerCache
                 return false; //don't cache right now, but don't disable it yet either
 
             //if in production, make sure that the cache is not explicitly disabled via context
param
-            String configParam = externalContext
-                    .getInitParameter(ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_ATTRIBUTE);
-            _resourceCacheEnabled = configParam == null ? ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_DEFAULT
-                    : Boolean.parseBoolean(configParam);
+            _resourceCacheEnabled = WebConfigParamUtils.getBooleanInitParameter(externalContext,

+                    ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_ATTRIBUTE,
+                    ResourceHandlerCache.RESOURCE_HANDLER_CACHE_ENABLED_DEFAULT);
 
             if (log.isLoggable(Level.FINE))
             {
@@ -128,13 +128,11 @@ public class ResourceHandlerCache
 
     private int getMaxSize()
     {
+        
         ExternalContext externalContext = FacesContext.getCurrentInstance()
                 .getExternalContext();
-
-        String configParam = externalContext == null ? null : externalContext
-                .getInitParameter(RESOURCE_HANDLER_CACHE_SIZE_ATTRIBUTE);
-        return configParam == null ? RESOURCE_HANDLER_CACHE_DEFAULT_SIZE
-                : Integer.parseInt(configParam);
+        return WebConfigParamUtils.getIntegerInitParameter(externalContext, 
+                RESOURCE_HANDLER_CACHE_SIZE_ATTRIBUTE, RESOURCE_HANDLER_CACHE_DEFAULT_SIZE);
     }
 
     public static class ResourceKey

Added: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/WebConfigParamUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/WebConfigParamUtils.java?rev=1003666&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/WebConfigParamUtils.java
(added)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/util/WebConfigParamUtils.java
Fri Oct  1 22:00:43 2010
@@ -0,0 +1,672 @@
+/*
+ *  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 org.apache.myfaces.shared.util;
+
+import javax.faces.FacesException;
+import javax.faces.context.ExternalContext;
+
+/**
+ * Utility class to handle web config parameters
+ * 
+ * @author Leonardo Uribe
+ * @since 4.0.4
+ */
+public final class WebConfigParamUtils
+{
+    public final static String[] COMMON_TRUE_VALUES = {"true", "on", "yes"};
+    public final static String[] COMMON_FALSE_VALUES = {"false", "off", "no"};
+
+    /**
+     * Gets the String init parameter value from the specified context. If the parameter
is an empty String or a String
+     * containing only white space, this method returns <code>null</code>
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     *            
+     * @return the parameter if it was specified and was not empty, <code>null</code>
otherwise
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */    
+    public static String getStringInitParameter(ExternalContext context, String name)
+    {
+        return getStringInitParameter(context,name,null);
+    }
+
+    /**
+     * Gets the String init parameter value from the specified context. If the parameter
is an empty String or a String
+     * containing only white space, this method returns <code>null</code>
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param defaultValue
+     *            the value by default if null or empty
+     *            
+     * @return the parameter if it was specified and was not empty, <code>null</code>
otherwise
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */    
+    public static String getStringInitParameter(ExternalContext context, String name, String
defaultValue)
+    {
+        if (name == null)
+            throw new NullPointerException();
+        
+        String param = context.getInitParameter(name);
+        
+        if (param == null)
+        {
+            return defaultValue;
+        }
+
+        param = param.trim();
+        if (param.length() == 0)
+        {
+            return defaultValue;
+        }
+
+        return param;
+    }
+    
+    /**
+     * Gets the String init parameter value from the specified context. If the parameter
is an empty String or a String
+     * containing only white space, this method returns <code>null</code>
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names, the first one is scanned first. Usually used
when a param has multiple aliases
+     *            
+     * @return the parameter if it was specified and was not empty, <code>null</code>
otherwise
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */    
+    public static String getStringInitParameter(ExternalContext context, String[] names)
+    {
+        return getStringInitParameter(context, names, null);
+    }
+    
+    /**
+     * Gets the String init parameter value from the specified context. If the parameter
is an empty String or a String
+     * containing only white space, this method returns <code>null</code>
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names, the first one is scanned first. Usually used
when a param has multiple aliases
+     * @param defaultValue
+     *            the value by default if null or empty
+     *            
+     * @return the parameter if it was specified and was not empty, <code>null</code>
otherwise
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */    
+    public static String getStringInitParameter(ExternalContext context, String[] names,
String defaultValue)
+    {
+        if (names == null)
+            throw new NullPointerException();
+        
+        String param = null;
+        
+        for (String name : names)
+        {
+            if (name == null)
+                throw new NullPointerException();
+            
+            param = context.getInitParameter(name);
+            if (param != null)
+            {
+                break;
+            }
+        }
+        
+        if (param == null)
+        {
+            return defaultValue;
+        }
+
+        param = param.trim();
+        if (param.length() == 0)
+        {
+            return defaultValue;
+        }
+
+        return param;
+    }
+    
+    /**
+     * Gets the boolean init parameter value from the specified context. If the parameter
was not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a boolean
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static boolean getBooleanInitParameter(ExternalContext context, String name)
+    {
+        return getBooleanInitParameter(context, name, false);
+    }
+    
+    /**
+     * Gets the boolean init parameter value from the specified context. If the parameter
was not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a boolean
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static boolean getBooleanInitParameter(ExternalContext context, String name, boolean
defaultValue)
+    {
+        if (name == null)
+            throw new NullPointerException();
+
+        String param = getStringInitParameter(context, name);
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            return Boolean.parseBoolean(param.toLowerCase());
+        }
+    }
+    
+    /**
+     * Gets the boolean init parameter value from the specified context. If the parameter
was not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * @param valuesIgnoreCase
+     *            an array of valid values to match
+     * @param returnOnValueEqualsIgnoreCase
+     *            the value to return in case the parameter match with valuesIgnoreCase
+     * 
+     * @return the init parameter value as a boolean
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static boolean getBooleanInitParameter(ExternalContext context, String name, boolean
defaultValue, String [] valuesIgnoreCase, boolean returnOnValueEqualsIgnoreCase)
+    {
+        if (name == null)
+            throw new NullPointerException();
+
+        String param = getStringInitParameter(context, name);
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            if (valuesIgnoreCase != null)
+            {
+                for (String trueValue : valuesIgnoreCase)
+                {
+                    if (trueValue.equalsIgnoreCase(param))
+                    {
+                        return returnOnValueEqualsIgnoreCase;
+                    }
+                }
+                return defaultValue;
+            }
+            else 
+            {
+                return Boolean.parseBoolean(param.toLowerCase());
+            }
+        }
+    }    
+
+    /**
+     * Gets the boolean init parameter value from the specified context. If the parameter
was not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names
+     * 
+     * @return the init parameter value as a boolean
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    
+    public static boolean getBooleanInitParameter(ExternalContext context, String[] names)
+    {
+        return getBooleanInitParameter(context, names, false);
+    }
+
+    /**
+     * Gets the boolean init parameter value from the specified context. If the parameter
was not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a boolean
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static boolean getBooleanInitParameter(ExternalContext context, String[] names,
boolean defaultValue)
+    {
+        if (names == null)
+            throw new NullPointerException();
+        
+        String param = null;
+        for (String name : names)
+        {
+            if (name == null)
+                throw new NullPointerException();
+            
+            param = getStringInitParameter(context, name);
+            if (param != null)
+            {
+                break;
+            }
+        }
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            return Boolean.parseBoolean(param.toLowerCase());
+        }
+    }
+    
+    /**
+     * Gets the boolean init parameter value from the specified context. If the parameter
was not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * @param valuesIgnoreCase
+     *            an array of valid values to match
+     * @param returnOnValueEqualsIgnoreCase
+     *            the value to return in case the parameter match with valuesIgnoreCase
+     * 
+     * @return the init parameter value as a boolean
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    
+    public static boolean getBooleanInitParameter(ExternalContext context, String[] names,
boolean defaultValue, String [] valuesIgnoreCase, boolean returnOnValueEqualsIgnoreCase)
+    {
+        if (names == null)
+            throw new NullPointerException();
+        
+        String param = null;
+        for (String name : names)
+        {
+            if (name == null)
+                throw new NullPointerException();
+            
+            param = getStringInitParameter(context, name);
+            if (param != null)
+            {
+                break;
+            }
+        }
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            if (valuesIgnoreCase != null)
+            {
+                for (String trueValue : valuesIgnoreCase)
+                {
+                    if (trueValue.equalsIgnoreCase(param))
+                    {
+                        return returnOnValueEqualsIgnoreCase;
+                    }
+                }
+                return defaultValue;
+            }
+            else 
+            {
+                return Boolean.parseBoolean(param.toLowerCase());
+            }
+        }
+    }
+    
+    /**
+     * Gets the int init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a int
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static int getIntegerInitParameter(ExternalContext context, String name)
+    {
+        return getIntegerInitParameter(context, name, 0);
+    }
+    
+    /**
+     * Gets the int init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a int
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static int getIntegerInitParameter(ExternalContext context, String name, int defaultValue)
+    {
+        if (name == null)
+            throw new NullPointerException();
+
+        String param = getStringInitParameter(context, name);
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            return Integer.parseInt(param.toLowerCase());
+        }
+    }
+
+    /**
+     * Gets the int init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names
+     * 
+     * @return the init parameter value as a int
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static int getIntegerInitParameter(ExternalContext context, String[] names)
+    {
+        return getIntegerInitParameter(context, names, 0);
+    }
+
+    /**
+     * Gets the int init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a int
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    
+    public static int getIntegerInitParameter(ExternalContext context, String[] names, int
defaultValue)
+    {
+        if (names == null)
+            throw new NullPointerException();
+        
+        String param = null;
+        for (String name : names)
+        {
+            if (name == null)
+                throw new NullPointerException();
+            
+            param = getStringInitParameter(context, name);
+            if (param != null)
+            {
+                break;
+            }
+        }
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            return Integer.parseInt(param.toLowerCase());
+        }
+    }
+    
+    /**
+     * Gets the long init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a long
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static long getLongInitParameter(ExternalContext context, String name)
+    {
+        return getLongInitParameter(context, name, 0);
+    }
+    
+    /**
+     * Gets the long init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a long
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    public static long getLongInitParameter(ExternalContext context, String name, long defaultValue)
+    {
+        if (name == null)
+            throw new NullPointerException();
+
+        String param = getStringInitParameter(context, name);
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            return Long.parseLong(param.toLowerCase());
+        }
+    }
+
+    /**
+     * Gets the long init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names
+     * 
+     * @return the init parameter value as a long
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    
+    public static long getLongInitParameter(ExternalContext context, String[] names)
+    {
+        return getLongInitParameter(context, names, 0);
+    }
+    
+    /**
+     * Gets the long init parameter value from the specified context. If the parameter was
not specified, the default
+     * value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param names
+     *            the init parameter's names
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as a long
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    
+    public static long getLongInitParameter(ExternalContext context, String[] names, long
defaultValue)
+    {
+        if (names == null)
+            throw new NullPointerException();
+        
+        String param = null;
+        for (String name : names)
+        {
+            if (name == null)
+                throw new NullPointerException();
+            
+            param = getStringInitParameter(context, name);
+            if (param != null)
+            {
+                break;
+            }
+        }
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            return Long.parseLong(param.toLowerCase());
+        }
+    }
+
+    /**
+     * Gets the init parameter value from the specified context and instanciate it. If the
parameter was not specified,
+     * the default value is used instead.
+     * 
+     * @param context
+     *            the application's external context
+     * @param name
+     *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     * @param defaultValue
+     *            the default value to return in case the parameter was not set
+     * 
+     * @return the init parameter value as an object instance
+     * 
+     * @throws NullPointerException
+     *             if context or name is <code>null</code>
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T getInstanceInitParameter(ExternalContext context, String name,
String deprecatedName, T defaultValue)
+    {
+        String param = getStringInitParameter(context, name, deprecatedName);
+        if (param == null)
+        {
+            return defaultValue;
+        }
+        else
+        {
+            try
+            {
+                return (T) ClassUtils.classForName(param).newInstance();
+            }
+            catch (Exception e)
+            {
+                throw new FacesException("Error Initializing Object[" + param + "]", e);
+            }
+        }
+    }
+}



Mime
View raw message