Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain Constant from Properties file?

Is it possible to obtain a constant form a java properties file? I am trying to develop my application so that I can set a flag in a master properties file that dictates whether the application uses the development config file or the production one.

Most of application works fine but Ive hit a bit of a brick wall with the database table mapping in the entity classes. I am using Dynamo DB so the actual class itself requires an annotation as follows:

@DynamoDBTable(tableName = "table_name")
public class EmailTemplates {
...

The tableName value must be a constant and I'm trying to use the following to retrieve the value from the properties file and pass it to the tableName;

@DynamoDBTable(tableName = DynamoTable.TABLE_NAME)
public class EmailTemplates {
...

The DynamoTable class:

public final class DynamoTable {

    public static final String TABLE_NAME = ResourceBundle.getBundle(ResourceBundle.getBundle("ProjectStage").getString("ProjectStage")).getString("EmailTemplates");

}

Unfortunately its not working as its saying the value is not a constant. If I simply put a literal string (ie. "aStringValue") then its fine, but not from the properties file.

NB. Just to be clear there isn't a problem with the above code retrieving the values from the properties file. The problem is that its not being treated as a constant.

like image 626
tarka Avatar asked Dec 28 '22 00:12

tarka


2 Answers

You can't do what you want to do.

You're asking the javac compiler to execute arbitrary code to read a properties file from disk, extract a value, and insert that as a constant into your code (at compile time).

As you can see, that's a bit much for the compiler to do.

You can only execute that code at runtime, which doesn't suite your situation because you're trying to assign a value to an annotation (and thus the value for the annotation must be available at compile time).

like image 156
Greg Kopff Avatar answered Jan 09 '23 11:01

Greg Kopff


If you are using the DynamoDBMapper class for your database interaction, there is the option to provide an extra DynamoDBMapperConfig argument for loads, saves, deletes, queries and scans.

You can build this config at runtime and then pass it into the dynamoDB operations.

private DynamoDBMapperConfig getEnvironmentConfig(DymanoTable tableEntity, String environment)
{
    DynamoDBTable annotation = (DynamoDBTable) tableEntity.getClass().getAnnotation(DynamoDBTable.class);
    DynamoDBMapperConfig.TableNameOverride tableNameOverride = new DynamoDBMapperConfig.TableNameOverride(environment + annotation.tableName());
    return new DynamoDBMapperConfig(tableNameOverride);
}

You will need to create a DymnamoDBMapper...

AWSCredentials credentials = new PropertiesCredentials(
                ObjectPersistenceQueryScanExample.class.getResourceAsStream("AwsCredentials.properties"));

        client = new AmazonDynamoDBClient(credentials);
        DynamoDBMapper mapper = new DynamoDBMapper(client);

And the use this for your dynamoDB interaction

public void save(DynamoTable entity)
{
    mapper.save(entity, getEnvironmentConfig(entity,"dev"));
}
like image 42
cfly Avatar answered Jan 09 '23 10:01

cfly