Griffon 1.2.0

griffon.util
[Java] Class ConfigUtils

java.lang.Object
  griffon.util.ConfigUtils

public final class ConfigUtils

Utility class for reading configuration properties.

Authors:
Andres Almiray


Field Summary
private static String GROOVY_SUFFIX

private static Logger LOG

private static String PROPERTIES_SUFFIX

 
Constructor Summary
private ConfigUtils()

 
Method Summary
static ConfigReader createConfigReader()

Creates a new ConfigReader instance configured with default conditional blocks.

static Object getConfigValue(Map config, String key)

Returns the value for the specified key.

static Object getConfigValue(Map config, String key, Object defaultValue)

Returns the value for the specified key with an optional default value if no match is found.

static boolean getConfigValueAsBoolean(Map config, String key)

Returns the value for the specified key coerced to a boolean.

static boolean getConfigValueAsBoolean(Map config, String key, boolean defaultValue)

Returns the value for the specified key with an optional default value if no match is found.

static int getConfigValueAsInt(Map config, String key)

Returns the value for the specified key coerced to an int.

static int getConfigValueAsInt(Map config, String key, int defaultValue)

Returns the value for the specified key with an optional default value if no match is found.

static String getConfigValueAsString(Map config, String key)

Returns the value for the specified key converted to a String.

static String getConfigValueAsString(Map config, String key, String defaultValue)

Returns the value for the specified key with an optional default value if no match is found.

static String getFilenameExtension(String path)

Extract the filename extension from the given path, e.g.

static boolean isValueDefined(Map config, String key)

Returns true if there's a on-null value for the specified key.

static Class loadClass(String className)

static ConfigObject loadConfig(String configFileName)

Loads configuration settings defined in a Groovy script and a properties file as fallback.

static ConfigObject loadConfig(Class configClass)

Loads configuration settings defined in a Groovy script and a properties file as fallback.

static ConfigObject loadConfig(Class configClass, String configFileName)

Loads configuration settings defined in a Groovy script and a properties file as fallback.

static ConfigObject loadConfig(ConfigReader configReader, Class configClass, String configFileName)

Loads configuration settings defined in a Groovy script and a properties file as fallback.

private static ConfigObject loadConfigFile(ConfigReader configReader, String configFileName)

static ConfigObject loadConfigWithI18n(String baseConfigFileName)

Loads configuration settings defined in a Groovy script and a properties file.

static ConfigObject loadConfigWithI18n(Class baseConfigClass)

Loads configuration settings defined in a Groovy script and a properties file.

static ConfigObject loadConfigWithI18n(Class baseConfigClass, String baseConfigFileName)

Loads configuration settings defined in a Groovy script and a properties file.

static ConfigObject loadConfigWithI18n(Locale locale, ConfigReader configReader, Class baseConfigClass, String baseConfigFileName)

Loads configuration settings defined in a Groovy script and a properties file.

static Map merge(Map defaults, Map overrides)

Merges two maps using ConfigObject.merge().

static Class safeLoadClass(String className)

static String stripFilenameExtension(String path)

Strip the filename extension from the given path, e.g.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

GROOVY_SUFFIX

private static final String GROOVY_SUFFIX


LOG

private static final Logger LOG


PROPERTIES_SUFFIX

private static final String PROPERTIES_SUFFIX


 
Constructor Detail

ConfigUtils

private ConfigUtils()


 
Method Detail

createConfigReader

public static ConfigReader createConfigReader()
Creates a new ConfigReader instance configured with default conditional blocks.
The following list enumerates the conditional blocks that get registered automatically:
Returns:
a newly instantiated ConfigReader.
Since:
1.1.0


getConfigValue

public static Object getConfigValue(Map config, String key)
Returns the value for the specified key.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
Returns:
the value of the key. May return null


getConfigValue

public static Object getConfigValue(Map config, String key, Object defaultValue)
Returns the value for the specified key with an optional default value if no match is found.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
defaultValue - the value to send back if no match is found
Returns:
the value of the key or the default value if no match is found


getConfigValueAsBoolean

public static boolean getConfigValueAsBoolean(Map config, String key)
Returns the value for the specified key coerced to a boolean.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
Returns:
the value of the key. Returns false if no match.


getConfigValueAsBoolean

public static boolean getConfigValueAsBoolean(Map config, String key, boolean defaultValue)
Returns the value for the specified key with an optional default value if no match is found.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
defaultValue - the value to send back if no match is found
Returns:
the value of the key or the default value if no match is found


getConfigValueAsInt

public static int getConfigValueAsInt(Map config, String key)
Returns the value for the specified key coerced to an int.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
Returns:
the value of the key. Returns 0 if no match.


