Package com.illumon.util
Class ConfigurationUtils
java.lang.Object
com.illumon.util.ConfigurationUtils
public class ConfigurationUtils extends Object
Class to provide functionality that expands on
Configuration
.-
Constructor Summary
Constructors Constructor Description ConfigurationUtils()
-
Method Summary
Modifier and Type Method Description static String
findPropertyName(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return the first property name found.static String
findRequiredPropertyValue(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return the first value found.static String
getBinaryLogDir(com.fishlib.configuration.Configuration configuration, boolean createDirectory)
Get the default directory for binary logs.static String
getBinaryLogDir(com.fishlib.configuration.Configuration configuration, String namespace, String tableName, boolean createDirectory)
Get the directory for binary logs for a given namespace and table.static String
getBinaryLogDirWithoutValidation(com.fishlib.configuration.Configuration configuration)
Get the default directory for binary logs.static String
getBinaryLogFile(com.fishlib.configuration.Configuration configuration, boolean createDirectory, String filename)
Get the directory for binary logs, concatenated with the provided log file name.static boolean
getBooleanWithDefault(com.fishlib.configuration.Configuration configuration, String processName, String propertySuffix, Class<?> className, boolean defaultValue)
Look for a cascading property value returning the most specified value found as a boolean.static String
getDirectoryString(com.fishlib.configuration.Configuration configuration, String property)
Retrieve a property that represents a directory and perform token substitution as inConfiguration.normalizeLogDirectoryPath(java.lang.String)
.static <T extends Enum<T>>
TgetEnum(com.fishlib.configuration.Configuration config, Class<T> clazz, String property)
Equivalent toEnum.valueOf(clazz, config.getProperty(property))
static <T extends Enum<T>>
EnumSet<T>getEnumSet(com.fishlib.configuration.Configuration config, Class<T> clazz, String property)
Creates theEnumSet
based on the string array for the given property.static Optional<File>
getFirstExistingFile(com.fishlib.configuration.Configuration configuration, String property)
Given a configuration and property splits along , and finds the first File that exists.static String
getGradleVersion()
Gets the Iris Gradle Version.static int
getIntWithDefault(com.fishlib.configuration.Configuration configuration, String processName, String propertySuffix, Class<?> className, int defaultValue)
Look for a cascading property value returning the most specified value found as an int.static long
getLongWithDefault(com.fishlib.configuration.Configuration configuration, String processName, String propertySuffix, Class<?> className, long defaultValue)
Look for a cascading property value returning the most specified value found as a long.static Optional<Map<String,String>>
getMapFromProperty(com.fishlib.configuration.Configuration configuration, String property)
Given a configuration and property splits along , for key=value pairs and turns it into a Map.static int
getPortProperty(com.fishlib.configuration.Configuration configuration, String propertyName)
Get a property and validate that the value is valid as a port (between 0 and 65535).static boolean
getPossibleBooleanWithDefault(com.fishlib.configuration.Configuration configuration, boolean defaultValue, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return its value if it is found.static int
getPossibleIntegerWithDefault(com.fishlib.configuration.Configuration configuration, int defaultValue, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return its value if it is found.static long
getPossibleLongWithDefault(com.fishlib.configuration.Configuration configuration, long defaultValue, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return its value if it is found.static String
getPossibleStringWithDefault(com.fishlib.configuration.Configuration configuration, String defaultValue, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return its value if it is found.static boolean
getRequiredBoolean(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return the first value found.static int
getRequiredInteger(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)
Look for a property name matching one of the passed-in names, and return the first value found.static List<String>
getStringPropertyValues(com.fishlib.configuration.Configuration configuration, String propertyPrefix)
Return a List all values for all properties matching the specified prefix.static String
getVcsVersion()
Gets the VCS Versionstatic String
getVersionString(String resourceName)
Gets a version String.static long
parseByteQuantity(String quantityString)
Parse a quantity in the same formats supported by JVM args, e.g.static void
registerStaticConfigMBean(AuditEventLoggerBasic audit)
RegistersConfiguration.getInstance()
as an MBean - seeConfigurationDynamicMBean
, if the value of the propertyJMX_REGISTER_CONFIG
is true.
-
Constructor Details
-
ConfigurationUtils
public ConfigurationUtils()
-
-
Method Details
-
getMapFromProperty
public static Optional<Map<String,String>> getMapFromProperty(com.fishlib.configuration.Configuration configuration, String property)Given a configuration and property splits along , for key=value pairs and turns it into a Map. If the property is not specified, then an empty optional is returned.- Parameters:
configuration
- configuration option to retrieve the property from.property
- the property name to fetch- Returns:
- an Optional with the map derived from the property.
-
getFirstExistingFile
public static Optional<File> getFirstExistingFile(com.fishlib.configuration.Configuration configuration, String property)Given a configuration and property splits along , and finds the first File that exists.- Parameters:
configuration
- configuration option to retrieve the property from.property
- the property name to fetch- Returns:
- an Optional with the File derived from the property.
-
getPossibleLongWithDefault
public static long getPossibleLongWithDefault(com.fishlib.configuration.Configuration configuration, long defaultValue, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return its value if it is found. If values are found for multiple of the specified names, throw an exception. If a matching property is found it will be parsed into a long.- Parameters:
configuration
- the Configuration instancedefaultValue
- default value to be returned if none of the properties are foundpossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's value, or defaultValue if none of the properties are found
-
getPossibleStringWithDefault
public static String getPossibleStringWithDefault(com.fishlib.configuration.Configuration configuration, String defaultValue, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return its value if it is found. If values are found for multiple of the specified names, throw an exception.- Parameters:
configuration
- the Configuration instancedefaultValue
- default value to be returned if none of the properties are foundpossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's value, or defaultValue if none of the properties are found
-
getRequiredInteger
public static int getRequiredInteger(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return the first value found. If no values are found, throw an exception.. If a matching property is found it will be parsed into an int.- Parameters:
configuration
- the Configuration instancepossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's value, or defaultValue if none of the properties are found
-
getPossibleIntegerWithDefault
public static int getPossibleIntegerWithDefault(com.fishlib.configuration.Configuration configuration, int defaultValue, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return its value if it is found. If values are found for multiple of the specified names, throw an exception. If a matching property is found it will be parsed into an int.- Parameters:
configuration
- the Configuration instancedefaultValue
- default value to be returned if none of the properties are foundpossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's value, or defaultValue if none of the properties are found
-
getRequiredBoolean
public static boolean getRequiredBoolean(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return the first value found. If no values are found, throw an exception.. If a matching property is found it will be parsed into a boolean.- Parameters:
configuration
- the Configuration instancepossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's value, or defaultValue if none of the properties are found
-
getPossibleBooleanWithDefault
public static boolean getPossibleBooleanWithDefault(com.fishlib.configuration.Configuration configuration, boolean defaultValue, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return its value if it is found. If values are found for multiple of the specified names, throw an exception. If a matching property is found it will be parsed into an boolean.- Parameters:
configuration
- the Configuration instancedefaultValue
- default value to be returned if none of the properties are foundpossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's value, or defaultValue if none of the properties are found
-
findRequiredPropertyValue
public static String findRequiredPropertyValue(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return the first value found. If no values are found, throw an exception.- Parameters:
configuration
- the Configuration instancepossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's value
- Throws:
IllegalArgumentException
- if no value is found for any of the specified property names
-
findPropertyName
public static String findPropertyName(com.fishlib.configuration.Configuration configuration, String... possiblePropertyNames)Look for a property name matching one of the passed-in names, and return the first property name found. If no values are found, return null.- Parameters:
configuration
- the Configuration instancepossiblePropertyNames
- the possible property names, in order of preference (a property name later in the properties is deprecated by one earlier)- Returns:
- the found property's name
-
getDirectoryString
public static String getDirectoryString(com.fishlib.configuration.Configuration configuration, String property)Retrieve a property that represents a directory and perform token substitution as inConfiguration.normalizeLogDirectoryPath(java.lang.String)
.- Parameters:
configuration
- configuration option to retrieve the property from.property
- the property name to fetch- Returns:
- the retrieved property, with the substitutions performed
-
getPortProperty
public static int getPortProperty(com.fishlib.configuration.Configuration configuration, String propertyName)Get a property and validate that the value is valid as a port (between 0 and 65535).- Parameters:
configuration
- the Configuration to usepropertyName
- the property name- Returns:
- a valid port, or throws a runtime exception
-
getStringPropertyValues
public static List<String> getStringPropertyValues(com.fishlib.configuration.Configuration configuration, String propertyPrefix)Return a List all values for all properties matching the specified prefix. Properties that map to empty values are not returned in the list. The values in the list will be sorted by property name.- Parameters:
configuration
- the Configuration instancepropertyPrefix
- the property prefix- Returns:
- the List of matching string values, or an empty list if none is found
-
getBooleanWithDefault
public static boolean getBooleanWithDefault(com.fishlib.configuration.Configuration configuration, String processName, String propertySuffix, Class<?> className, boolean defaultValue)Look for a cascading property value returning the most specified value found as a boolean. Properties are checked in this order of preference:- {className}.{processName}{propertySuffix}
- {processName}{propertySuffix}
- {className}{propertySuffix}
- defaultValue
- Parameters:
configuration
- the Configuration to useprocessName
- the process namepropertySuffix
- the property suffix (begin with a . or other separator if desired)className
- the class name (className.getSimpleName() will be used)defaultValue
- the default value to be used if no matching properties are defined- Returns:
- the determined value
-
getIntWithDefault
public static int getIntWithDefault(com.fishlib.configuration.Configuration configuration, String processName, String propertySuffix, Class<?> className, int defaultValue)Look for a cascading property value returning the most specified value found as an int. Properties are checked in this order of preference:- {className}.{processName}{propertySuffix}
- {processName}{propertySuffix}
- {className}{propertySuffix}
- defaultValue
- Parameters:
configuration
- the Configuration to useprocessName
- the process namepropertySuffix
- the property suffix (begin with a . or other separator if desired)className
- the class name (className.getSimpleName() will be used)defaultValue
- the default value to be used if no matching properties are defined- Returns:
- the determined value
-
getLongWithDefault
public static long getLongWithDefault(com.fishlib.configuration.Configuration configuration, String processName, String propertySuffix, Class<?> className, long defaultValue)Look for a cascading property value returning the most specified value found as a long. Properties are checked in this order of preference:- {className}.{processName}{propertySuffix}
- {processName}{propertySuffix}
- {className}{propertySuffix}
- defaultValue
- Parameters:
configuration
- the Configuration to useprocessName
- the process namepropertySuffix
- the property suffix (begin with a . or other separator if desired)className
- the class name (className.getSimpleName() will be used)defaultValue
- the default value to be used if no matching properties are defined- Returns:
- the determined value
-
getBinaryLogDir
public static String getBinaryLogDir(@NotNull com.fishlib.configuration.Configuration configuration, @NotNull String namespace, @NotNull String tableName, boolean createDirectory) throws IOExceptionGet the directory for binary logs for a given namespace and table. Create it if it doesn't exist and createDirectory is true. Look for properties in the following order:BINARY_LOG_DIR_PROPERTY
.namespace.tableNameBINARY_LOG_DIR_PROPERTY
.namespaceBINARY_LOG_DIR_PROPERTY
Configuration.getLogDir()
, otherwise perform substitution and normalization withConfiguration.normalizeLogDirectoryPath(java.lang.String)
.- Parameters:
configuration
- the Configuration to usenamespace
- the namespace for the binary logtableName
- the table name for the binary logcreateDirectory
- if true, create the directory if it doesn't exist- Returns:
- the directory name
- Throws:
IOException
- if the path exists and is not a directory, if the path doesn't exist and createDirectory is false, or if the path doesn't exist and can't be created
-
getBinaryLogFile
public static String getBinaryLogFile(com.fishlib.configuration.Configuration configuration, boolean createDirectory, String filename) throws IOExceptionGet the directory for binary logs, concatenated with the provided log file name. Create it if it doesn't exist and createDirectory is true. Use the propertyBINARY_LOG_DIR_PROPERTY
to retrieve the directory name, and if this property doesn't exist useConfiguration.getLogDir()
. Perform substitution and normalization withConfiguration.normalizeLogDirectoryPath(java.lang.String)
.- Parameters:
configuration
- the Configuration to usecreateDirectory
- if true, create the directory if it doesn't existfilename
- the name of the binary log file- Returns:
- the binary log directory name concatenated with the filename
- Throws:
IOException
- if the path exists and is not a directory, if the path doesn't exist and createDirectory is false, or if the path doesn't exist and can't be created
-
getBinaryLogDir
public static String getBinaryLogDir(com.fishlib.configuration.Configuration configuration, boolean createDirectory) throws IOExceptionGet the default directory for binary logs. Create it if it doesn't exist and createDirectory is true. Use the propertyBINARY_LOG_DIR_PROPERTY
to retrieve the directory name, and if this property doesn't exist useConfiguration.getLogDir()
. Perform substitution and normalization withConfiguration.normalizeLogDirectoryPath(java.lang.String)
.- Parameters:
configuration
- the Configuration to usecreateDirectory
- if true, create the directory if it doesn't exist- Returns:
- the directory name
- Throws:
IOException
- if the path exists and is not a directory, if the path doesn't exist and createDirectory is false, or if the path doesn't exist and can't be created
-
getBinaryLogDirWithoutValidation
public static String getBinaryLogDirWithoutValidation(com.fishlib.configuration.Configuration configuration) throws IOExceptionGet the default directory for binary logs. Do not validate that it exists or create it. Use the propertyBINARY_LOG_DIR_PROPERTY
to retrieve the directory name, and if this property doesn't exist useConfiguration.getLogDir()
. Perform substitution and normalization withConfiguration.normalizeLogDirectoryPath(java.lang.String)
.- Parameters:
configuration
- the Configuration to use- Returns:
- the directory name
- Throws:
IOException
- from the directory calls
-
getEnum
public static <T extends Enum<T>> T getEnum(com.fishlib.configuration.Configuration config, Class<T> clazz, String property)Equivalent toEnum.valueOf(clazz, config.getProperty(property))
- Throws:
IllegalArgumentException
- if the value is invalid according toEnum.valueOf(Class, String)
-
getEnumSet
public static <T extends Enum<T>> EnumSet<T> getEnumSet(com.fishlib.configuration.Configuration config, Class<T> clazz, String property)Creates theEnumSet
based on the string array for the given property. If the given property doesn't exist, the returned set will be empty. Each item in the array is converted into the enum via the equivalent ofEnum.valueOf(clazz, value)
.- Throws:
IllegalArgumentException
- if the value is invalid according toEnum.valueOf(Class, String)
-
registerStaticConfigMBean
RegistersConfiguration.getInstance()
as an MBean - seeConfigurationDynamicMBean
, if the value of the propertyJMX_REGISTER_CONFIG
is true. -
parseByteQuantity
Parse a quantity in the same formats supported by JVM args, e.g. -Xmx (Format: -Xmx<size>[g|G|m|M|k|K]).- Parameters:
quantityString
- The quantity in String form- Returns:
- The result byte quantity
-
getVersionString
Gets a version String.- Parameters:
resourceName
- the resource name- Returns:
- the resource string or undetermined if it cannot be found.
-
getGradleVersion
Gets the Iris Gradle Version.- Returns:
- the Iris Gradle Version
-
getVcsVersion
Gets the VCS Version- Returns:
- the VCS Version
-