org.apache.directory.shared.ldap.util
Class PropertiesUtils

java.lang.Object
  extended byorg.apache.directory.shared.ldap.util.PropertiesUtils

public class PropertiesUtils
extends java.lang.Object

A utility class used for accessing, finding, merging and macro expanding properties, on disk, via URLS or as resources.

Version:
$Rev: 379008 $
Author:
Apache Directory Project

Constructor Summary
PropertiesUtils()
           
 
Method Summary
static void discover(java.util.Properties keys, java.util.Properties[] sources, boolean haltOnDiscovery)
          Merges a set of properties from source Properties into a target properties instance containing keys.
static java.lang.String discover(java.lang.String key, java.util.Properties[] sources, boolean haltOnDiscovery)
          Discovers a value within a set of Properties either halting on the first time the property is discovered or continuing on to take the last value found for the property key.
static java.util.List fill(java.util.Properties props, java.lang.String key, java.util.List values)
          Fills a list with the space delimited values of a property.
static java.util.List fill(java.util.Properties props, java.lang.String key, java.util.List values, java.lang.String delimiter)
          Fills a list with the space delimited values of a property.
static java.util.Set fill(java.util.Properties props, java.lang.String key, java.util.Set values)
          Fills a set with the space delimited values of a property.
static java.util.Set fill(java.util.Properties props, java.lang.String key, java.util.Set values, java.lang.String delimiter)
          Fills a set with the delimited values of a property.
static javax.naming.directory.Attributes fillAttributes(java.util.Properties props, java.lang.String key, javax.naming.directory.Attributes values)
          Creates, fills and returns an Attributes instance using the LDIF encoded within the property value.
static java.util.Collection fillCollection(java.util.Properties props, java.lang.String key, java.util.Collection values, java.lang.String delimiter)
          Fills a collection with the delimited values of a property.
static java.util.Properties findProperties(java.io.File dir, java.lang.String filename)
          Create a new properties object and load the properties file if it exists relative to [dir]/[filename] or [dir]/[filename].properties.
static java.util.Properties findUserProperties(java.lang.String filename)
          Loads a properties object in a properties file if it exists relative to the filename ${user.home}.
static int get(java.util.Hashtable ht, java.lang.Object key, int defval)
          Gets a property or entry value from a hashtable and tries to transform whatever the value may be to an primitive integer.
static boolean get(java.util.Properties props, java.lang.String key, boolean defaultValue)
          Gets a String property as a boolean returning a defualt if the key is not present.
static byte get(java.util.Properties props, java.lang.String key, byte defaultValue)
           
static char get(java.util.Properties props, java.lang.String key, char defaultValue)
           
static long get(java.util.Properties props, java.lang.String key, long defaultValue)
           
static java.util.Properties getProperties(java.lang.ClassLoader classloader, java.lang.String path)
          Loads a properties file as a CL resource if it exists and returns an empty Properties object otherwise.
static java.util.Properties getProperties(java.lang.Class clazz, java.lang.String path)
          Loads a properties file as a class resource if it exists and returns an empty Properties object otherwise.
static java.util.Properties getProperties(java.io.File file)
          Creates a properties object and loads the properties in the file otherwise and empty property object will be returned.
static java.util.Properties getStaticProperties(java.lang.Class ref)
          Load a properties from a resource relative to a supplied class.
static java.util.Properties getStaticProperties(java.lang.Class ref, java.lang.String path)
          Load properties from a resource relative to a supplied class and path.
static void macroExpand(java.util.Properties expanded, java.util.Properties[] optionals)
          Expands out a set of property key macros in the following format ${foo.bar} where foo.bar is a property key, by dereferencing the value of the key using the original source Properties and other optional Properties.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertiesUtils

public PropertiesUtils()
Method Detail

findUserProperties

public static java.util.Properties findUserProperties(java.lang.String filename)
Loads a properties object in a properties file if it exists relative to the filename ${user.home}. If the file ${user.home}/[filename] does not exist then one last attempt to find the file is made if filename does not have a .properties extension. If so and ${user.home}/[filename].properties exists then it is loaded.

Parameters:
filename - the properties file name with or without an extension
Returns:
the user properties object

findProperties

public static java.util.Properties findProperties(java.io.File dir,
                                                  java.lang.String filename)
Create a new properties object and load the properties file if it exists relative to [dir]/[filename] or [dir]/[filename].properties.

Parameters:
dir - the base directory
filename - the full fine name or the base name w/o the extension
Returns:
the loaded properties object

getStaticProperties

public static java.util.Properties getStaticProperties(java.lang.Class ref)
Load a properties from a resource relative to a supplied class. First an attempt is made to locate a property file colocated with the class with the name [class].properties. If this cannot be found or errors result an empty Properties file is returned.

Parameters:
ref - a class to use for relative path references
Returns:
the static properties

getStaticProperties

public static java.util.Properties getStaticProperties(java.lang.Class ref,
                                                       java.lang.String path)
Load properties from a resource relative to a supplied class and path.

Parameters:
ref - a class to use for relative path references
path - the relative path to the resoruce
Returns:
the static properties

getProperties

public static java.util.Properties getProperties(java.io.File file)
Creates a properties object and loads the properties in the file otherwise and empty property object will be returned.

Parameters:
file - the properties file
Returns:
the properties object

getProperties

public static java.util.Properties getProperties(java.lang.ClassLoader classloader,
                                                 java.lang.String path)
Loads a properties file as a CL resource if it exists and returns an empty Properties object otherwise.

Parameters:
classloader - the loader to use for the resources
path - the path to the resource
Returns:
the loaded or new Properties

getProperties

public static java.util.Properties getProperties(java.lang.Class clazz,
                                                 java.lang.String path)
