org.openbp.common.setting
Class SettingUtil

java.lang.Object
  extended by org.openbp.common.setting.SettingUtil

public final class SettingUtil
extends java.lang.Object

The setting util class provides utility methods for access to setting using the SettingResolver mechanism. It also keeps a standard setting resolver. Note that the application is resonsible for setting up the standard resolver! Setting values may be strings, booleans and integers. Settings can be retrieved using one of the methods
getStringSetting(String)
getBooleanSetting(String, boolean)
getIntSetting(String, int) In order to store a setting value, call setStringSetting(String, String)
setBooleanSetting(String, boolean)
setIntSetting(String, int) For each method, there is a version that allows to specify the setting resolver and a version that delegates to the standard resolver. Some setting providers may save the settings automatically if one of the set methods is called, whereas others may cache the settings and save them to the persistent storage after the saveSettings() method has been called.

Author:
Heiko Erhardt

Field Summary
static char PROVIDER_SEPARATOR
          Provider separator
 
Method Summary
static java.util.Properties expandProperties(java.util.Properties props)
          Performs expandVariables on all values of the given properties.
static java.lang.String expandVariables(java.lang.String s, boolean mandatory)
          Substitutes variables in the given string using the standard setting resolver.
static java.lang.String expandVariables(java.lang.String s, boolean mandatory, SettingResolver resolver)
          Substitutes variables in the given string using the supplied setting resolver.
static boolean getBooleanSetting(java.lang.String name, boolean dflt)
          Gets a boolean setting value or the default using the standard setting resolver.
static boolean getBooleanSetting(java.lang.String name, boolean dflt, SettingResolver resolver)
          Gets a boolean setting value or the default using the supplied setting resolver.
static int getIntSetting(java.lang.String name, int dflt)
          Gets an integer setting value or the default using the standard setting resolver.
static int getIntSetting(java.lang.String name, int dflt, SettingResolver resolver)
          Gets an integer setting value or the default using the supplied setting resolver.
static boolean getMandatoryBooleanSetting(java.lang.String name)
          Gets a mandatory boolean setting value or the default using the standard setting resolver.
static int getMandatoryIntSetting(java.lang.String name)
          Gets a mandatory integer setting value or the default using the standard setting resolver.
static java.lang.String getMandatoryStringSetting(java.lang.String name)
          Gets a mandatory string setting value using the standard setting resolver.
static SettingResolver getStandardResolver()
          Gets the standard resolver.
static java.lang.String getStringSetting(java.lang.String name)
          Gets a string setting value using the standard setting resolver.
static java.lang.String getStringSetting(java.lang.String name, java.lang.String dflt)
          Gets a string setting value or the default using the standard setting resolver.
static java.lang.String getStringSetting(java.lang.String name, java.lang.String dflt, SettingResolver resolver)
          Gets a string setting value or the default using the supplied setting resolver.
static java.util.Properties overwriteProperties(java.util.Properties props)
          Performs a overwrite support for all properties keys of the given properties.
static boolean saveSettings()
          Saves the current setting values of the setting provider of the standard setting resolver.
static boolean saveSettings(SettingResolver resolver)
          Saves the current setting values of the setting provider of the specified setting resolver.
static SettingProvider setBooleanSetting(java.lang.String name, boolean value)
          Sets a boolean setting value using the standard setting resolver.
static SettingProvider setBooleanSetting(java.lang.String name, boolean value, SettingResolver resolver)
          Sets a boolean setting value using the supplied setting resolver.
static SettingProvider setIntSetting(java.lang.String name, int value)
          Sets an integer setting value using the standard setting resolver.
static SettingProvider setIntSetting(java.lang.String name, int value, SettingResolver resolver)
          Sets an integer setting value using the supplied setting resolver.
static void setStandardResolver(SettingResolver standardResolverArg)
          Sets the standard resolver.
static SettingProvider setStringSetting(java.lang.String name, java.lang.String value)
          Sets a string setting value using the standard setting resolver.
static SettingProvider setStringSetting(java.lang.String name, java.lang.String value, SettingResolver resolver)
          Sets a string setting value using the supplied setting resolver.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROVIDER_SEPARATOR

public static final char PROVIDER_SEPARATOR
Provider separator

See Also:
Constant Field Values
Method Detail

getStandardResolver

public static SettingResolver getStandardResolver()
Gets the standard resolver.


setStandardResolver

public static void setStandardResolver(SettingResolver standardResolverArg)
Sets the standard resolver.


getStringSetting

public static java.lang.String getStringSetting(java.lang.String name,
                                                java.lang.String dflt,
                                                SettingResolver resolver)
Gets a string setting value or the default using the supplied setting resolver.

Parameters:
name - Name of the setting
dflt - Default value of the setting
resolver - Resolver to use
Returns:
The setting value or the default value if the setting does not exist

getBooleanSetting

public static boolean getBooleanSetting(java.lang.String name,
                                        boolean dflt,
                                        SettingResolver resolver)
Gets a boolean setting value or the default using the supplied setting resolver.

Parameters:
name - Name of the setting
dflt - Default value of the setting
resolver - Resolver to use
Returns:
The setting value or the default value if the setting does not exist

setStringSetting

public static SettingProvider setStringSetting(java.lang.String name,
                                               java.lang.String value,
                                               SettingResolver resolver)
Sets a string setting value using the supplied setting resolver.

Parameters:
name - Name of the setting
value - value of the setting
resolver - Resolver to use
Returns:
The setting provider that saved the setting

getIntSetting

public static int getIntSetting(java.lang.String name,
                                int dflt,
                                SettingResolver resolver)
Gets an integer setting value or the default using the supplied setting resolver.

