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.felix.webconsole.internal.servlet;
 
 import java.util.Map;
 
A helper class to get configuration properties.
 
 public class ConfigurationUtil
 {
 
     private ConfigurationUtil()
     {
         // prevent instantiation
     }


    
Returns the named property from the framework. If the property does not exist, the default value def is returned.

Parameters:
context The BundleContext providing framework properties
name The name of the property to return
def The default value if the named property does not exist
Returns:
The value of the named property or def if the property does not exist
 
     public static final String getPropertyBundleContext contextString nameString def )
     {
         String value = context.getPropertyname );
         return ( value == null ) ? def : value;
     }

    
Returns the named property from the framework. If the property does not exist, the default value def is returned.

Parameters:
context The BundleContext providing framework properties
name The name of the property to return
def The default value if the named property does not exist
Returns:
The value of the named property as a string or def if the property does not exist
 
     public static final int getProperty(BundleContext contextString nameint def)
     {
         String value = context.getPropertyname);
         if (value != null)
         {
             try
             {
                 return Integer.parseInt(value.toString());
             }
             catch (NumberFormatException nfe)
             {
                 // don't care
             }
         }
 
         // not a number, not convertible, not set, use default
         return def;
     }


    
Returns the named property from the configuration. If the property does not exist, the default value def is returned.

Parameters:
config The properties from which to returned the named one
name The name of the property to return
def The default value if the named property does not exist
Returns:
The value of the named property as a string or def if the property does not exist
 
     public static final String getProperty(Map configString nameString def)
     {
         Object value = config.get(name);
         if ( value instanceof String )
         {
             final String s = ( ( String ) value ).trim();
            return ( s.length() > 0 ) ? s : def;
        }
        if (value == null)
        {
            return def;
        }
        return String.valueOf(value);
    }

    
Returns the named property from the configuration. If the property does not exist, the default value def is returned.

Parameters:
config The properties from which to returned the named one
name The name of the property to return
def The default value if the named property does not exist
Returns:
The value of the named property as a string or def if the property does not exist
    public static final int getProperty(Map configString nameint def)
    {
        Object value = config.get(name);
        if (value instanceof Number)
        {
            return ((Numbervalue).intValue();
        }
        // try to convert the value to a number
        if (value != null)
        {
            try
            {
                return Integer.parseInt(value.toString());
            }
            catch (NumberFormatException nfe)
            {
                // don't care
            }
        }
        // not a number, not convertible, not set, use default
        return def;
    }

    
Gets a property as String[]

Parameters:
config The properties from which to returned the named one
name The name of the property to return
Returns:
the property value as string array - no matter if originally it was other kind of array, collection or comma-separated string. Returns null if the property is not set.
    public static final String[] getStringArrayProperty(Map configString name)
    {
        Object value = config.get(name);
        if (value == null)
        {
            return null;
        }
        String[] ret = null;
        if (value.getClass().isArray())
        {
            final Object[] names = (Object[]) value;
            ret = new String[names.length];
            for (int i = 0; i < names.lengthi++)
            {
                ret[i] = String.valueOf(names[i]);
            }
        }
        else if (value instanceof Collection)
        {
            Collection collection = (Collectionvalue;
            ret = new String[collection.size()];
            int i = 0;
            for (Iterator iter = collection.iterator(); iter.hasNext();)
            {
                ret[i] = String.valueOf(iter.next());
                i++;
            }
        }
        else if (value instanceof String)
        {
            String pv = ((Stringvalue).trim();
            if (pv.length() != 0)
            {
                StringTokenizer tok = new StringTokenizer(pv",;"); //$NON-NLS-1$
                ret = new String[tok.countTokens()];
                int i = 0;
                while (tok.hasMoreTokens())
                {
                    ret[i] = tok.nextToken();
                    i++;
                }
            }
        }
        return ret;
    }
New to GrepCode? Check out our FAQ X