Loads a properties file as a class resource if it exists and returns an empty Properties object otherwise.

Parameters:
clazz - the class to use for resolving the resources
path - the relative path to the resource
Returns:
the loaded or new Properties

macroExpand

public static void macroExpand(java.util.Properties expanded,
                               java.util.Properties[] optionals)
Expands out a set of property key macros in the following format ${foo.bar} where foo.bar is a property key, by dereferencing the value of the key using the original source Properties and other optional Properties. If the original expanded Properties contain the value for the macro key, foo.bar, then dereferencing stops by using the value in the expanded Properties: the other optional Properties are NOT used at all. If the original expanded Properties do NOT contain the value for the macro key, then the optional Properties are used in order. The first of the optionals to contain the value for the macro key (foo.bar) shorts the search. Hence the first optional Properties in the array to contain a value for the macro key (foo.bar) is used to set the expanded value. If a macro cannot be expanded because it's key was not defined within the expanded Properties or one of the optional Properties then it is left as is.

Parameters:
expanded - the Properties to perform the macro expansion upon
optionals - null or an optional set of Properties to use for dereferencing macro keys (foo.bar)

discover

public static java.lang.String discover(java.lang.String key,
                                        java.util.Properties[] sources,
                                        boolean haltOnDiscovery)
Discovers a value within a set of Properties either halting on the first time the property is discovered or continuing on to take the last value found for the property key.

Parameters:
key - a property key
sources - a set of source Properties
haltOnDiscovery - true if we stop on finding a value, false otherwise
Returns:
the value found or null

discover

public static void discover(java.util.Properties keys,
                            java.util.Properties[] sources,
                            boolean haltOnDiscovery)
Merges a set of properties from source Properties into a target properties instance containing keys. This method does not allow null overrides.

Parameters:
keys - the keys to discover values for
sources - the sources to search
haltOnDiscovery - true to halt on first find or false to continue to last find

get

public static boolean get(java.util.Properties props,
                          java.lang.String key,
                          boolean defaultValue)
Gets a String property as a boolean returning a defualt if the key is not present. In any case, true, on, 1 and yes strings return true and everything else returns

Parameters:
props - the properties to get the value from
key - the property key
defaultValue - the default value to return if key is not present
Returns:
true defaultValue if property does not exist, else return true if the String value is one of 'true', 'on', '1', 'yes', otherwise false is returned

get

public static int get(java.util.Hashtable ht,
                      java.lang.Object key,
                      int defval)
Gets a property or entry value from a hashtable and tries to transform whatever the value may be to an primitive integer.

Parameters:
ht - the hashtable to access for the value
key - the key to use when accessing the ht
defval - the default value to use if the key is not contained in ht or if the value cannot be represented as a primitive integer.
Returns:
the primitive integer representation of a hashtable value

get

public static long get(java.util.Properties props,
                       java.lang.String key,
                       long defaultValue)

get

public static byte get(java.util.Properties props,
                       java.lang.String key,
                       byte defaultValue)

get

public static char get(java.util.Properties props,
                       java.lang.String key,
                       char defaultValue)

fill

public static java.util.Set fill(java.util.Properties props,
                                 java.lang.String key,
                                 java.util.Set values)
Fills a set with the space delimited values of a property. If values is null a new Set is created and returned.

Parameters:
props - the properties to get the property values from
key - the key of the multivalued property
values - the values to populate
Returns:
the values set so it can be filled then used

fill

public static java.util.Set fill(java.util.Properties props,
                                 java.lang.String key,
                                 java.util.Set values,
                                 java.lang.String delimiter)
Fills a set with the delimited values of a property. If values is null a new Set is created and returned.

Parameters:
props - the properties to get the property values from
key - the key of the multivalued property
values - the values to populate
delimiter - the delimiter string to split the property with
Returns:
the values set so it can be filled then used

fill

public static java.util.List fill(java.util.Properties props,
                                  java.lang.String key,
                                  java.util.List values)
Fills a list with the space delimited values of a property. The list maintains the order of values in the multivalued property. If values is null a new List is created and returned.

Parameters:
props - the properties to get the property values from
key - the key of the multivalued property
values - the values to populate
Returns:
the values list so it can be filled then used

fill

public static java.util.List fill(java.util.Properties props,
                                  java.lang.String key,
                                  java.util.List values,
                                  java.lang.String delimiter)
Fills a list with the space delimited values of a property. The list maintains the order of values in the multivalued property. If values is null a new List is created and returned.

Parameters:
props - the properties to get the property values from
key - the key of the multivalued property
values - the values to populate
delimiter - the delimiter string to split the property with
Returns:
the values list so it can be filled then used

fillCollection

public static java.util.Collection fillCollection(java.util.Properties props,
                                                  java.lang.String key,
                                                  java.util.Collection values,
                                                  java.lang.String delimiter)
Fills a collection with the delimited values of a property.

Parameters:
props - the properties to get the property values from
key - the key of the multivalued property
values - the values to populate
delimiter - the delimiter string to split the property with
Returns:
the values collection so it can be filled then used

fillAttributes

public static javax.naming.directory.Attributes fillAttributes(java.util.Properties props,
                                                               java.lang.String key,
                                                               javax.naming.directory.Attributes values)
                                                        throws javax.naming.NamingException
Creates, fills and returns an Attributes instance using the LDIF encoded within the property value. The LDIF should use '*' (asterisk) characters as line delimiters within the property value. These are replaced with newlines and fed to the LDIF parser. Also note that the LdifParser deposites the DN as a property within the attributes object.

Parameters:
props - the properties to get the ldif property from
key - the key for the LDIF property
Returns:
the attributes for the encoded LDIF entry
Throws:
javax.naming.NamingException


Copyright © 2003-2006 . All Rights Reserved.