public class ConfigUtils extends Object
Config objects.| Modifier and Type | Field and Description |
|---|---|
static String |
STRIP_SUFFIX
A suffix that is automatically appended to property keys that are prefixes of other
property keys.
|
| Constructor and Description |
|---|
ConfigUtils(FileUtils fileUtils) |
| Modifier and Type | Method and Description |
|---|---|
static Properties |
configToProperties(com.typesafe.config.Config config)
Convert a given
Config instance to a Properties instance. |
static Properties |
configToProperties(com.typesafe.config.Config config,
com.google.common.base.Optional<String> prefix)
Convert a given
Config instance to a Properties instance. |
static Properties |
configToProperties(com.typesafe.config.Config config,
String prefix)
Convert a given
Config instance to a Properties instance. |
static State |
configToState(com.typesafe.config.Config config)
Convert a given
Config to a State instance. |
static String |
desanitizeKey(String propKey) |
static String |
emptyIfNotPresent(com.typesafe.config.Config config,
String path)
Return string value at
path if config has path. |
static Set<String> |
findFullPrefixKeys(Properties properties,
com.google.common.base.Optional<String> keyPrefix)
Finds a list of properties whose keys are complete prefix of other keys.
|
static boolean |
getBoolean(com.typesafe.config.Config config,
String path,
boolean def)
Return boolean value at
path if config has path. |
static com.typesafe.config.Config |
getConfig(com.typesafe.config.Config config,
String path,
com.typesafe.config.Config def)
Return
Config value at path if config has path. |
static com.typesafe.config.Config |
getConfigOrEmpty(com.typesafe.config.Config config,
String key) |
static double |
getDouble(com.typesafe.config.Config config,
String path,
double def)
Return double value at
path if config has path. |
static Integer |
getInt(com.typesafe.config.Config config,
String path,
Integer def)
|
static Long |
getLong(com.typesafe.config.Config config,
String path,
Long def)
|
static String |
getString(com.typesafe.config.Config config,
String path,
String def)
Return string value at
path if config has path. |
static List<String> |
getStringList(com.typesafe.config.Config config,
String path)
An extension to
Config.getStringList(String). |
static TimeUnit |
getTimeUnit(com.typesafe.config.Config config,
String path,
TimeUnit def)
Return TimeUnit value at
path if config has path. |
static boolean |
hasNonEmptyPath(com.typesafe.config.Config config,
String key)
Check if the given
key exists in config and it is not null or empty
Uses StringUtils.isNotBlank(CharSequence) |
static com.typesafe.config.Config |
propertiesToConfig(Properties properties)
Convert a given
Properties to a Config instance. |
static com.typesafe.config.Config |
propertiesToConfig(Properties properties,
com.google.common.base.Optional<String> prefix)
|
static com.typesafe.config.Config |
propertiesToTypedConfig(Properties properties,
com.google.common.base.Optional<String> prefix)
|
static com.typesafe.config.Config |
resolveEncrypted(com.typesafe.config.Config config,
com.google.common.base.Optional<String> encConfigPath)
Resolves encrypted config value(s) by considering on the path with "encConfigPath" as encrypted.
|
static boolean |
sanitizedKey(String propKey)
returns true if is it a sanitized key
|
static String |
sanitizeFullPrefixKey(String propKey) |
void |
saveConfigToFile(com.typesafe.config.Config config,
Path destPath) |
static boolean |
verifySubset(com.typesafe.config.Config superConfig,
com.typesafe.config.Config subConfig)
Check that every key-value in superConfig is in subConfig
|
public static final String STRIP_SUFFIX
public ConfigUtils(FileUtils fileUtils)
public void saveConfigToFile(com.typesafe.config.Config config,
Path destPath)
throws IOException
IOExceptionpublic static Properties configToProperties(com.typesafe.config.Config config)
Config instance to a Properties instance.config - the given Config instanceProperties instancepublic static Properties configToProperties(com.typesafe.config.Config config, com.google.common.base.Optional<String> prefix)
Config instance to a Properties instance.
If the config value is not of String type, it will try to get it as a generic Object type
using com.typesafe.config.Config#getAnyRef() and then try to return its json representation as a stringconfig - the given Config instanceprefix - an optional prefix; if present, only properties whose name starts with the prefix
will be returned.Properties instancepublic static Properties configToProperties(com.typesafe.config.Config config, String prefix)
Config instance to a Properties instance.config - the given Config instanceprefix - only properties whose name starts with the prefix will be returned.Properties instancepublic static com.typesafe.config.Config getConfigOrEmpty(com.typesafe.config.Config config,
String key)
public static State configToState(com.typesafe.config.Config config)
Config to a State instance.config - the given Config instanceState instancepublic static com.typesafe.config.Config propertiesToConfig(Properties properties)
Properties to a Config instance.
This method will throw an exception if (1) the Object.toString() method of any two keys in the
Properties objects returns the same String, or (2) if any two keys are prefixes of one another,
see the Java Docs of ConfigFactory.parseMap(Map) for more details.
properties - the given Properties instanceConfig instancepublic static Set<String> findFullPrefixKeys(Properties properties, com.google.common.base.Optional<String> keyPrefix)
properties - the Properties collection to inspectkeyPrefix - an optional key prefix which limits which properties are inspected.public static com.typesafe.config.Config propertiesToConfig(Properties properties, com.google.common.base.Optional<String> prefix)
prefix in Properties to a Config instance.
This method will throw an exception if (1) the Object.toString() method of any two keys in the
Properties objects returns the same String, or (2) if any two keys are prefixes of one another,
see the Java Docs of ConfigFactory.parseMap(Map) for more details.
properties - the given Properties instanceprefix - of keys to be convertedConfig instancepublic static boolean sanitizedKey(String propKey)
public static com.typesafe.config.Config propertiesToTypedConfig(Properties properties, com.google.common.base.Optional<String> prefix)
prefix in Properties to a
Config instance. The method also tries to guess the types of properties.
This method will throw an exception if (1) the Object.toString() method of any two keys in the
Properties objects returns the same String, or (2) if any two keys are prefixes of one another,
see the Java Docs of ConfigFactory.parseMap(Map) for more details.
properties - the given Properties instanceprefix - of keys to be convertedConfig instancepublic static String emptyIfNotPresent(com.typesafe.config.Config config, String path)
path if config has path. If not return an empty stringconfig - in which the path may be presentpath - key to look for in the config objectpath if config has path. If not return an empty stringpublic static String getString(com.typesafe.config.Config config, String path, String def)
path if config has path. If not return def
If the config value is not of String type, it will try to get it as a generic Object type
using com.typesafe.config.Config#getAnyRef() and then try to return its json representation as a stringconfig - in which the path may be presentpath - key to look for in the config objectpath if config has path. If not return defpublic static TimeUnit getTimeUnit(com.typesafe.config.Config config, String path, TimeUnit def)
path if config has path. If not return defconfig - in which the path may be presentpath - key to look for in the config objectpath if config has path. If not return defpublic static Long getLong(com.typesafe.config.Config config, String path, Long def)
config - in which the path may be presentpath - key to look for in the config objectLong value at path if config has path. If not return defpublic static Integer getInt(com.typesafe.config.Config config, String path, Integer def)
config - in which the path may be presentpath - key to look for in the config objectInteger value at path if config has path. If not return defpublic static boolean getBoolean(com.typesafe.config.Config config,
String path,
boolean def)
path if config has path. If not return defconfig - in which the path may be presentpath - key to look for in the config objectpath if config has path. If not return defpublic static double getDouble(com.typesafe.config.Config config,
String path,
double def)
path if config has path. If not return defconfig - in which the path may be presentpath - key to look for in the config objectpath if config has path. If not return defpublic static com.typesafe.config.Config getConfig(com.typesafe.config.Config config,
String path,
com.typesafe.config.Config def)
Config value at path if config has path. If not return defconfig - in which the path may be presentpath - key to look for in the config objectpath if config has path. If not return defpublic static List<String> getStringList(com.typesafe.config.Config config, String path)
An extension to Config.getStringList(String). The value at path can either be a TypeSafe
ConfigList of strings in which case it delegates to Config.getStringList(String) or as list of
comma separated strings in which case it splits the comma separated list.
path does not exist
config - in which the path may be presentpath - key to look for in the config objectpublic static boolean hasNonEmptyPath(com.typesafe.config.Config config,
String key)
key exists in config and it is not null or empty
Uses StringUtils.isNotBlank(CharSequence)config - which may have the keykey - to look for in the configpublic static boolean verifySubset(com.typesafe.config.Config superConfig,
com.typesafe.config.Config subConfig)
public static com.typesafe.config.Config resolveEncrypted(com.typesafe.config.Config config,
com.google.common.base.Optional<String> encConfigPath)
config - encConfigPath -