public final class ConfigurationUtils extends Object
Utility methods to convert configurations.
Modifier and Type | Method and Description |
---|---|
static 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 |
convertToHierarchical(Configuration conf)
Converts the passed in configuration to a hierarchical one.
|
static HierarchicalConfiguration |
convertToHierarchical(Configuration conf,
ExpressionEngine engine)
Converts the passed in
Configuration object to a
hierarchical one using the specified ExpressionEngine . |
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 |
enableRuntimeExceptions(Configuration src)
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 |
getFile(String basePath,
String fileName)
Tries to convert the specified base path and file name into a file object.
|
static URL |
getURL(String basePath,
String file)
Constructs a URL from a base path and a file name.
|
static URL |
locate(String name)
Return the location of the specified resource by searching the user home
directory, the current classpath and the system classpath.
|
static URL |
locate(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.
|
public static void dump(Configuration configuration, PrintStream out)
configuration
- the configurationout
- the output stream to dump the configuration topublic static void dump(Configuration configuration, PrintWriter out)
configuration
- the configurationout
- the writer to dump the configuration topublic static String toString(Configuration configuration)
configuration
- the configurationpublic static void copy(Configuration source, Configuration target)
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 the
copy()
method of AbstractConfiguration
should
be used. In a future release this method might become deprecated.
source
- the source configurationtarget
- the target configurationpublic static void append(Configuration source, Configuration target)
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 the
copy()
method of AbstractConfiguration
should
be used. In a future release this method might become deprecated.
source
- the source configurationtarget
- the target configurationpublic static HierarchicalConfiguration convertToHierarchical(Configuration conf)
conf
- the configuration to convertpublic static HierarchicalConfiguration convertToHierarchical(Configuration conf, ExpressionEngine engine)
Configuration
object to a
hierarchical one using the specified ExpressionEngine
. 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 its
ExpressionEngine
. 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 the
ExpressionEngine
; then the default expression engine is
used. If the passed in configuration is already hierarchical, it is
directly returned. (However, the ExpressionEngine
is set if
it is not null.) Otherwise all properties are copied into a new
hierarchical configuration.conf
- the configuration to convertengine
- the ExpressionEngine
for the hierarchical
configuration or null for the defaultpublic 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 objectpublic static URL getURL(String basePath, String file) throws MalformedURLException
basePath
- the base path URL (can be null)file
- the file nameMalformedURLException
- if URLs are invalidpublic static URL locate(String name)
name
- the name of the resourcepublic static URL locate(String base, String name)
base
- the base path of the resourcename
- the name of the resourcepublic static File getFile(String basePath, 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 File fileFromURL(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
Copyright © 2018 JBoss by Red Hat. All rights reserved.