wood burning stoves
The moose likes Java in General and the fly likes Hierarchical properties files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hierarchical properties files" Watch "Hierarchical properties files" New topic
Author

Hierarchical properties files

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3906
    
  18

Hi guys,

I am working on a very ambitious project, in which I have loads of services, that will be loaded using instances of a class; let's call it Tag. You could compare this architecture to how formats, calendars, currencies, etc. are loaded by providers (e.g. NumberFormatProvider, CurrencyNameProvider) using Locale.

Now, I'm in a situation where I'd like to use Properties, depending on a Tag. The thing is, I want to make these Properties default to ancestor Properties. Basically what I need, is a class that's to Tag what ResourceBundle is to Locale. So why not use Locale and ResourceBundle? The mechanics are exactly what I need, but Tag simply has nothing to do with languages or countries.

I would really have liked that ResourceBundle worked with Strings, rather than Locales, but here we are. Now, to avoid making the same mistake, I'm thinking of writing a class PropertiesBundle that takes Strings, rather than Tags, and have my Tag class return a tag String.

I'm curious what you think of this approach, if you have better ideas, suggestions or hints, or if you know if someone already made something like this. What I found so far online were poorly written classes that I really don't want to add to my project.

Thanks guys.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19693
    
  10

Maybe I'm missing an important detail, but a Properties object can contain another Properties object which it uses for defaults. (After 30 seconds of thinking) I don't see any reason why this can't be used hierarchically -- i.e. the Properties object which is used for defaults could itself use a third Properties object for defaults? And no, I haven't thrown together the sample code to test this theory either.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3906
    
  18

Ahh I'm very sorry, I had a bit of a brain-fart.

Yes, I am going to use a chain of Properties instances, but I want to wrap it in a class that hides the chain, and initializes it using a tag String such as "com.example.a_b_c". It will have the exact same functionality as PropertiesResourceBundle.

Do you suppose it's nicer to supply the application with my own (or preferably a third party's) version of PropertiesResourceBundle, or just a class that provides a Properties instance based on a Tag instance?

Personally I prefer the former, because I don't like the mutable nature of Properties, nor the bogus methods that come with it, which I'd like to abstract away. I'm guessing it's a matter of taste, but I'm building my application with opinions of others in mind.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19693
    
  10

If you need a subclass of ResourceBundle, I still don't think I would subclass PropertyResourceBundle to do what you want. Unless you actually need its built-in capability to read a Properties file from an InputStream or Reader, that is. If you're going to have a customized way of initializing the object then I think I would just subclass ResourceBundle. Or maybe even just invent your own class which is like Properties and/or ResourceBundle but only has the methods you really need. And yeah, I agree with you that I would probably wrap my chain of Properties objects into something less Java-1-like.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3906
    
  18

Thanks for the input Paul, much appreciated. Hopefully in three more years I'll have my thing finished XD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hierarchical properties files
 
It's not a secret anymore!