Class ConfigurationUtils
- Version:
- $Id: ConfigurationUtils.java 1208795 2011-11-30 21:18:17Z oheger $
- Author:
- Herve Quiroz, Emmanuel Bourg
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
append
(Configuration source, Configuration target) Append all properties from the source configuration to the target configuration.static Configuration
cloneConfiguration
(Configuration config) Clones the given configuration object if this is possible.static HierarchicalConfiguration
Converts the passed in configuration to a hierarchical one.static HierarchicalConfiguration
convertToHierarchical
(Configuration conf, ExpressionEngine engine) Converts the passed inConfiguration
object to a hierarchical one using the specifiedExpressionEngine
.static void
copy
(Configuration source, Configuration target) Copy all properties from the source configuration to the target configuration.static void
dump
(Configuration configuration, PrintStream out) Dump the configuration key/value mappings to some ouput stream.static void
dump
(Configuration configuration, PrintWriter out) Dump the configuration key/value mappings to some writer.static void
Enables runtime exceptions for the specified configuration object.static File
fileFromURL
(URL url) Tries to convert the specified URL to a file object.static File
Tries to convert the specified base path and file name into a file object.static URL
Constructs a URL from a base path and a file name.static URL
Return the location of the specified resource by searching the user home directory, the current classpath and the system classpath.static URL
Return the location of the specified resource by searching the user home directory, the current classpath and the system classpath.static URL
locate
(FileSystem fileSystem, String base, String name) Return the location of the specified resource by searching the user home directory, the current classpath and the system classpath.static String
toString
(Configuration configuration) Get a string representation of the key/value mappings of a configuration.
-
Method Details
-
dump
Dump the configuration key/value mappings to some ouput stream.- Parameters:
configuration
- the configurationout
- the output stream to dump the configuration to
-
dump
Dump the configuration key/value mappings to some writer.- Parameters:
configuration
- the configurationout
- the writer to dump the configuration to
-
toString
Get a string representation of the key/value mappings of a configuration.- Parameters:
configuration
- the configuration- Returns:
- a string representation of the configuration
-
copy
Copy all properties from the source configuration to the target configuration. Properties in the target configuration are replaced with the properties with the same key in the source configuration.
Note: This method is not able to handle some specifics of configurations derived from
AbstractConfiguration
(e.g. list delimiters). For a full support of all of these features thecopy()
method ofAbstractConfiguration
should be used. In a future release this method might become deprecated.- Parameters:
source
- the source configurationtarget
- the target configuration- Since:
- 1.1
-
append
Append all properties from the source configuration to the target configuration. Properties in the source configuration are appended to the properties with the same key in the target configuration.
Note: This method is not able to handle some specifics of configurations derived from
AbstractConfiguration
(e.g. list delimiters). For a full support of all of these features thecopy()
method ofAbstractConfiguration
should be used. In a future release this method might become deprecated.- Parameters:
source
- the source configurationtarget
- the target configuration- Since:
- 1.1
-
convertToHierarchical
Converts the passed in configuration to a hierarchical one. If the configuration is already hierarchical, it is directly returned. Otherwise all properties are copied into a new hierarchical configuration.- Parameters:
conf
- the configuration to convert- Returns:
- the new hierarchical configuration (the result is null if and only if the passed in configuration is null)
- Since:
- 1.3
-
convertToHierarchical
public static HierarchicalConfiguration convertToHierarchical(Configuration conf, ExpressionEngine engine) Converts the passed inConfiguration
object to a hierarchical one using the specifiedExpressionEngine
. This conversion works by adding the keys found in the configuration to a newly created hierarchical configuration. When adding new keys to a hierarchical configuration the keys are interpreted by itsExpressionEngine
. If they contain special characters (e.g. brackets) that are treated in a special way by the default expression engine, it may be necessary using a specific engine that can deal with such characters. Otherwise null can be passed in for theExpressionEngine
; then the default expression engine is used. If the passed in configuration is already hierarchical, it is directly returned. (However, theExpressionEngine
is set if it is not null.) Otherwise all properties are copied into a new hierarchical configuration.- Parameters:
conf
- the configuration to convertengine
- theExpressionEngine
for the hierarchical configuration or null for the default- Returns:
- the new hierarchical configuration (the result is null if and only if the passed in configuration is null)
- Since:
- 1.6
-
cloneConfiguration
public static Configuration cloneConfiguration(Configuration config) throws ConfigurationRuntimeException Clones the given configuration object if this is possible. If the passed in configuration object implements theCloneable
interface, itsclone()
method will be invoked. Otherwise an exception will be thrown.- Parameters:
config
- the configuration object to be cloned (can be null)- Returns:
- the cloned configuration (null if the argument was null, too)
- Throws:
ConfigurationRuntimeException
- if cloning is not supported for this object- Since:
- 1.3
-
getURL
Constructs a URL from a base path and a file name. The file name can be absolute, relative or a full URL. If necessary the base path URL is applied.- Parameters:
basePath
- the base path URL (can be null)file
- the file name- Returns:
- the resulting URL
- Throws:
MalformedURLException
- if URLs are invalid
-
locate
Return the location of the specified resource by searching the user home directory, the current classpath and the system classpath.- Parameters:
name
- the name of the resource- Returns:
- the location of the resource
-
locate
Return the location of the specified resource by searching the user home directory, the current classpath and the system classpath.- Parameters:
base
- the base path of the resourcename
- the name of the resource- Returns:
- the location of the resource
-
locate
Return the location of the specified resource by searching the user home directory, the current classpath and the system classpath.- Parameters:
fileSystem
- the FileSystem to use.base
- the base path of the resourcename
- the name of the resource- Returns:
- the location of the resource
-
getFile
Tries to convert the specified base path and file name into a file object. This method is called e.g. by the save() methods of file based configurations. The parameter strings can be relative files, absolute files and URLs as well. This implementation checks first whether the passed in file name is absolute. If this is the case, it is returned. Otherwise further checks are performed whether the base path and file name can be combined to a valid URL or a valid file name. Note: The test if the passed in file name is absolute is performed usingjava.io.File.isAbsolute()
. If the file name starts with a slash, this method will return true on Unix, but false on Windows. So to ensure correct behavior for relative file names on all platforms you should never let relative paths start with a slash. E.g. in a configuration definition file do not use something like that:<properties fileName="/subdir/my.properties"/>
Under Windows this path would be resolved relative to the configuration definition file. Under Unix this would be treated as an absolute path name.- Parameters:
basePath
- the base pathfileName
- the file name- Returns:
- the file object (null if no file can be obtained)
-
fileFromURL
Tries to convert the specified URL to a file object. If this fails, null is returned. Note: This code has been copied from theFileUtils
class from Commons IO.- Parameters:
url
- the URL- Returns:
- the resulting file object
-
enableRuntimeExceptions
Enables runtime exceptions for the specified configuration object. This method can be used for configuration implementations that may face errors on normal property access, e.g.DatabaseConfiguration
orJNDIConfiguration
. Per default such errors are simply logged and then ignored. This implementation will register a specialConfigurationErrorListener
that throws a runtime exception (namely aConfigurationRuntimeException
) on each received error event.- Parameters:
src
- the configuration, for which runtime exceptions are to be enabled; this configuration must be derived fromEventSource
-