Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Properties, canonical way of reading/writing configuration files.

Reply
Thread Tools

Properties, canonical way of reading/writing configuration files.

 
 
Alex Polite
Guest
Posts: n/a
 
      06-06-2004
Here's another newbie question?

Is java.util.Properties the canonical way of reading and writing
configuration files?

alex

--
Alex Polite
http://polite.se
 
Reply With Quote
 
 
 
 
lyallex
Guest
Posts: n/a
 
      06-06-2004
On 6 Jun 2004 14:59:24 GMT, Alex Polite <(E-Mail Removed)> wrote:

>Here's another newbie question?
>
>Is java.util.Properties the canonical way of reading and writing
>configuration files?
>
>alex


Alex

I would say yes before J2SE 1.4 came out.
If you are using 1.4 or later check out java.util.prefs.Preferences.

Rgds
Lyall

 
Reply With Quote
 
 
 
 
Liz
Guest
Posts: n/a
 
      06-07-2004

"lyallex" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 6 Jun 2004 14:59:24 GMT, Alex Polite <(E-Mail Removed)> wrote:
>
> >Here's another newbie question?
> >
> >Is java.util.Properties the canonical way of reading and writing
> >configuration files?
> >
> >alex

>
> Alex
>
> I would say yes before J2SE 1.4 came out.
> If you are using 1.4 or later check out java.util.prefs.Preferences.
>
> Rgds
> Lyall
>


A nit, perhaps, but in Windows the preferences are stored in the registry
not in a file, so it can't be a configuration file )


 
Reply With Quote
 
Duncan Strang
Guest
Posts: n/a
 
      06-07-2004
On Mon, 07 Jun 2004 03:15:33 GMT, "Liz" <(E-Mail Removed)> wrote:

>
>"lyallex" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> On 6 Jun 2004 14:59:24 GMT, Alex Polite <(E-Mail Removed)> wrote:
>>
>> >Here's another newbie question?
>> >
>> >Is java.util.Properties the canonical way of reading and writing
>> >configuration files?
>> >
>> >alex

>>
>> Alex
>>
>> I would say yes before J2SE 1.4 came out.
>> If you are using 1.4 or later check out java.util.prefs.Preferences.
>>
>> Rgds
>> Lyall
>>

>
>A nit, perhaps, but in Windows the preferences are stored in the registry
>not in a file, so it can't be a configuration file )


Guess you need to read the API docs Liz

http://java.sun.com/j2se/1.4.2/docs/api/index.html

Rgds
Lyall

 
Reply With Quote
 
lyallex
Guest
Posts: n/a
 
      06-07-2004
On Mon, 07 Jun 2004 03:15:33 GMT, "Liz" <(E-Mail Removed)> wrote:

>
>"lyallex" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> On 6 Jun 2004 14:59:24 GMT, Alex Polite <(E-Mail Removed)> wrote:
>>
>> >Here's another newbie question?
>> >
>> >Is java.util.Properties the canonical way of reading and writing
>> >configuration files?
>> >
>> >alex

>>
>> Alex
>>
>> I would say yes before J2SE 1.4 came out.
>> If you are using 1.4 or later check out java.util.prefs.Preferences.
>>
>> Rgds
>> Lyall
>>

>
>A nit, perhaps, but in Windows the preferences are stored in the registry
>not in a file, so it can't be a configuration file )
>



If you find tha API docs a bit abstract (I know I do) try

http://www.javaworld.com/javaworld/j...rences-p2.html

Where the preferences are actually stored doesn't matter. You can
create and edit a preferences file, I guess that's all you need.

Rgds
Lyall

 
Reply With Quote
 
Alex Polite
Guest
Posts: n/a
 
      06-07-2004
On sön, jun 06, 2004 at 03:43:53 +0000, lyallex wrote:
> Alex
>
> I would say yes before J2SE 1.4 came out.
> If you are using 1.4 or later check out java.util.prefs.Preferences.
>


OK I tried it out.

Storing preferences data in an XML file and the way that file is
interfaced with getInt et. al. feels soooo right.

But this ~/.java/.prefs thing isn't right for this project.

Can I tell the Preferences constructor to look at a specific path
rather then ~/.java/.prefs?

