• Post Reply Bookmark Topic Watch Topic
  • New Topic

Config data storage - best practices?  RSS feed

 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm developing a small application with Java 1.4.2 that will check mail using a proprietary protocol. My program needs to keep track of login information (username, password, URL, and a couple of other things) for each mailbox. It can handle N mailboxes. Upon startup, the program should be able to read in all the data. A Config screen will allow me to add new mailboxes, etc.

What's the best way to keep track of this stuff? A properties file? An XML file of some sort? If I use an XML file, what's the best way to read and write to it? XML and Java are fairly new to me, but I'm not averse to learning it.

Thanks for the advice!
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ... um ... T,

A properties file sounds like the best choice if you want to keep it simple. I would say that best practice would probably involve some kind of database (or secure persistent storage for something like login details), but that may be overkill for your little app and would add a dependency.

Jules
 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Julian,

That was my original thought, too. A properties file, as I understand it, is basically a bunch of key/value pairs right? Since there wil be about 6 variables for each mailbox, and N mailboxes, I suppose I'll have to think carefully about how to structure the file. Any suggestions are welcome! I can see either:

---------
Box1=var1, var2, var3, ...
Box2=var1, var2, var3, ...

And then parse the comma-delimited value into values
---------

Or:

---------
Var11=x
Var21=x
...
Var61=x
Var12=x
Var22=x
...
Var6N=x
---------

And parse that somehow. An XML file with N mailbox elements might be nicer, but would it be a lot more difficult?

Thanks again,

Tom Manning
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that xml might be more appropriate. Inaddition to key/value pairs, xml adds hierarchy and most importantly for your case allows multiple values with the same key. For example,

 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The structure of properties files is (or can be) hierarchical as with the Java package structure (e.g. com.company.app.package).

So you can (and probably should) have the following:

You can then read all the properties for box1 with one command. Check out the API - you should be able to see how to do it.

I can't really comment on the XML approach as my experience with JAXP is limited (but I will anyway ). It's not very complicated but it is more complicated than using properties.

Jules
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian,

You might be right that properties will be easier to use. I like your structure so I did look in the API and I don't see how to get all the values for keys starting in say 'box1.' with one method call. I'd won't mind being wrong if I could discover how to do that.

Thanks,
Mark
 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jules - that's excellent information. I think that to start with I will learn about the Properties API and use a properties file to keep track of this stuff.

Rovas - In the future (once the app is actually working with Properties), I think that I would like to try to do it with XML - mostly just because I want to learn how to do it. Can you offer tips on how best to go about it, or examples anywhere on using XML for properties in Java 1.4.2? (I read somewhere that support for XML is integrated into Java 5 Properties, but I might be stuck with Java 1.4 for a while).

Thanks again, for all your help (both of you). APIs are all very nice, but best practices for novices are hard to come by sometimes.

Tom
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, I've just followed my own advice (for once) and RTFM and it seems that I've been leading you astray.

I can't see any mechanism for reading a subset of properties and there is no hierarchy inherent in the format (though it's good practice). This is something I've seen done previously in an enterprise app, but it must have been a custom API. Sorry about that.

I still think properties will be simpler to use but, as Rovas says, XML may be more appropriate. It also gives you an excuse to play with it!

Jules
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun has an xml parser that you can download for free from their website.

Another thing to consider is to serialize objects and write them to disk using ObjectOutputStream. You can then have the server read the resultant binary file at startup using ObjectInputStream. This will work dandily if you don't need to read the config with a text editor. If you want the config file to be editable then you can use java.beans.XMLEncoder and java.beans.XMLDecoder(very cool and simple) which will write objects in user readable-xml and reconstruct the objects from the same xml.
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought java.util.prefs.Preferences might be a good option. It provides the hierarchical functionality I mentioned. Having read the documentation properly, however, I don't think it's really appropriate.

The JavaBeans XML utilities are a good option - I'd forgotten about those. I don't know how you actually manipulate the XML once you've read it in though.

Jules
 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm.... too bad about the Properties. I'll investigate XMLEncoder/Decoder and see how hard it will be to get them to work. I suppose I might need a separate class to save/load my config data that way. If I take an object, say, "Config" that contains an array or a list of Mailbox objects, each object consisting of Strings and a bunch of methods, then what? If I encode the "Config" object, does it make the one big XML file with N Mailbox objects, holding only their Strings?

So many hurdles to overcome and questions to answer, and it seemed like such a simple idea - "Use an XML file to store config data for multiple objects".

*sigh*

Thank you for your input so far, it's been invaluable.

Tom
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
config-files often used such a structure for repeated values:

[Alice]
password=
url=
other=
[Bob]
password=
url=
other=

but I guess the java-Properties don't support it, which means to write it on your own.

Multiple files would be another soulution:
Alice.box
Bob.box
....box

But multiple mini-files aren't very nice.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless of the file format, here's a thought about dependencies. If your mail sending engine does calls like configManager.getConfigItem("key") you won't be able to test it without a configManager and a file. To get around that, you might have a configurable configuration manager ... plug in a special configManager for testing. But where do you configure that thing?

There's a design sometimes called "application assembler" that has some object that puts all the pieces together at startup. It might have lines like this in a loop:

Now to unit test mailEngine you can addBoxInfo with some made up information and no file or config manager at all. And look how easy it would be to switch to a SAX parser that does the addBoxInfo as it parses. Hope that was useful!
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,

Here's an example:






The contents of the file created by XMLEncoder.

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_03" class="java.beans.XMLDecoder">
<object class="Config">
<void property="IT">
<string>whomever</string>
</void>
<void property="mbList">
<array class="MailBox" length="2">
<void index="0">
<object class="MailBox">
<void property="password">
<string>letme</string>
</void>
<void property="username">
<string>john.doe</string>
</void>
</object>
</void>
<void index="1">
<object class="MailBox">
<void property="password">
<string>showme</string>
</void>
<void property="username">
<string>jane.smith</string>
</void>
</object>
</void>
</array>
</void>
</object>
</java>
[ August 09, 2004: Message edited by: Rovas Kram ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go with XML, you should take a look at DOM4J - it has a much nicer API than W3C's SAX/DOM (which JAXP is building on).
 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow - I never expected so much input. I really appreciate everybody spending the time to give their advice!

Thanks very much! I'll check out Dom4j and probably end up using an XML encoder/decoder of some sort for this

Tom
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!