getConfigValueAsInt

public static int getConfigValueAsInt(Map config, String key, int defaultValue)
Returns the value for the specified key with an optional default value if no match is found.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
defaultValue - the value to send back if no match is found
Returns:
the value of the key or the default value if no match is found


getConfigValueAsString

public static String getConfigValueAsString(Map config, String key)
Returns the value for the specified key converted to a String.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
Returns:
the value of the key. Returns "" if no match.


getConfigValueAsString

public static String getConfigValueAsString(Map config, String key, String defaultValue)
Returns the value for the specified key with an optional default value if no match is found.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
defaultValue - the value to send back if no match is found
Returns:
the value of the key or the default value if no match is found


getFilenameExtension

public static String getFilenameExtension(String path)
Extract the filename extension from the given path, e.g. "mypath/myfile.txt" -> "txt".
Parameters:
path - the file path (may be null)
Returns:
the extracted filename extension, or null if none


isValueDefined

public static boolean isValueDefined(Map config, String key)
Returns true if there's a on-null value for the specified key.
Parameters:
config - the configuration object to be searched upon
key - the key to be searched
Returns:
true if there's a value for the specified key, false otherwise


loadClass

public static Class loadClass(String className)


loadConfig

public static ConfigObject loadConfig(String configFileName)
Loads configuration settings defined in a Groovy script and a properties file as fallback.
The name of the script matches the name of the file.
Parameters:
configFileName - the configuration file
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


loadConfig

public static ConfigObject loadConfig(Class configClass)
Loads configuration settings defined in a Groovy script and a properties file as fallback.
The alternate properties file matches the simple name of the script.
Parameters:
configClass - the script's class, may be null
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


loadConfig

public static ConfigObject loadConfig(Class configClass, String configFileName)
Loads configuration settings defined in a Groovy script and a properties file as fallback.
Parameters:
configClass - the script's class, may be null
configFileName - the alternate configuration file
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


loadConfig

public static ConfigObject loadConfig(ConfigReader configReader, Class configClass, String configFileName)
Loads configuration settings defined in a Groovy script and a properties file as fallback.
Parameters:
configReader - a ConfigReader instance already configured
configClass - the script's class, may be null
configFileName - the alternate configuration file
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


loadConfigFile

private static ConfigObject loadConfigFile(ConfigReader configReader, String configFileName)


loadConfigWithI18n

public static ConfigObject loadConfigWithI18n(String baseConfigFileName)
Loads configuration settings defined in a Groovy script and a properties file. The script and file names are Locale aware.

The name of the script matches the name of the file.
The following suffixes will be used besides the base names for script and file

Parameters:
baseConfigFileName - the configuration file
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


loadConfigWithI18n

public static ConfigObject loadConfigWithI18n(Class baseConfigClass)
Loads configuration settings defined in a Groovy script and a properties file. The script and file names are Locale aware.

The alternate properties file matches the simple name of the script.
The following suffixes will be used besides the base names for script and file

Parameters:
baseConfigClass - the script's class
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


loadConfigWithI18n

public static ConfigObject loadConfigWithI18n(Class baseConfigClass, String baseConfigFileName)
Loads configuration settings defined in a Groovy script and a properties file. The script and file names are Locale aware.

The following suffixes will be used besides the base names for script and file

Parameters:
baseConfigClass - the script's class, may be null
baseConfigFileName - the alternate configuration file
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


loadConfigWithI18n

public static ConfigObject loadConfigWithI18n(Locale locale, ConfigReader configReader, Class baseConfigClass, String baseConfigFileName)
Loads configuration settings defined in a Groovy script and a properties file. The script and file names are Locale aware.

The following suffixes will be used besides the base names for script and file

Parameters:
locale - the locale to use
configReader - a ConfigReader instance already configured
baseConfigClass - the script's class, may be null
baseConfigFileName - the alternate configuration file
Returns:
a merged configuration between the script and the alternate file. The file has precedence over the script.
Since:
1.1.0


merge

public static Map merge(Map defaults, Map overrides)
Merges two maps using ConfigObject.merge().
Parameters:
defaults - configuration values available by default
overrides - configuration values that override defaults
Returns:
the result of merging both maps


safeLoadClass

public static Class safeLoadClass(String className)


stripFilenameExtension

public static String stripFilenameExtension(String path)
Strip the filename extension from the given path, e.g. "mypath/myfile.txt" -> "mypath/myfile".
Parameters:
path - the file path (may be null)
Returns:
the path with stripped filename extension, or null if none


 

Groovy Documentation