I need instances for different users to look at the same place. I
can't put it under system preferences cause I'm not root. This is for
an application running on a webhotel. So the user will be something
like nobody or www-data.


Maybe there's some other package that does persistance with XML,
better fitted for my needs?

alex



--
Alex Polite
http://polite.se
 
Reply With Quote
 
lyallex
Guest
Posts: n/a
 
      06-07-2004
On 6 Jun 2004 14:59:24 GMT, Alex Polite <(E-Mail Removed)> wrote:

>Here's another newbie question?
>
>Is java.util.Properties the canonical way of reading and writing
>configuration files?
>
>alex


Hi

It seems that the article I mentioned in an earlier post

http://www.javaworld.com/javaworld/j...rences-p2.html

has some 'inconsistencies' with my current version of J2SE 1.4.2. The
article mentions getting a reference to a Preferences object by
passing an object reference but my API docs only show a
userNodeForPackage method that takes a Class as argument so I thought
I'd do a quick run through of getting and setting preferences and see
if anyone can point out problems with this approach.

First set up a properties node

package anon.pack.prefs;

import java.util.prefs.Preferences;
import java.io.FileOutputStream;

public class PropertiesTest {

private Preferences prefs;

public PropertiesTest() {
prefs = Preferences.userNodeForPackage(this.getClass());
String outfile = "C:/prefs/prefs.xml";
prefs.put("outfile", outfile);
try{
prefs.exportNode(new FileOutputStream(outfile));
}
catch(Exception ex){
}
}

public static void main(String[] args){
new PropertiesTest();
}
}

After running this once I have the following file in
C:/prefs/prefs.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE preferences SYSTEM
'http://java.sun.com/dtd/preferences.dtd'>

<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="anon">
<map />
<node name="pack">
<map />
<node name="prefs">
<map>
<entry key="outfile" value="C:/prefs/prefs.xml" />
</map>
</node>
</node>
</node>
</root>
</preferences>

I can now modify my code to read the preferences file location thus

....

public PropertiesTest() {
prefs = Preferences.userNodeForPackage(this.getClass());
String prefsfile = prefs.get("outfile", "C:/temp/tempprefs.xml");
try{
prefs.importPreferences(new FileInputStream(prefsfile));
}
catch(Exception ex){
ex.printStackTrace();
}
}

So now I can change the location of my preference file at will which
is pretty cool I think.
Anyway say I now need to set a 'property' that returns the value of a
listenPort (crap example but it does the job). All I need to do is to
edit my preferences file and add in a new entry like this.

<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="anon">
<map />
<node name="pack">
<map />
<node name="prefs">
<map>
<entry key="outfile" value="C:/prefs/prefs.xml" />
<!-- *********** new entry *********** -->
<entry key="listenPort" value="26358" />
</map>
</node>
</node>
</node>
</root>
</preferences>

Now I can access this preference/property in my code

....

public String getPref(String key){
return prefs.get(key, "someDefaultValue");
}

public static void main(String[] args){
System.out.println(new PropertiesTest().getPref("listenPort"));
}

The output is

26358


Which seems pretty cool to me, I couldn't give a monkeys where the
information is stored on the platform, as far as I'm concerned I have
a platform independent, location independent way of specifying
properties for my programs.

