Package overit.geocall.util
Class TextInjector
java.lang.Object
overit.geocall.util.TextInjector
The class is used to inject into a string, that contains a reference to a file,
a part or the entire contents of the referenced file. The string to be injected
will replace the string portion containing the reference.
To create a reference to a file within a string, it is necessary to insert the content inside square brackets and insert as the first attribute 'inject'.
The allowed attributes are:
In case you want to use the mode with the reference to a variable, the file must comply with the rules for defining the
If neither the 'line' attribute nor the
Below is an example to clarify its use:
To create a reference to a file within a string, it is necessary to insert the content inside square brackets and insert as the first attribute 'inject'.
The allowed attributes are:
'inject': reference to the file to be used in the search for the string to be extrapolated. This must be the first of the attributes and is the only one required;'encoding': encoding to be used to interpret the file. If not set the default system is taken;'line': line number to be extrapolated; If the line number is greater than the length of the file, is returned an empty string. To be used as an alternative to the'var'attribute;'var': variable to search within the file. If a match is found, the associated value is returned; otherwise return an empty string. To be used alternatively to the'line'attribute.'where': can be "fs", that is the default value, to define that the reference to the file is an external reference, "cp" to define that the reference to the file is a classpath or "awsSecret" to define that the reference of the value must be fetched from AWS Secrets Manager service
In case you want to use the mode with the reference to a variable, the file must comply with the rules for defining the
Properties.If neither the 'line' attribute nor the
'var' attribute is inserted,
it will be loaded entirely the file.Below is an example to clarify its use:
| "text [inject:etc/file.conf] text" | Inject all text of the file |
| "text [inject:/overit/geocall/basic/Password.properties;where:cp] text" | Inject all the text of the property file located in the classpath overit.geocall.basic |
| "text [inject:etc/file.conf;encoding:UTF-8] text" | Inject all text of the file forcing the encoding "UTF-8" |
| "text [inject:etc/file.conf;line:10] text" | Inject the line 10 of the file |
| "text [inject:etc/file.conf;var:VARNAME;encoding:UTF-8] text" | Inject the value of the variable VARNAME of the file forcing the encoding "UTF-8" |
| "text [inject:/overit/geocall/basic/Password.properties;var:VARNAME;encoding:UTF-8;where:cp] text" | Inject the value of the variable VARNAME of the property file located in the classpath overit.geocall.basic, forcing the encoding "UTF-8" |
| "text [inject:etc/file1.conf;var:VARNAME] text [inject:etc/file2.txt;line:10] text" | Inject the value of the variable VARNAME extracted from the "file1.conf" and inject the line 10 extracted from the "file2.txt" |
| "text [inject:my-secret;where:awsSecret] text" | Fetch the secrets from AWS Secret manager and inject the my-secret key's value |
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static ConcurrentHashMap<String, ConcurrentHashMap<String, String>> protected Stringprotected String -
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance of theTextInjector.TextInjector(String s, String cache) Creates an instance of theTextInjector, that will use a cache mechanism, basing the injections on the cache with the name passed as parameter -
Method Summary
Modifier and TypeMethodDescriptionChoose the mode used.inject()Resolve the references contained in the string used to create the instance (seeTextInjector()), injecting the string extracted from the file referenced.protected StringloadInject(String path, Charset encoding, int l, String where) Load the line read from the file.protected StringloadInject(String path, Charset encoding, String where) Load the entire file.protected StringloadInject(String path, Charset encoding, String variable, String where) Load the value of the variable read from the file.protected Stringprotected StringInjects the string, loading the value from the referenced file
-
Field Details
-
_stringToParse
-
_cacheToUse
-
_caches
-
-
Constructor Details
-
TextInjector
Creates an instance of theTextInjector.- Parameters:
s- String contains a reference to a file. If the string isnull, the methodinject()will returnnull; if it not contains a reference, the method will return the same string.
-
TextInjector
Creates an instance of theTextInjector, that will use a cache mechanism, basing the injections on the cache with the name passed as parameter- Parameters:
s- String contains a reference to a file. If the string isnull, the methodinject()will returnnull; if it not contains a reference, the method will return the same string.cache- The name of the cache to use to retrieve and store the injections
-
-
Method Details
-
inject
Resolve the references contained in the string used to create the instance (seeTextInjector()), injecting the string extracted from the file referenced. If requested, it uses the cache- Returns:
- The string with the reference resolved. If the string is
nullreturnnull; if it not contains a reference, return the same string.
-
parse
-
parse
Injects the string, loading the value from the referenced file- Parameters:
s- The string to parsestart- The start delimiterend- The end delimiter- Returns:
- The string injected
-
doInject
Choose the mode used.- Parameters:
injection- String in the format: "inject:..."- Returns:
- The string resolved
- Throws:
IOException- in case of injection IO errors like for example not founding injection file
-
loadInject
protected String loadInject(String path, Charset encoding, String variable, String where) throws IOException Load the value of the variable read from the file.- Parameters:
path- Path of the property fileencoding- Encoding used in readingvariable- The name of the variablewhere- Defines if the path is a classpath ("cp") or an external path ("fs", default)- Returns:
- The string resolved
- Throws:
IOException- in case of injection IO errors like for example not founding injection file
-
loadInject
Load the line read from the file.- Parameters:
path- Path of the fileencoding- Encoding used in readingl- Line numberwhere- Defines if the path is a classpath ("cp") or an external path ("fs", default)- Returns:
- The string resolved
- Throws:
IOException- in case of injection IO errors like for example not founding injection file
-
loadInject
Load the entire file.- Parameters:
path- Path of the fileencoding- Encoding used in readingwhere- Defines if the path is a classpath ("cp") or an external path ("fs", default)- Returns:
- The string resolved
- Throws:
IOException- in case of injection IO errors like for example not founding injection file
-