public final class ConfigurationUtils
extends java.lang.Object
Utility methods to convert configurations.
Modifier and Type | Field and Description |
---|---|
private static org.apache.commons.logging.Log |
log
The logger.
|
private static java.lang.String |
METHOD_CLONE
Constant for the name of the clone() method.
|
(package private) static java.lang.String |
PROTOCOL_FILE
Constant for the file URL protocol.
|
(package private) static java.lang.String |
RESOURCE_PATH_SEPARATOR
Constant for the resource path separator.
|
Modifier | Constructor and Description |
---|---|
private |
ConfigurationUtils()
Private constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
append(Configuration source,
Configuration target)
Append all properties from the source configuration to the target
configuration.
|
(package private) static java.lang.Object |
clone(java.lang.Object obj)
An internally used helper method for cloning objects.
|
static Configuration |
cloneConfiguration(Configuration config)
Clones the given configuration object if this is possible.
|
(package private) static java.io.File |
constructFile(java.lang.String basePath,
java.lang.String fileName)
Helper method for constructing a file object from a base path and a
file name.
|
static HierarchicalConfiguration |
convertToHierarchical(Configuration conf)
Converts the passed in configuration to a hierarchical one.
|
static void |
copy(Configuration source,
Configuration target)
Copy all properties from the source configuration to the target
configuration.
|
static void |
dump(Configuration configuration,
java.io.PrintStream out)
Dump the configuration key/value mappings to some ouput stream.
|
static void |
dump(Configuration configuration,
java.io.PrintWriter out)
Dump the configuration key/value mappings to some writer.
|
static void |
enableRuntimeExceptions(Configuration src)
Enables runtime exceptions for the specified configuration object.
|
static java.io.File |
fileFromURL(java.net.URL url)
Tries to convert the specified URL to a file object.
|
(package private) static java.lang.String |
getBasePath(java.net.URL url)
Return the path without the file name, for example http://xyz.net/foo/bar.xml
results in http://xyz.net/foo/
|
static java.io.File |
getFile(java.lang.String basePath,
java.lang.String fileName)
Tries to convert the specified base path and file name into a file object.
|
(package private) static java.lang.String |
getFileName(java.net.URL url)
Extract the file name from the specified URL.
|
static java.net.URL |
getURL(java.lang.String basePath,
java.lang.String file)
Constructs a URL from a base path and a file name.
|
static java.net.URL |
locate(java.lang.String name)
Return the location of the specified resource by searching the user home
directory, the current classpath and the system classpath.
|
static java.net.URL |
locate(java.lang.String base,
java.lang.String name)
Return the location of the specified resource by searching the user home
directory, the current classpath and the system classpath.
|
(package private) static java.net.URL |
locateFromClasspath(java.lang.String resourceName)
Tries to find a resource with the given name in the classpath.
|
static java.lang.String |
toString(Configuration configuration)
Get a string representation of the key/value mappings of a
configuration.
|
static final java.lang.String PROTOCOL_FILE
static final java.lang.String RESOURCE_PATH_SEPARATOR
private static final java.lang.String METHOD_CLONE
private static org.apache.commons.logging.Log log
private ConfigurationUtils()
public static void dump(Configuration configuration, java.io.PrintStream out)
configuration
- the configurationout
- the output stream to dump the configuration topublic static void dump(Configuration configuration, java.io.PrintWriter out)
configuration
- the configurationout
- the writer to dump the configuration topublic static java.lang.String toString(Configuration configuration)
configuration
- the configurationpublic static void copy(Configuration source, Configuration target)
clone()
methods
should be used.source
- the source configurationtarget
- the target configurationpublic static void append(Configuration source, Configuration target)
source
- the source configurationtarget
- the target configurationpublic static HierarchicalConfiguration convertToHierarchical(Configuration conf)
conf
- the configuration to convertpublic static Configuration cloneConfiguration(Configuration config) throws ConfigurationRuntimeException
Cloneable
interface, its clone()
method will be invoked. Otherwise
an exception will be thrown.config
- the configuration object to be cloned (can be null)ConfigurationRuntimeException
- if cloning is not supported for
this objectstatic java.lang.Object clone(java.lang.Object obj) throws java.lang.CloneNotSupportedException
Cloneable
interface. If
this is the case, the clone()
method is invoked by
reflection. Errors that occur during the cloning process are re-thrown as
runtime exceptions.obj
- the object to be clonedjava.lang.CloneNotSupportedException
- if the object cannot be clonedpublic static java.net.URL getURL(java.lang.String basePath, java.lang.String file) throws java.net.MalformedURLException
basePath
- the base path URL (can be null)file
- the file namejava.net.MalformedURLException
- if URLs are invalidstatic java.io.File constructFile(java.lang.String basePath, java.lang.String fileName)
getURL()
does not seem to be a valid URL.basePath
- the base pathfileName
- the file namepublic static java.net.URL locate(java.lang.String name)
name
- the name of the resourcepublic static java.net.URL locate(java.lang.String base, java.lang.String name)
base
- the base path of the resourcename
- the name of the resourcestatic java.net.URL locateFromClasspath(java.lang.String resourceName)
resourceName
- the name of the resourcestatic java.lang.String getBasePath(java.net.URL url)
url
- the URL from which to extract the pathstatic java.lang.String getFileName(java.net.URL url)
url
- the URL from which to extract the file namepublic static java.io.File getFile(java.lang.String basePath, java.lang.String fileName)
java.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.
basePath
- the base pathfileName
- the file namepublic static java.io.File fileFromURL(java.net.URL url)
url
- the URLpublic static void enableRuntimeExceptions(Configuration src)
DatabaseConfiguration
or
JNDIConfiguration
. Per default such errors are simply
logged and then ignored. This implementation will register a special
ConfigurationErrorListener
that throws a runtime
exception (namely a ConfigurationRuntimeException
) on
each received error event.src
- the configuration, for which runtime exceptions are to be
enabled; this configuration must be derived from
EventSource