• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Gstring interpolation from properties file

 
Jon Walser
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you change the properties file or does the code have to work with that specifically?
 
Jon Walser
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic