| Author |
Gstring interpolation from properties file
|
Jon Walser
Greenhorn
Joined: Jul 23, 2012
Posts: 6
|
|
I've been handed a piece of groovy code and tasked with making it work. Of course it "worked fine" in its original environment...or so I'm told.
Properties file:
db.env=prod
db.db=${db.env}.cdc1
db.bulkdb=${db.env}.cdc1
...
jdbc.url=${${db.db}.jdbc.url}
jdbc.ro.url=${${db.db}.jdbc.ro.url}
.........................................
Code:
properties = new Properties();
FileInputStream propFile = new FileInputStream(
"conf/groovydb.properties");
properties.load(propFile);
propFile.close();
...
String dbEnv = properties.getProperty("db.db", "");
String url = properties.getProperty(dbEnv + ".jdbc.url", "");
...
System.out.println("env:" + dbEnv);
System.out.println("url:" + url);
output:
env:${db.env}.cdc1
url:
And an error indicating that my url value didn't work.
-------------
I've read about the use of Gstring interpolation, but I don't see how to apply it in a properties file. I can, of course, write some method to walk through the properties, but I'm hoping for a more elegant solution.
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
|
Can you change the properties file or does the code have to work with that specifically?
|
 |
Jon Walser
Greenhorn
Joined: Jul 23, 2012
Posts: 6
|
|
The properties file is hundreds of lines long and will necessarily grow when the interpolation references are expanded. This will require significant manual effort, with the associated risk of fat-fingering and will increase the future maintenance issues.
I am already creating a simplyfied properties file (one database, one environment etc) so I can test the rest of the program. I was just hoping for a more elegant way.
|
 |
 |
|
|
subject: Gstring interpolation from properties file
|
|
|