Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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;
 
Utility class to handle web config parameters

Author(s):
Leonardo Uribe
Since:
2.0.10 (4.0.4 in shared, 1.0.1 in commons)
 
 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 null

Parameters:
context the application's external context
name the init parameter's name
Returns:
the parameter if it was specified and was not empty, null otherwise
Throws:
java.lang.NullPointerException if context or name is null
     
     public static String getStringInitParameter(ExternalContext contextString 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 null

Parameters:
context the application's external context
name the init parameter's name
defaultValue the value by default if null or empty
Returns:
the parameter if it was specified and was not empty, null otherwise
Throws:
java.lang.NullPointerException if context or name is null
     
     public static String getStringInitParameter(ExternalContext contextString nameString 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 null

Parameters:
context the application's external context
names the init parameter's names, the first one is scanned first. Usually used when a param has multiple aliases
Returns:
the parameter if it was specified and was not empty, null otherwise
Throws:
java.lang.NullPointerException if context or name is null
    
    public static String getStringInitParameter(ExternalContext contextString[] names)
    {
        return getStringInitParameter(contextnamesnull);
    }
    
    
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 null

Parameters:
context the application's external context
names the init parameter's names, the first one is scanned first. Usually used when a param has multiple aliases
defaultValue the value by default if null or empty
Returns:
the parameter if it was specified and was not empty, null otherwise
Throws:
java.lang.NullPointerException if context or name is null
    
    public static String getStringInitParameter(ExternalContext contextString[] namesString 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.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a boolean
Throws:
java.lang.NullPointerException if context or name is null
    public static boolean getBooleanInitParameter(ExternalContext contextString name)
    {
        return getBooleanInitParameter(contextnamefalse);
    }
    
    
Gets the boolean init parameter value from the specified context. If the parameter was not specified, the default value is used instead.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a boolean
Throws:
java.lang.NullPointerException if context or name is null
    public static boolean getBooleanInitParameter(ExternalContext contextString nameboolean defaultValue)
    {
        if (name == null)
        {
            throw new NullPointerException();
        }
        String param = getStringInitParameter(contextname);
        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.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
valuesIgnoreCase an array of valid values to match
returnOnValueEqualsIgnoreCase the value to return in case the parameter match with valuesIgnoreCase
Returns:
the init parameter value as a boolean
Throws:
java.lang.NullPointerException if context or name is null
    public static boolean getBooleanInitParameter(ExternalContext contextString name
            boolean defaultValueString [] valuesIgnoreCaseboolean returnOnValueEqualsIgnoreCase)
    {
        if (name == null)
        {
            throw new NullPointerException();
        }
        String param = getStringInitParameter(contextname);
        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.

Parameters:
context the application's external context
names the init parameter's names
Returns:
the init parameter value as a boolean
Throws:
java.lang.NullPointerException if context or name is null
    
    public static boolean getBooleanInitParameter(ExternalContext contextString[] names)
    {
        return getBooleanInitParameter(contextnamesfalse);
    }

    
Gets the boolean init parameter value from the specified context. If the parameter was not specified, the default value is used instead.

Parameters:
context the application's external context
names the init parameter's names
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a boolean
Throws:
java.lang.NullPointerException if context or name is null
    public static boolean getBooleanInitParameter(ExternalContext contextString[] namesboolean defaultValue)
    {
        if (names == null)
        {
            throw new NullPointerException();
        }
        
        String param = null;
        for (String name : names)
        {
            if (name == null)
            {
                throw new NullPointerException();
            }
            
            param = getStringInitParameter(contextname);
            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.

Parameters:
context the application's external context
names the init parameter's names
defaultValue the default value to return in case the parameter was not set
valuesIgnoreCase an array of valid values to match
returnOnValueEqualsIgnoreCase the value to return in case the parameter match with valuesIgnoreCase
Returns:
the init parameter value as a boolean
Throws:
java.lang.NullPointerException if context or name is null
    
    public static boolean getBooleanInitParameter(ExternalContext contextString[] namesboolean defaultValue,
            String [] valuesIgnoreCaseboolean returnOnValueEqualsIgnoreCase)
    {
        if (names == null)
        {
            throw new NullPointerException();
        }
        
        String param = null;
        for (String name : names)
        {
            if (name == null)
            {
                throw new NullPointerException();
            }
            
            param = getStringInitParameter(contextname);
            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.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a int
Throws:
java.lang.NullPointerException if context or name is null
    public static int getIntegerInitParameter(ExternalContext contextString name)
    {
        return getIntegerInitParameter(contextname, 0);
    }
    
    
Gets the int init parameter value from the specified context. If the parameter was not specified, the default value is used instead.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a int
Throws:
java.lang.NullPointerException if context or name is null
    public static int getIntegerInitParameter(ExternalContext contextString nameint defaultValue)
    {
        if (name == null)
        {
            throw new NullPointerException();
        }
        String param = getStringInitParameter(contextname);
        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.

Parameters:
context the application's external context
names the init parameter's names
Returns:
the init parameter value as a int
Throws:
java.lang.NullPointerException if context or name is null
    public static int getIntegerInitParameter(ExternalContext contextString[] names)
    {
        return getIntegerInitParameter(contextnames, 0);
    }

    
Gets the int init parameter value from the specified context. If the parameter was not specified, the default value is used instead.

Parameters:
context the application's external context
names the init parameter's names
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a int
Throws:
java.lang.NullPointerException if context or name is null
    
    public static int getIntegerInitParameter(ExternalContext contextString[] namesint defaultValue)
    {
        if (names == null)
        {
            throw new NullPointerException();
        }
        
        String param = null;
        for (String name : names)
        {
            if (name == null)
            {
                throw new NullPointerException();
            }
            
            param = getStringInitParameter(contextname);
            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.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a long
Throws:
java.lang.NullPointerException if context or name is null
    public static long getLongInitParameter(ExternalContext contextString name)
    {
        return getLongInitParameter(contextname, 0);
    }
    
    
Gets the long init parameter value from the specified context. If the parameter was not specified, the default value is used instead.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a long
Throws:
java.lang.NullPointerException if context or name is null
    public static long getLongInitParameter(ExternalContext contextString namelong defaultValue)
    {
        if (name == null)
        {
            throw new NullPointerException();
        }
        String param = getStringInitParameter(contextname);
        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.

Parameters:
context the application's external context
names the init parameter's names
Returns:
the init parameter value as a long
Throws:
java.lang.NullPointerException if context or name is null
    
    public static long getLongInitParameter(ExternalContext contextString[] names)
    {
        return getLongInitParameter(contextnames, 0);
    }
    
    
Gets the long init parameter value from the specified context. If the parameter was not specified, the default value is used instead.

Parameters:
context the application's external context
names the init parameter's names
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as a long
Throws:
java.lang.NullPointerException if context or name is null
    
    public static long getLongInitParameter(ExternalContext contextString[] nameslong defaultValue)
    {
        if (names == null)
        {
            throw new NullPointerException();
        }
        
        String param = null;
        for (String name : names)
        {
            if (name == null)
            {
                throw new NullPointerException();
            }
            
            param = getStringInitParameter(contextname);
            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.

Parameters:
context the application's external context
name the init parameter's name
deprecatedName the init parameter's deprecated name.
defaultValue the default value to return in case the parameter was not set
Returns:
the init parameter value as an object instance
Throws:
java.lang.NullPointerException if context or name is null
    @SuppressWarnings("unchecked")
    public static <T> T getInstanceInitParameter(ExternalContext contextString name
            String deprecatedName, T defaultValue)
    {
        String param = getStringInitParameter(contextnamedeprecatedName);
        if (param == null)
        {
            return defaultValue;
        }
        else
        {
            try
            {
                return (T) ClassUtils.classForName(param).newInstance();
            }
            catch (Exception e)
            {
                throw new FacesException("Error Initializing Object[" + param + "]"e);
            }
        }
    }
New to GrepCode? Check out our FAQ X