Home > Software engineering >  How to read properties file in Install4j - example appreciated
How to read properties file in Install4j - example appreciated

Time:10-07

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;

CodePudding user response:

1 - Create your own 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 function for reading Longs, Strings, Booleans, whatever.

2- Initialize your properties entity and read the file:

ConfigProperties props = new ConfigProperties();
props.load(new FileInputStream(propertiesFile));

3- Read from your properties instance

Once you have the keys and values stored in your props variable, now you can just retrieve the values by key.

int majorv = props.readMandatoryInteger("version.major"); 

System.out.print(majorv); // 2

It's not required at all to extend the base Properties class. Anyway, doing so lets you control the inputs and avoid possible errors (such as inserting a text in a numeric field, or missing crucial variables, such as the connection url for a database.

CodePudding user response:

There is no "map" variable type in Install4j.

This is the java.util.Map from the standard library in the JDK. The action does not create installer variables for each entry in the properties file. I've added this idea for a new action to our issue tracker.

Assuming you have set the "Variable name" property of the "Read a properties file" action to "propertyMap", add a "Run script" action after it and set its "Script" property to

    Map<String, String> propertyMap = (Map<String, String>)context.getVariable("propertyMap");
    for (Map.Entry<String, String> entry : propertyMap.entrySet()) {
        context.setVariable(entry.getKey(), entry.getValue());
    }
    return true;

Then each property in the properties file will be saved as an installer variable and you can use the variables with the syntax

${installer:version.major}

in text field properties of subsequent screens, actions and form components.

  • Related