public class ConfigUtils extends Object
| Constructor and Description |
|---|
ConfigUtils() |
| Modifier and Type | Method and Description |
|---|---|
static IllegalArgumentException |
convertConfigException(ConfigException e,
String basePath)
Creates a
IllegalArgumentException from a ConfigException, sanitizing the error
message for better user experience. |
static Config |
createApplicationConfig(Path appConfigPath)
Creates an unresolved application config for DSBulk, optionally pulling application settings
from the given alternate location.
|
static Config |
createReferenceConfig()
Creates an unresolved reference config for DSBulk.
|
static char |
getChar(Config config,
String path)
Returns the character at the given path.
|
static Charset |
getCharset(Config config,
String path)
Returns the
Charset at the given path. |
static String |
getComments(ConfigValue value)
Returns the comments associated with the given value, excluding type hints.
|
static ConfigValue |
getNullSafeValue(Config config,
String path)
An alternative to
Config.getValue(String) that handles null values gracefully instead
of throwing. |
static Path |
getPath(Config config,
String path)
Returns the
Path object at the given path. |
static int |
getThreads(Config config,
String path)
Returns the number of threads at the given path.
|
static Optional<String> |
getTypeHint(ConfigValue value)
Retrieves the type hint for the given value, if any.
|
static Optional<String> |
getTypeString(Config config,
String path)
Returns a string representation of the value type at this path.
|
static URL |
getURL(Config config,
String path)
Returns the
URL object at the given path. |
static List<URL> |
getURLsFromFile(Path urlfile)
Loads list of URLs from a file given as the urlfile argument using encoding.
|
static ConfigValueType |
getValueType(Config config,
String path)
Alternative to
ConfigValue.valueType() that honors any type hints found in the
configuration, if any. |
static boolean |
hasReferenceValue(Config config,
String path)
Checks whether the given path has a default value or not.
|
static boolean |
hasUserOverride(Config config,
String path)
Checks whether the given path has a user-defined value or not.
|
static boolean |
isLeaf(ConfigValue value)
Returns true if the given value is a leaf.
|
static boolean |
isLeaf(String line)
Checks the given line for the presence of an @leaf annotation.
|
static boolean |
isPathPresentAndNotEmpty(Config config,
String path)
Checks if the given path is present and its value is a non-empty string.
|
static boolean |
isTypeHint(String line)
Checks the given line for the presence of an @type annotation.
|
static Config |
readPassword(Config config,
String path,
Console console)
Reads a password from the console, then returns a new
Config object containing the
password read under the given path. |
static Path |
resolvePath(String path)
Resolves the given path.
|
static int |
resolveThreads(String threadsStr)
Resolves the given input as a positive integer representing the number of threads to allocate.
|
static URL |
resolveURL(String url)
Resolves the given URL.
|
static Optional<Path> |
resolveUserHome(String path)
Resolves a path starting with "~" against the current user's home directory.
|
static Config |
standaloneDriverReference()
Creates a resolved config containing only the driver settings that DSBulk overrides.
|
static Config |
standaloneDSBulkReference()
Creates a resolved config containing only DSBulk settings.
|
public static Config standaloneDSBulkReference()
The reference config is obtained from all classpath resources named dsbulk-reference.conf.
This method is only useful for documentation purposes.
public static Config standaloneDriverReference()
The reference config is obtained from all classpath resources named driver-reference.conf.
This method is only useful for documentation purposes.
@NonNull public static Config createReferenceConfig()
The reference config is obtained from the following stack:
@NonNull public static Config createApplicationConfig(@Nullable Path appConfigPath)
The application config is obtained from the following stack:
appConfigPath
if non null: application settings (DSBulk and driver overrides).
public static Path getPath(Config config, String path)
Path object at the given path.
The returned Path is normalized and absolute.
For convenience, if the path begins with a tilde (`~`), that symbol will be expanded to the current user's home directory, as supplied by `System.getProperty("user.home")`. Note that this expansion will not occur when the tilde is not the first character in the path, nor when the home directory owner is not the current user.
config - The Config to use.path - path expression.ConfigException.Missing - if value is absent or null.ConfigException.WrongType - if value is not convertible to a Path.public static URL getURL(Config config, String path)
URL object at the given path.
The value will be first interpreted directly as a URL; if the parsing fails, the value will be then interpreted as a path on the local filesystem, then converted to a file URL.
If the value is "-" map it to "std:/", to indicate this url represents stdout (when unloading) and stdin (when loading).
The returned URL is normalized and absolute.
config - The Config to use.path - path expression.ConfigException.Missing - if value is absent or null.ConfigException.WrongType - if value is not convertible to a URL.public static int getThreads(Config config, String path)
The given path can be an integer, or alternatively, an integer followed by the letter C, in which case, the resulting value is the integer multiplied by the number of available cores on the system.
config - The Config to use.path - path expression.ConfigException.Missing - if value is absent or null.ConfigException.WrongType - if value is not convertible to a number of threads.public static char getChar(Config config, String path)
config - The Config to use.path - path expression.ConfigException.Missing - if value is absent or null.ConfigException.WrongType - if value is not convertible to a single character.public static Charset getCharset(Config config, String path)
Charset at the given path.config - The Config to use.path - path expression.ConfigException.Missing - if value is absent or null.ConfigException.WrongType - if value is not convertible to a Charset.@NonNull public static Path resolvePath(@NonNull String path) throws InvalidPathException
The returned path is normalized and absolute. If the input denotes a relative path, it is resolved against the current working directory. If it starts with a tilde, the tilde is expanded into the current user's home directory.
path - The path to resolve.Path, absolute and normalized.InvalidPathException - If the path cannot be resolved.@NonNull public static URL resolveURL(@NonNull String url) throws MalformedURLException, InvalidPathException
The returned URL is normalized.
This method first tries to interpret the input as a valid URL, possibly expanding the
special "-" (single dash) URL into DSBulk's standard input/output URL.
If that fails, this method then attempts to interpret the input as a path object. See resolvePath(String).
url - The URL to resolve.URL, normalized.MalformedURLException - If the URL cannot be resolved.InvalidPathException - If the path cannot be resolved.@NonNull public static Optional<Path> resolveUserHome(@NonNull String path)
Resolving against another user's home directory is not supported and throws InvalidPathException.
path - The path to resolve.InvalidPathException - if the path string cannot be converted to a Path, or if the path
references another user's home directory.public static int resolveThreads(@NonNull
String threadsStr)
This method first tries to parse the input directly as an integer.
If that fails, it then tries to parse the input as an integer followed by the letter 'C'. If
that succeeds, the total number of threads returned is n * .number of available cores
threadsStr - The string to parse.PatternSyntaxException - If the input cannot be parsed.IllegalArgumentException - If the input can be parsed, but the resulting integer is not
positive.@NonNull public static Optional<String> getTypeString(@NonNull Config config, @NonNull String path)
config - the config.path - path expression.ConfigException.Missing - if value is absent.@NonNull public static ConfigValueType getValueType(@NonNull Config config, @NonNull String path)
ConfigValue.valueType() that honors any type hints found in the
configuration, if any.config - the config.path - path expression.value type.ConfigException.Missing - if value is absent.@NonNull public static Optional<String> getTypeHint(@NonNull ConfigValue value)
value - the value to inspect.@NonNull public static String getComments(@NonNull ConfigValue value)
value - the value to inspect.public static boolean isTypeHint(@NonNull
String line)
line - The line to inspect.public static boolean isLeaf(@NonNull
ConfigValue value)
Leaf values are values of all types except OBJECT, or values of type OBJECT explicitly annotated with @leaf.
value - The value to inspect.public static boolean isLeaf(@NonNull
String line)
line - The line to inspect.@NonNull public static ConfigValue getNullSafeValue(@NonNull Config config, @NonNull String path)
Config.getValue(String) that handles null values gracefully instead
of throwing.
Note that the path must still exist; if the path does not exist (i.e., the value is
completely absent from the config object), this method still throws ConfigException.Missing.
config - The config object to get the value from.path - The path at which the value is to be found.value.ConfigException.Missing - If the path is not present in the config object.public static List<URL> getURLsFromFile(Path urlfile) throws IOException
urlfile - The path to file passed as the --urlfile argument to dsbulk.IOException - If unable to load a file from urlfile path.public static boolean isPathPresentAndNotEmpty(Config config, String path)
config - The config.path - The path expression.true if the given path is present and its value is a non-empty string, false otherwise.public static boolean hasReferenceValue(Config config, String path)
A default value is the value defined in reference.conf or dsbulk-reference.conf.
If the given path doesn't exist at all this method returns false.
config - The config.path - The path expression.true if the given path has a default value, false otherwise.public static boolean hasUserOverride(Config config, String path)
A user-defined value is a value defined in application.conf.
If the given path doesn't exist at all this method returns false.
config - The config.path - The path expression.true if the given path has a user-defined value, false otherwise.@NonNull public static IllegalArgumentException convertConfigException(@NonNull ConfigException e, @NonNull String basePath)
IllegalArgumentException from a ConfigException, sanitizing the error
message for better user experience.
The origin description, usually placed at the beginning of the error message, is placed rather at the end, enclosed in parentheses.
e - The original exceptionbasePath - The base path of the configuration; required to create an absolute path from an
error message containing relative paths.IllegalArgumentException with sanitized error message.Copyright © 2017–2021 DataStax. All rights reserved.