Class ConfigurationTools

java.lang.Object
de.aristaflow.adept2.util.ConfigurationTools

public class ConfigurationTools extends Object
This class provides methods for converting values used in Configurations from the corresponding type to strings and back.
  • Constructor Details

    • ConfigurationTools

      public ConfigurationTools()
  • Method Details

    • parseBoolean

      public static Boolean parseBoolean(String booleanValue)
      Parses the designated string and returns the corresponding boolean value. Use toString(boolean) to convert a boolean value to a string.
      Parameters:
      booleanValue - The string representation of a boolean value to be converted.
      Returns:
      The boolean value of the designated string or null in case the string is null.
    • toString

      public static String toString(boolean booleanValue)
      Converts the designated boolean value to a string for usage in configurations when manually editing these. When setting the value via the Configuration-interface directly, the conversion will be handled internally.
      Use parseBoolean(String) to convert a string back to a boolean.
      Parameters:
      booleanValue - The boolean value to be represented as a string.
      Returns:
      The string representation of the designated boolean value.
    • parseInteger

      public static Long parseInteger(String integerValue)
      Parses the designated string and returns the corresponding integer value or null in case the value cannot be parsed or is not a valid integer. Use toString(long) to convert an integer value to a string.
      Parameters:
      integerValue - The string representation of an integer value to be converted.
      Returns:
      The integer value of the designated string or null in case the designated string cannot be parsed to a valid integer.
    • toString

      public static String toString(long integerValue)
      Converts the designated integer value to a string for usage in configurations when manually editing these. When setting the value via the Configuration-interface directly, the conversion will be handled internally.
      Use parseInteger(String) to convert a string back to an integer.
      Parameters:
      integerValue - The integer value to be represented as a string.
      Returns:
      The string representation of the designated integer value.
    • parseFloat

      public static Double parseFloat(String floatValue)
      Parses the designated string and returns the corresponding float value or null in case the value cannot be parsed or is not a valid float. Use toString(double) to convert a float value to a string.
      Parameters:
      floatValue - The string representation of a float value to be converted.
      Returns:
      The float value of the designated string or null in case the designated string cannot be parsed to a valid float.
    • toString

      public static String toString(double floatValue)
      Converts the designated float value to a string for usage in configurations when manually editing these. When setting the value via the Configuration-interface directly, the conversion will be handled internally.
      Use parseFloat(String) to convert a string back to a float.
      Parameters:
      floatValue - The float value to be represented as a string.
      Returns:
      The string representation of the designated float value.
    • parseURI

      public static URI parseURI(String uriValue)
      Parses the designated string and returns the corresponding URI or null in case the value cannot be parsed or is not a valid URI. Use toString(URI) to convert a URI to a string.
      Parameters:
      uriValue - The string representation of the URI to be converted.
      Returns:
      The URI-value of the designated string or null in case the designated string cannot be parsed to a valid URI.
    • toString

      public static String toString(URI uriValue)
      Converts the designated URI to a string for usage in configurations when manually editing these. When setting the value via the Configuration-interface directly, the conversion will be handled internally.
      Use parseURI(String) to convert a string back to a URI.
      Parameters:
      uriValue - The URI to be represented as a string.
      Returns:
      The string representation of the designated URI-value.
      Throws:
      NullArgumentException - If the designated URI is null, a NullArgumentException will be thrown.
    • parseDate

      public static Date parseDate(String dateValue)
      Parses the designated string and returns the corresponding date value or null in case the value cannot be parsed or is not a valid date. Use toString(Date) to convert a date value to a string.

      The following grammar is used for parsing:

       datetime      = date-opt-time | time
       date-opt-time = date-element ['T' [time-element] [offset]]
       date-element  = yyyy ['-' MM ['-' dd]]
       time          = 'T' time-element [offset]
       time-element  = HH [':' mm [':' ss [fraction]]]
       fraction      = ('.' | ',') digit+
       offset        = 'Z' | (('+' | '-') HH [':' mm [':' ss [('.' | ',') SSS]]])
       
      If the string representation does not contain a time zone or zone offset, UTC will be assumed.
      Parameters:
      dateValue - The string representation of a date value to be converted.
      Returns:
      The date value of the designated string or null in case the designated string cannot be parsed to a valid date.
    • getDateTimeFormatter

      protected static DateTimeFormatter getDateTimeFormatter(DateTimeFormatter dateTimeFormatter)
      Gets the designated formatter initialised with default values for the temporal fields.
      Parameters:
      dateTimeFormatter - The date time formatter missing default values for the temporal fields.
      Returns:
      The designated formatter initialised with default values for the temporal fields.
    • getTimeFormatter

      protected static DateTimeFormatter getTimeFormatter(DateTimeFormatter timeElementFormatter)
      Gets the formatter for a time with an optional offset in a string.
      Parameters:
      timeElementFormatter - The formatter for the time element part.
      Returns:
      The formatter for a time with an optional offset in a string.
    • getDateOptTimeFormatter

      protected static DateTimeFormatter getDateOptTimeFormatter(DateTimeFormatter timeElementFormatter)
      Gets the formatter for a date with an optional time and an optional offset in a string.
      Parameters:
      timeElementFormatter - The formatter for the time element part.
      Returns:
      The formatter for a date with an optional time and an optional offset in a string.
    • getTimeElementFormatterDot

      protected static DateTimeFormatter getTimeElementFormatterDot()
      Gets a formatter for a time string using . as decimal point for the fraction of a second.
      Returns:
      A formatter for a time string using . as decimal point for the fraction of a second.
    • getTimeElementFormatterComma

      protected static DateTimeFormatter getTimeElementFormatterComma()
      Gets a formatter for a time string using , as decimal point for the fraction of a second.
      Returns:
      A formatter for a time string using , as decimal point for the fraction of a second.
    • toString

      public static String toString(Date dateValue)
      Converts the designated date to a string for usage in configurations when manually editing these. When setting the value via the Configuration-interface directly, the conversion will be handled internally.
      Use parseDate(String) to convert a string back to a date.
      Parameters:
      dateValue - The date to be represented as a string.
      Returns:
      The string representation of the designated date.
      Throws:
      NullArgumentException - If the designated date is null, a NullArgumentException will be thrown.
    • parseUDT

      public static byte[] parseUDT(String udtValue)
      Parses the designated string and returns the corresponding UDT-value (as byte-array) or null in case the value cannot be parsed or is not a valid byte[]. Use toString(byte[]) to convert a byte-array to a string.
      Parameters:
      udtValue - The string representation of the UDT-value to be converted or null if the designated string is null.
      Returns:
      The UTD-value of the designated string or null in case the designated string cannot be parsed to a valid UDT-value.
    • toString

      public static String toString(byte[] udtValue)
      Converts the designated byte[] to a string for usage in configurations when manually editing these. This encodes the value as basic Base64. It will also be gzipped until there is a problem. In such a case it will be converted without gzip.
      When setting the value via the Configuration-interface directly, the conversion will be handled internally.
      Use parseUDT(String) to convert a string back to an UDT-value.
      Parameters:
      udtValue - The UDT-value to be represented as a string.
      Returns:
      The string representation of the designated UDT-value.
      Throws:
      NullArgumentException - If the designated UDT-value is null, a NullArgumentException will be thrown.
    • toString

      public static String toString(InputStream inputStream) throws IOException
      Converts the designated InputStream to a string for usage in configurations when manually editing these. This encodes the value as basic Base64. It will also be gzipped until there is a problem. In such a case it will be converted without gzip.
      When setting the value via the Configuration-interface directly, the conversion will be handled internally.
      Use parseUDT(String) to convert a string back to an UDT-value.
      Parameters:
      inputStream - The input stream (of which the content is) to be to be represented as a string. The stream will be closed after reading.
      Returns:
      The string representation (of the content) of the designated input stream.
      Throws:
      NullArgumentException - If the designated input stream is null, a NullArgumentException will be thrown.
      IOException - If there are problems creating the stream without gzip, an IOException will be thrown.
    • stringValid

      public static final boolean stringValid(String validString)
      Checks whether the designated string is not null and not the empty string. This method can be used for checking property values of configurations.
      Parameters:
      validString - The string to check.
      Returns:
      validString != null && validString.length() > 0
    • propertyValuePresent

      public static final boolean propertyValuePresent(Configuration configuration, String key)
      Checks whether the value for the designated key in the designated configuration is not null and not the empty string. This method can be used for checking property values of configurations.
      Parameters:
      configuration - The configuration which contain a value for the designated key (or not).
      key - The key for which to check the value of the property.
      Returns:
      stringValid(configuration.getString(key))
    • propertyPresent

      public static final boolean propertyPresent(Configuration configuration, String key)
      Checks whether the value for the designated key in the designated configuration is not null and if it is a string, it is not the empty string. This method can be used for checking property values of arbitrary type provided by configurations.
      Parameters:
      configuration - The configuration which contain a value for the designated key (or not).
      key - The key for which to check the value of the property.
      Returns:
      propertyValuePresent(configuration.getString(key)) or a non-string is != null.
    • checkConfigurationForNulls

      public static final boolean checkConfigurationForNulls(Configuration conf, StringBuilder sb, String[] keys)
      Checks whether the given configuration contains every given key and if every property is set.
      Parameters:
      conf - The configuration that should be checked.
      sb - The string builder where the missing keys are appended, if not null.
      keys - The configuration keys that should be checked.
      Returns:
      Returns true if one of the given keys is missing or the value is not set, false otherwise.
    • parseCSVString

      public static String[] parseCSVString(String csvString, String separator)
      Splits the given CSV string around the designated separator and removes whitespace at the start and end of each resulting token. Returns an empty array if the string is not set or empty.
      Parameters:
      csvString - The string representing the multi-valued string.
      separator - The string which separates the values of the property.
      Returns:
      The separated values of the property list.
    • parseURI

      public static URI parseURI(Configuration configuration, String propertyName)
      Parses the property's value in the given configuration and returns it as a URI. The property is expected to be set and of the correct type.
      Parameters:
      configuration - the configuration containing the property value
      propertyName - the name of property whose value should be parsed
      Returns:
      The property's value as a URI or null in case the property value could not be parsed as URI.
    • getConfigurationDump

      public static String getConfigurationDump(Configuration conf)
      Gets the complete content of the designated configuration as appropriately formatted string. This is useful for debugging.
      Parameters:
      conf - The configuration for which to get the complete content.
      Returns:
      The content of the designated string formatted appropriately.
    • createKeyPair

      public static Triple<BigInteger,BigInteger,BigInteger> createKeyPair(int keySize) throws NoSuchAlgorithmException, InvalidKeySpecException
      Creates a RSA key pair and returns the modulus, the public and the private exponent.
      Parameters:
      keySize - The size of the key to create.
      Returns:
      The modulus, the public and the private exponent of a newly created RSA key pair.
      Throws:
      NoSuchAlgorithmException - If no RSA algorithm can be found, a NoSuchAlgorithmException will be thrown.
      InvalidKeySpecException - If the key spec for the private RSA key cannot be found, an InvalidKeySpecException will be thrown.
    • insertNewGsmKeyPair

      public static boolean insertNewGsmKeyPair(Configuration conf, int keySize, String modConfKey, String pubConfKey, String privConfKey, Logger logger, PrintStream ps)
      Creates a new RSA key pair for the global security manager and stores it in the designated configuration using the appropriate configuration variables (modulus, public and private exponent).
      If the corresponding configuration variables already exist in the designated configuration file, they will be overwritten.
      Parameters:
      conf - The configuration in which to store the key pair attributes.
      keySize - The size of the RSA key.
      modConfKey - The name of the configuration key with which to store the modulus of the key pair.
      pubConfKey - The name of the configuration key with which to store the public exponent of the key pair.
      privConfKey - The name of the configuration key with which to store the private exponent of the key pair.
      logger - The logger to track the progress and problems. This may be null.
      ps - A print stream to track the progress and problems. This may be null.
      Returns:
      Whether creating and inserting the key pair has been successful. If not, check the logger and/or the print stream for details.
    • encryptPassword

      public static String encryptPassword(String password) throws GeneralSecurityException
      Encrypts the designated password string with the public key of the AristaFlow configuration framework and encodes the resulting string as Base64 (non-strict since it does not contain line breaks).
      Parameters:
      password - The password string to encrypt and encode.
      Returns:
      A string representing the encrypted and (Base64-) encoded password.
      Throws:
      GeneralSecurityException - If there are problems encrypting the password, a GeneralSecurityException will be thrown.
    • encryptPassword

      public static boolean encryptPassword(Configuration conf, String key, String password, Logger logger, PrintStream ps)
      Encrypts the designated password and stores it under the designated configuration key name in the designated configuration. If no password is provided, it will be read from the configuration using the designated configuration key name.
      Parameters:
      conf - The configuration in which to store the password and from which to optionally read the password.
      key - The configuration key name under which to store the password and optionally with which to read the password.
      password - The password to be encrypted and stored in the designated configuration. If this is null, the password will be read with the designated configuration key name from the designated configuration.
      logger - The logger to track the progress and problems. This may be null.
      ps - A print stream to track the progress and problems. This may be null.
      Returns:
      Whether encrypting and storing the password has been successful. If not, check the logger and/or the print stream for details.
    • parsePassword

      public static final String parsePassword(Configuration configuration, String propertyName) throws GeneralSecurityException
      Parses the property's value in the designated configuration as a string, decodes and decrypts it and returns the resulting string. This allows to use encrypted passwords in configuration files. The property is expected to be set and of the correct type. Returns an empty string if the password is empty.
      Parameters:
      configuration - The configuration containing the property value
      propertyName - The name of property of which the value should be parsed and decrypted.
      Returns:
      The value of the property as decoded and decrypted String or the empty string in case the password is empty.
      Throws:
      GeneralSecurityException - If there are problems decrypting the property value, a GeneralSecurityException will be thrown.
    • parsePassword

      public static final String parsePassword(String password) throws GeneralSecurityException
      Parses the property's value in the designated configuration as a string, decodes and decrypts it and returns the resulting string. This allows to use encrypted passwords in configuration files. The property is expected to be set and of the correct type. Returns an empty string if the password is empty.
      Parameters:
      password - The encrypted password.
      Returns:
      The value of the property as decoded and decrypted String or the empty string in case the password is empty.
      Throws:
      GeneralSecurityException - If there are problems decrypting the property value, a GeneralSecurityException will be thrown.
    • parsePasswordTolerant

      public static final String parsePasswordTolerant(Configuration configuration, String propertyName)
      Parses the property's value in the designated configuration as a string, decodes and decrypts it and returns the resulting string. If the password cannot be decrypted, it will be assumed to be plain text and it will be returned.
      The property is expected to be set and of the correct type. Returns an empty string if the password is empty.
      Parameters:
      configuration - The configuration containing the property value
      propertyName - The name of property of which the value should be parsed and decrypted or simply returned.
      Returns:
      The value of the property as decoded and decrypted String or the property value directly in case it cannot be decrypted.
    • parsePasswordTolerant

      public static final String parsePasswordTolerant(String password)
      Parses the property's value in the designated configuration as a string, decodes and decrypts it and returns the resulting string. If the password cannot be decrypted, it will be assumed to be plain text and it will be returned.
      This allows to use encrypted passwords in configuration files. The property is expected to be set and of the correct type. Returns an empty string if the password is empty.
      Parameters:
      password - The possibly encrypted password.
      Returns:
      The designated value decoded and decrypted or directly without any changes.
    • getCommaHandler

      public static ListDelimiterHandler getCommaHandler()
      Gets the instance for the list delimiter handler separating via ,.
      Returns:
      The instance for the list delimiter handler separating via ,.
    • getList

      public static List<String> getList(Configuration conf, String key)
      A convenience method for ImmutableConfiguration.getList(String) that calls getList on the designated configuration with the designated key and converts the result to a list of strings or an empty list.
      Parameters:
      conf - The configuration from which to retrieve the list.
      key - The configuration key.
      Returns:
      A list of strings or an empty list if the configuration does not contain the key or the value is empty.
    • getList

      public static List<String> getList(List<Object> list)
      Since the signature of ImmutableConfiguration.getList(String) changed, we could use this tool method to 'cast' the list of objects to a list of strings.
      Parameters:
      list - A list of objects.
      Returns:
      A list of strings.
    • newMapConfiguration

      public static MapConfiguration newMapConfiguration(Map<String,String> values)
      Creates a new AbstractConfiguration containing the designated values (if provided).
      Parameters:
      values - The values to be in the base configuration or null for an empty configuration.
      Returns:
      A new map configuration.
    • newMapConfiguration

      public static MapConfiguration newMapConfiguration(Map<String,String> values, boolean handleListDelimiters)
      Creates a new AbstractConfiguration containing the designated values (if provided).
      Parameters:
      values - The values to be in the base configuration or null for an empty configuration.
      handleListDelimiters - Whether to handle list delimiters within strings.
      Returns:
      A new map configuration.
    • newPropertiesConfiguration

      public static PropertiesConfiguration newPropertiesConfiguration()
      Creates a new empty PropertiesConfiguration.
      Returns:
      A new empty properties configuration.
    • newPropertiesConfiguration

      public static PropertiesConfiguration newPropertiesConfiguration(InputStream is) throws ConfigurationException
      Creates a new PropertiesConfiguration containing the contents of the designated input stream.
      Parameters:
      is - The input stream to read into the new properties configuration or null for an empty configuration.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading from the stream fails, a ConfigurationException will be thrown.
    • newPropertiesConfiguration

      public static PropertiesFileConfiguration newPropertiesConfiguration(URL url, InputStream is) throws ConfigurationException
      Creates a new PropertiesFileConfiguration containing the contents of the designated input stream.
      Parameters:
      url - The URL specifying the properties file. This is merely for information purpose. If you do not have an URL use newPropertiesConfiguration(InputStream).
      is - The input stream to read the configuration from.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading from the stream fails, a ConfigurationException will be thrown.
    • newPropertiesConfiguration

      public static PropertiesFileConfiguration newPropertiesConfiguration(String file) throws ConfigurationException
      Creates a new PropertiesFileConfiguration containing the contents of the designated file.
      Parameters:
      file - The file to read into the new properties configuration or null for an empty configuration.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading the file fails, a ConfigurationException will be thrown.
    • newPropertiesConfiguration

      public static PropertiesFileConfiguration newPropertiesConfiguration(String file, Logger autosaveLogger) throws ConfigurationException
      Creates a new PropertiesFileConfiguration containing the contents of the designated file.
      Parameters:
      file - The file to read into the new properties configuration or null for an empty configuration.
      autosaveLogger - Whether to automatically save changes to the underlying file. If so, problems will be logged to this logger. Use null to not automatically save changes.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading the file fails, a ConfigurationException will be thrown.
    • newPropertiesConfiguration

      public static PropertiesFileConfiguration newPropertiesConfiguration(File file) throws ConfigurationException
      Creates a new PropertiesFileConfiguration containing the contents of the designated file.
      Parameters:
      file - The file to read into the new properties configuration or null for an empty configuration.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading the file fails, a ConfigurationException will be thrown.
    • newPropertiesConfiguration

      public static PropertiesFileConfiguration newPropertiesConfiguration(File file, Logger autosaveLogger) throws ConfigurationException
      Creates a new PropertiesFileConfiguration containing the contents of the designated file.
      Parameters:
      file - The file to read into the new properties configuration or null for an empty configuration.
      autosaveLogger - Whether to automatically save changes to the underlying file. If so, problems will be logged to this logger. Use null to not automatically save changes.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading the file fails, a ConfigurationException will be thrown.
    • newPropertiesConfiguration

      public static PropertiesFileConfiguration newPropertiesConfiguration(URL url) throws ConfigurationException
      Creates a new PropertiesFileConfiguration containing the contents of the designated URL.
      Parameters:
      url - The URL which to read into the new properties configuration or null for an empty configuration.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading from the URL fails, a ConfigurationException will be thrown.
    • newPropertiesConfiguration

      public static PropertiesFileConfiguration newPropertiesConfiguration(URL url, Logger autosaveLogger) throws ConfigurationException
      Creates a new PropertiesFileConfiguration containing the contents of the designated URL.
      Parameters:
      url - The URL which to read into the new properties configuration or null for an empty configuration.
      autosaveLogger - Whether to automatically save changes to the underlying file. If so, problems will be logged to this logger. Use null to not automatically save changes.
      Returns:
      A new properties configuration.
      Throws:
      ConfigurationException - If creating the configuration or loading from the URL fails, a ConfigurationException will be thrown.
    • newDatabaseConfiguration

      public static DatabaseConfiguration newDatabaseConfiguration(DataSource dataSource, String tableName, String keyColumn, String valueColumn) throws ConfigurationException
      Creates a new DatabaseConfiguration using the designated table and columns to store the configuration in the designated data source. This is a simple database configuration as provided by Commons Configuration, that is, the existence of the table is not handled. That is, the table will not be created and thus no configuration values will be stored. Also the existence of the table will not be checked before accessing it. This may lead to a lot of warnings in the DBMS log; every read or write access of a configuration entry without the corresponding table being present creates a log entry.
      Therefore usage of this method is strongly discouraged. Use DatabaseConfiguration.newDatabaseConfiguration(Logger, de.aristaflow.adept2.base.dbaccess.JDBCDataSource, String, String, String) instead which creates the table if appropriate and checks for its existence before accessing it.
      Parameters:
      dataSource - The datasource which stores the configuration.
      tableName - The name of the table containing the configuration values.
      keyColumn - The name of the column containing the configuration keys.
      valueColumn - The name of the column containing the configuration values.
      Returns:
      A new database configuration.
      Throws:
      ConfigurationException - If creating the configuration fails, a ConfigurationException will be thrown.
      See Also:
    • newCompositeConfiguration

      public static CompositeConfiguration newCompositeConfiguration(Configuration inMemoryConf)
      Creates a new CompositeConfiguration allowing to combine several configurations. If a configuration is provided, this will be the in memory configuration in which all changes of the returned composite configuration values will be stored.
      Parameters:
      inMemoryConf - The in memory configuration in which all changes of the returned composite configuration values will be stored. This may be null.
      Returns:
      A new composite configuration.