Class ConfigPropUtilities

java.lang.Object
org.bzdev.util.ConfigPropUtilities

public class ConfigPropUtilities extends Object
Support class for processing ConfigPropertyEditor files. These files use the format described by Properties.load(java.io.Reader). The values for keys that start with
  • ebase64. are encrypted using GPG and then encoded as printable ASCII characters using the basic Base64 encoding using the alphabet specified in Table 1 of RFC 4648.
  • base64. are encoded as printable ASCII characters using the basic Base64 encoding using the alphabet specified in Table 1 of RFC 4648.
For all other keys, the sequence $(KEY) is replaced with property value for the key KEY. This replacement occurs recursively, with the recursion terminating when the value does not specify a replacement or KEY starts with "base64." or "ebase64.".

The motivation is that sometimes values are repeated and it is both tedious and error-prone to replace each instance when there is a change. For example


 foregroundColor = white
 backgroundColor = rbg(10,10,20)
 headingColor = $(foregroundColor)
 textColor = $(foregroundColor)
 errorColor = red
 
allows one to switch from "dark mode" by simply changing two values.

Substitution loops are not supported:


 foregroundColor = $(backgroundColor)
 backgroundColor = $(foregroundColor)
 headingColor = $(foregroundColor)
 textColor = $(foregroundColor)
 errorColor = red
 
will fail because the recursion will not terminate. This class does not test for this error, but the class ConfigPropertyEditor does check and will not allow a file containing this error to be written.

Finally, this class duplicates some of the functionality provided by ConfigPropertyEditor. The reason for the duplication is that

  • the amount of code is small.
  • the JAR file containing ConfigPropertyEditor is large enough that it would be wasteful to require its module when the only functionality needed is that provided by ConfigPropUtilities.
  • Field Details

    • EMPTY_CHAR_ARRAY

      public static final char[] EMPTY_CHAR_ARRAY
  • Constructor Details

    • ConfigPropUtilities

      public ConfigPropUtilities()
  • Method Details

    • newInstance

      public static Properties newInstance(File f) throws IOException
      Create a new instance of Properties, loading its keys and values from a file.
      Parameters:
      f - the file from which to load properties
      Throws:
      IOException
    • newInstance

      public static Properties newInstance(InputStream is) throws IOException
      Create a new instance of Properties, loading its keys and values from an input stream. The stream is not automatically closed.
      Parameters:
      is - the input stream from which to load properties
      Throws:
      IOException
    • newInstance

      public static Properties newInstance(File f, String mediaType) throws IOException
      Create a new instance of Properties, loading its keys and values from a file, and checking the file's media type.

      Media types are encoded in the first line of the file, which is expected to be

      
       #(!M.T MEDIATYPE)
       
      where MEDIATYPE is the media (or MIME type) as defined in RFC 2045 and subsequent RFCs.
      Parameters:
      f - the file from which to load properties
      mediaType - the expected media type for the file
      Throws:
      IOException
    • newInstance

      public static Properties newInstance(InputStream is, String mediaType) throws IOException
      Create a new instance of Properties, loading its keys and values from an input sream, and checking the stream's media type.

      Media types are encoded in the first line of the file, which is expected to be

      
       #(!M.T MEDIATYPE)
       
      where MEDIATYPE is the media (or MIME type) as defined in RFC 2045 and subsequent RFCs. The stream is not automatically closed.
      Parameters:
      is - the input stream from which to load properties
      mediaType - the expected media type for the file
      Throws:
      IOException
    • getGPGPassphrase

      public static char[] getGPGPassphrase(Supplier<char[]> supplier)
      Get the GPG passphrase. The default supplier obtains the passphrase from the system console.
      Parameters:
      supplier - a Supplier that will provide the passphrase; null for a default
      Returns:
      the passphrase
    • getDecryptedProperty

      public static char[] getDecryptedProperty(Properties props, String key, char[] passphrase) throws GeneralSecurityException
      Get the value, decrypted if necessary, stored in an instance of Properties under a given key.
      Parameters:
      props - the properties
      key - the key
      passphrase - the GPG passphrase for decryption.
      Returns:
      the decrypted value
      Throws:
      GeneralSecurityException - if decryption failed
    • getProperty

      public static String getProperty(Properties props, String key) throws IllegalStateException
      Get the value stored in an instance of Properties under a given key. Values whose keys start with "base64." are decoded using a Base-64 decoder. Otherwise, the sequence "$(KEY)", where KEY is some key, is replaced with the value stored for KEY in the value provided for the given key.

      There are no checks for loops: the class ConfigPropertyEditor will normally be used to create the property file, and this class will check for loops.

      Parameters:
      props - the instance of Properties storing key-value pairs.
      key - the key
      Returns:
      the value for the given key; null if the key does not exist
      Throws:
      IllegalStateException