• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variables within Properties files

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this in my global.properties file:

dir.base=/home/dev/app
dir.conf=${dir.base}/conf
dir.bin=${dir.base}/bin
dir.data=${dir.base}/data
dir.lib=${dir.base}/lib
dir.log=${dir.base}/log

My code pulls from this properties file like so:



The output however looks like so:


baseDir = /home/dev/app
confDir = ${dir.base}/conf
binDir = ${dir.base}/bin
dataDir = ${dir.base}/data
libDir = ${dir.base}/lib
logDir = ${dir.base}/log

So obviously it outputs ${dir.base} when I would like to see the string value from baseDir/dir.base. Is this possible what I'm wanting or do I maybe need to use a different Properties class to accomplish this?

Thanks much for the help!

Jay N.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, java.util.Properties does not support this. However, it's not very hard to add this behaviour yourself. In fact, I have done just that myself.

I won't give you the full code for two reasons: 1) we are NotACodeMill, and 2) my code is licensed under GPLv2, and that has some limitations what you can do with the code.

I will however give you a few hints:
1) Extend or wrap java.util.Properties. Add a new method; e.g. String getExpandedProperty(String key)

2) This method should get the original property (getProperty(key)), and then replace all occurrences of {abc} with the values of property abc. You can use a java.util.regex.Pattern for finding those occurrences. If you make this method recursive, you can even use variables inside variables.

3) If you make your method recursive, beware of cyclic variables. If one variable contains another variable which again contains the first variable, you could get either an infinite loop or a StackOverflowException. I have used a Map<String, String> to cache values; if a key is first encountered I store it in this Map, then I set its value once it's resolved. If a key is already in the Map then I do not try to replace it again.
 
Jay Newberg
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

I was hoping to avoid writing a wrapper and hoping for something out of the box :-) but no worries.... I appreciate you taking the time to explain this and a solution.

Many thanks,
JN
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might not help you, but Spring does this out of the box if you use PropertyPlaceholderConfigurer.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Newberg wrote:Hi Rob,

I was hoping to avoid writing a wrapper and hoping for something out of the box :-) but no worries.... I appreciate you taking the time to explain this and a solution.

Many thanks,
JN


You're welcome.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's funny. It so happenned, one of my coworkers was looking at the exact same thing. He found Apache Commons Configuration does exactly what you want. I think Spring uses it internally
reply
    Bookmark Topic Watch Topic
  • New Topic