I have a properties file that basically looks like this:
version.major=2
version.minor=1
version.revision=3
And am trying to read in all 3 pieces of information using the "Read a properties file" action. The information area says the following of the variable:
The name of the variable that will be set with an instance of java.util.Map.
There is no "map" variable type in Install4j. Are we supposed to declare a single variable of type "array" like "string array" and then loop through it and set 3 discrete variables? Is there a way to leverage the array as a map throughout the installer so as to avoid helper variables like this (simply look up each key/value pair as needed)? I feel like I'm missing something.
Here's the script I'm currently using. I have read the properties into a string array called BUILD and use this to set the "major" variable (which I repeat for "minor" and "revision").
final String key = "version.major";
String[] arr = (String[])context.getVariable("BUILD");
for (int i = 0; i < arr.length; i++) {
if (arr[i].startsWith(key)) {
return arr[i].substring(arr[i].indexOf('=') + 1).trim();
}
}
return null;
Properties class extension: (optional)public class ConfigProperties extends Properties
{
private static final long serialVersionUID = 1L;
public Integer readMandatoryInteger(String key) throws IOException
{
String value = getProperty(key);
if (value == null || value.isEmpty())
throw new IOException(String.format("Insert missing config: %s.",key));
return Integer.parseInt(value);
}
//readMandatoryString, and so on....
public String getKey(String prefix, String key)
{
String value = key;
if (prefix != null && !prefix.isEmpty())
value = prefix + "." + key;
return value;
}
}
This is the simplest example I could get, you can add a function for reading Longs, Strings, Booleans, or whatever.
ConfigProperties props = new ConfigProperties();
props.load(new FileInputStream(propertiesFile));
You can just retrieve the values by key.
int majorv = props.readMandatoryInteger("version.major");
System.out.print(majorv); // 2
You can also print/show key and values, just:
list(PrintStream out)Prints this property list out to the specified output stream.
props.list(System.out);
Or you could get an Enumeration of the keys, in order to be retrieved by the first shown read operation:
propertyNames()Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.**
nameEnums = props.propertyNames();
getter method in your extended Properties classYou could use your extended Properties class and create a method that returns a HashMap, or desired structure, that stores your property file keys and values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With