java.lang.Object
org.bzdev.util.ConfigPropUtilities
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.
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
allows one to switch from "dark mode" by simply changing two values.foregroundColor = white backgroundColor = rbg(10,10,20) headingColor = $(foregroundColor) textColor = $(foregroundColor) errorColor = red
Substitution loops are not supported:
will fail because the recursion will not terminate. This class does not test for this error, but the classforegroundColor = $(backgroundColor) backgroundColor = $(foregroundColor) headingColor = $(foregroundColor) textColor = $(foregroundColor) errorColor = red
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 byConfigPropUtilities
.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic char[]
getDecryptedProperty
(Properties props, String key, char[] passphrase) Get the value, decrypted if necessary, stored in an instance ofProperties
under a given key.static char[]
getGPGPassphrase
(Supplier<char[]> supplier) Get the GPG passphrase.static String
getProperty
(Properties props, String key) Get the value stored in an instance ofProperties
under a given key.static Properties
newInstance
(File f) Create a new instance ofProperties
, loading its keys and values from a file.static Properties
newInstance
(File f, String mediaType) Create a new instance ofProperties
, loading its keys and values from a file, and checking the file's media type.static Properties
Create a new instance ofProperties
, loading its keys and values from an input stream.static Properties
newInstance
(InputStream is, String mediaType) Create a new instance ofProperties
, loading its keys and values from an input sream, and checking the stream's media type.
-
Field Details
-
EMPTY_CHAR_ARRAY
public static final char[] EMPTY_CHAR_ARRAY
-
-
Constructor Details
-
ConfigPropUtilities
public ConfigPropUtilities()
-
-
Method Details
-
newInstance
Create a new instance ofProperties
, loading its keys and values from a file.- Parameters:
f
- the file from which to load properties- Throws:
IOException
-
newInstance
Create a new instance ofProperties
, 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
Create a new instance ofProperties
, 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
where MEDIATYPE is the media (or MIME type) as defined in RFC 2045 and subsequent RFCs.#(!M.T MEDIATYPE)
- Parameters:
f
- the file from which to load propertiesmediaType
- the expected media type for the file- Throws:
IOException
-
newInstance
Create a new instance ofProperties
, 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
where MEDIATYPE is the media (or MIME type) as defined in RFC 2045 and subsequent RFCs. The stream is not automatically closed.#(!M.T MEDIATYPE)
- Parameters:
is
- the input stream from which to load propertiesmediaType
- the expected media type for the file- Throws:
IOException
-
getGPGPassphrase
Get the GPG passphrase. The default supplier obtains the passphrase from the system console.- Parameters:
supplier
- aSupplier
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 ofProperties
under a given key.- Parameters:
props
- the propertieskey
- the keypassphrase
- the GPG passphrase for decryption.- Returns:
- the decrypted value
- Throws:
GeneralSecurityException
- if decryption failed
-
getProperty
Get the value stored in an instance ofProperties
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 ofProperties
storing key-value pairs.key
- the key- Returns:
- the value for the given key; null if the key does not exist
- Throws:
IllegalStateException
-