Parameters:
name - Name of the setting
dflt - Default value of the setting
resolver - Resolver to use
Returns:
The setting value or the default value if the setting does not exist

setBooleanSetting

public static SettingProvider setBooleanSetting(java.lang.String name,
                                                boolean value,
                                                SettingResolver resolver)
Sets a boolean setting value using the supplied setting resolver.

Parameters:
name - Name of the setting
value - value of the setting
resolver - Resolver to use
Returns:
The setting provider that saved the setting

setIntSetting

public static SettingProvider setIntSetting(java.lang.String name,
                                            int value,
                                            SettingResolver resolver)
Sets an integer setting value using the supplied setting resolver.

Parameters:
name - Name of the setting
value - value of the setting
resolver - Resolver to use
Returns:
The setting provider that saved the setting

expandVariables

public static java.lang.String expandVariables(java.lang.String s,
                                               boolean mandatory,
                                               SettingResolver resolver)
Substitutes variables in the given string using the supplied setting resolver. Will replace variables of the form "${var}" with the respective setting value or the empty string. Can optionally hold prefixes or suffixes of the form ${?value-prefix?default-prefix?var?value-suffix?default-suffix} that will be included depending if the variable is set or not.

Parameters:
s - String to process
mandatory - true If an unknown variable should result in an exception.
false If an unknown variable should be sustituted by an empty string.
resolver - Resolver to use
Returns:
The string with the substitutes applied
Throws:
java.lang.RuntimeException - On an unknown mandatory variable

saveSettings

public static boolean saveSettings(SettingResolver resolver)
Saves the current setting values of the setting provider of the specified setting resolver.

Parameters:
resolver - Resolver to use
Returns:
true If the setting were successfully saved false If there was an error saving the properties of at least one provider.

expandProperties

public static java.util.Properties expandProperties(java.util.Properties props)
Performs expandVariables on all values of the given properties.

Parameters:
props - Props to check
Returns:
Expanded properties

overwriteProperties

public static java.util.Properties overwriteProperties(java.util.Properties props)
Performs a overwrite support for all properties keys of the given properties.

Parameters:
props - Props to check
Returns:
overwriten properties

getStringSetting

public static java.lang.String getStringSetting(java.lang.String name)
Gets a string setting value using the standard setting resolver.

Parameters:
name - Name of the setting
Returns:
The setting value or null if the setting does not exist

getStringSetting

public static java.lang.String getStringSetting(java.lang.String name,
                                                java.lang.String dflt)
Gets a string setting value or the default using the standard setting resolver.

Parameters:
name - Name of the setting
dflt - Default value of the setting
Returns:
The setting value or the default value if the setting does not exist

getBooleanSetting

public static boolean getBooleanSetting(java.lang.String name,
                                        boolean dflt)
Gets a boolean setting value or the default using the standard setting resolver.

Parameters:
name - Name of the setting
dflt - Default value of the setting
Returns:
The setting value or the default value if the setting does not exist

setStringSetting

public static SettingProvider setStringSetting(java.lang.String name,
                                               java.lang.String value)
Sets a string setting value using the standard setting resolver.

Parameters:
name - Name of the setting
value - value of the setting
Returns:
The setting provider that saved the setting

getIntSetting

public static int getIntSetting(java.lang.String name,
                                int dflt)
Gets an integer setting value or the default using the standard setting resolver.

Parameters:
name - Name of the setting
dflt - Default value of the setting
Returns:
The setting value or the default value if the setting does not exist

setBooleanSetting

public static SettingProvider setBooleanSetting(java.lang.String name,
                                                boolean value)
Sets a boolean setting value using the standard setting resolver.

Parameters:
name - Name of the setting
value - value of the setting
Returns:
The setting provider that saved the setting

setIntSetting

public static SettingProvider setIntSetting(java.lang.String name,
                                            int value)
Sets an integer setting value using the standard setting resolver.

Parameters:
name - Name of the setting
value - value of the setting
Returns:
The setting provider that saved the setting

getMandatoryStringSetting

public static java.lang.String getMandatoryStringSetting(java.lang.String name)
Gets a mandatory string setting value using the standard setting resolver.

Parameters:
name - Name of the setting
Returns:
The setting value or null if the setting does not exist
Throws:
java.lang.RuntimeException - If the setting is not present

getMandatoryBooleanSetting

public static boolean getMandatoryBooleanSetting(java.lang.String name)
Gets a mandatory boolean setting value or the default using the standard setting resolver.

Parameters:
name - Name of the setting
Returns:
The setting value or the default value if the setting does not exist
Throws:
java.lang.RuntimeException - If the setting is not present

getMandatoryIntSetting

public static int getMandatoryIntSetting(java.lang.String name)
Gets a mandatory integer setting value or the default using the standard setting resolver.

Parameters:
name - Name of the setting
Returns:
The setting value or the default value if the setting does not exist
Throws:
java.lang.RuntimeException - If the setting is not present

expandVariables

public static java.lang.String expandVariables(java.lang.String s,
                                               boolean mandatory)
Substitutes variables in the given string using the standard setting resolver. Will replace variables of the form "${var}" with the respective setting value or the empty string.

Parameters:
s - String to process
mandatory - true If an unknown variable should result in an exception.
false If an unknown variable should be sustituted by an empty string.
Returns:
The string with the substitutes applied
Throws:
NestedRuntimeException - On an unknown mandatory variable

saveSettings

public static boolean saveSettings()
Saves the current setting values of the setting provider of the standard setting resolver.

Returns:
true If the setting were successfully saved false If there was an error saving the properties of at least one provider.


Copyright © 2011. All Rights Reserved.