I'm not really sure where the author of the article above is coming
from when he uses objects to get preferences, perhaps this was an
earlier version (or a later one, I haven't looked yet). Whatever, much
of what he says seems to make sense (to me at least).

What, if anything, do people think is wrong with this approach ?

Rgds
Lyall





 
Reply With Quote
 
Alex Polite
Guest
Posts: n/a
 
      06-07-2004
On mån, jun 07, 2004 at 09:58:56 +0000, Alex Polite wrote:
> On sön, jun 06, 2004 at 03:43:53 +0000, lyallex wrote:
> > Alex
> >
> > I would say yes before J2SE 1.4 came out.
> > If you are using 1.4 or later check out java.util.prefs.Preferences.
> >

>
> OK I tried it out.
>
> Storing preferences data in an XML file and the way that file is
> interfaced with getInt et. al. feels soooo right.
>
> But this ~/.java/.prefs thing isn't right for this project.
>
> Can I tell the Preferences constructor to look at a specific path
> rather then ~/.java/.prefs?
>
> I need instances for different users to look at the same place. I
> can't put it under system preferences cause I'm not root. This is for
> an application running on a webhotel. So the user will be something
> like nobody or www-data.



Turns out you can supply the userRoot and systemRoot variables as
arguments to the JVM.

java -Djava.util.prefs.systemRoot=<somepath> <someapp>

Then you have to give anyone using the app read/write permissions to
everything under <somepath>.

The problem is just that if a user creates a new node that file will
have write premissions for that user only. If you only have one
concurrent user and reset the premissions after the app is run you
should be alright.

A cleaner alternative would be to override
java.util.prefs.PreferencesFactory and use a SQL backend or something
like that. But 1.4.1 dosen't seem to provide that and I don't want to
stray from the standard library.

Besides my app will not be changing the preferences.

alex

--
Alex Polite
http://polite.se
 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      06-07-2004
lyallex wrote:

> First set up a properties node
>
> package anon.pack.prefs;
>
> import java.util.prefs.Preferences;
> import java.io.FileOutputStream;
>
> public class PropertiesTest {
>
> private Preferences prefs;
>
> public PropertiesTest() {
> prefs = Preferences.userNodeForPackage(this.getClass());
> String outfile = "C:/prefs/prefs.xml";
> prefs.put("outfile", outfile);
> try{
> prefs.exportNode(new FileOutputStream(outfile));
> }
> catch(Exception ex){
> }
> }
>
> public static void main(String[] args){
> new PropertiesTest();
> }
> }
>
> After running this once I have the following file in
> C:/prefs/prefs.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <!DOCTYPE preferences SYSTEM
> 'http://java.sun.com/dtd/preferences.dtd'>
>
> <preferences EXTERNAL_XML_VERSION="1.0">
> <root type="user">
> <map />
> <node name="anon">
> <map />
> <node name="pack">
> <map />
> <node name="prefs">
> <map>
> <entry key="outfile" value="C:/prefs/prefs.xml" />
> </map>
> </node>
> </node>
> </node>
> </root>
> </preferences>
>


Yes, but you should ALSO have the same information in ~/.java/ whatever,
so there is no point in having exported it to a file, other than that
another user might be able to import those settings and configure their
app environment the same way.

Note that you read the location of the prefs outfile from prefs before
you have imported your (previously exported) file. Try skipping that
step, and seeing if your listenPort is still set

> Which seems pretty cool to me, I couldn't give a monkeys where the
> information is stored on the platform, as far as I'm concerned I have
> a platform independent, location independent way of specifying
> properties for my programs.


Sure. It looks like it is platform independent anyway, which is really
all you need, I think.

>
> What, if anything, do people think is wrong with this approach ?


See above
>
> Rgds
> Lyall


Please don't take my comments to be knocking Preferences, I think it
looks great! I'm just pointing out some implementation mistakes that you
made.

Rogan
--
Rogan Dawes

*ALL* messages to http://www.velocityreviews.com/forums/(E-Mail Removed) will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      06-07-2004
> On Mon, 07 Jun 2004 03:15:33 GMT, "Liz" <(E-Mail Removed)> wrote:

> >A nit, perhaps, but in Windows the preferences are stored in the registry
> >not in a file, so it can't be a configuration file )


Duncan Strang wrote:
>
> Guess you need to read the API docs Liz
>
> http://java.sun.com/j2se/1.4.2/docs/api/index.html


Eh? The API docs consists of a lot of stuff. What piece exactly are
you pointing out? Is it that it's possible to reimplement the
Preferences API? Or do you mean something else?

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Canonical" way of deleting elements from lists Robert Latest Python 12 01-09-2008 08:07 PM
Canonical way to copy an array Frederick Gotham C++ 15 08-20-2006 09:09 AM
Canonical way of dealing with null-separated lines? Douglas Alan Python 17 03-02-2005 01:25 AM
Canonical way to find if a date is valid or not ? foo Java 4 01-26-2005 08:13 PM
canonical way for handling raw data Matthias Czapla C++ 7 08-27-2003 09:42 AM



Advertisments