You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Kerber <dc...@verizon.net> on 2007/01/26 02:01:32 UTC

How to locate a configuration file on disk at runtime

This is a followup to my question about startup parameters.  After 
digging around a bit, it looks like my best bet is to use the Properties 
class to read my settings from a disk file at startup.  The properties 
api appears to be easy to use, and works very similarly to the 
Preferences class.  My question is how to locate the file without 
hard-coding its location.  I have looked at several possibilities, and 
can't work out the details on any of them even after a bunch of 
googling.  :-(

1.  Pass the location of the file as a JVM -D argument at app startup.  
But I can't figure how how to pick that value up inside my app.

2.  Put the config file somewhere in the tree of my webapp, such at 
docbase or in conf.  But again, I can't figure out how to pick that 
location in a form I can use to read the file in with a FileReader object.

3.  ???

Any suggestions greatly appreciated!

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Dave,
>
>David Kerber wrote:
>  
>
>>Previously, I had been using the java Preferences class, which stores
>>things in a way that is transparent to the java program (in the
>>registry on windows, elsewhere on other OS's).
>>    
>>
>
>IMHO, the Java Preferences class is a travesty for exactly that reason.
>It would have been great if they had allowed the developer to choose the
>method of storage (Windows-style INI file, Windows Registry, JNDI, Java
>properties file, UNIX run-control, whatever), but they didn't. :(
>  
>
Yeah, I hated it from the moment I saw it, but at the time I thought it 
was the only game in town...

>  
>
>>I much prefer text files for configs, but for a long time didn't know
>>there were built-in capabilities for this in java, and didn't want to
>>bother to roll my own.
>>    
>>
>
>Light dawns. Everything in java.util is really good stuff. Well, almost.
>Maybe not "Date" and "StrinkTokenizer". But everything else is good, I
>swear!
>  
>
That's probably why a good chunk of Date as been deprecated and replaced 
with Calendar and particularly GregorianCalendar.


>java.util.Properties and java.util.ResourceBundle are very similar. In
>fact, I'm not entirely sure why they are not more closely related. You
>can use the Properties class to load a properties file (using
>Properties.load()), but you can use ResourceBundle.getBundle to load a
>properties file from the classpath and /get the right Locale for those
>properties/. This is important if you are considering providing
>locale-specific properties (like translations of strings).
>  
>
Not at the moment, but I'll keep it in mind.

>If you use ResourceBundle.getBundle, then you don't even need to worry
>about ServletContext.getResourceAsStream or anything like that. Just put
>your properties files into WEB-INF/classes, or into a JAR file in
>WEB-INF/lib, and ResourceBundle will locate and load your file for you.
>  
>
This doesn't work for our situation because I load multiple apps at 
completely different context roots from the same documentRoot, but I 
imagine there are easy workarounds for this.  That's also why I have to 
define my contexts in server.xml.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave,

David Kerber wrote:
> Previously, I had been using the java Preferences class, which stores
> things in a way that is transparent to the java program (in the
> registry on windows, elsewhere on other OS's).

IMHO, the Java Preferences class is a travesty for exactly that reason.
It would have been great if they had allowed the developer to choose the
method of storage (Windows-style INI file, Windows Registry, JNDI, Java
properties file, UNIX run-control, whatever), but they didn't. :(

> I much prefer text files for configs, but for a long time didn't know
> there were built-in capabilities for this in java, and didn't want to
> bother to roll my own.

Light dawns. Everything in java.util is really good stuff. Well, almost.
Maybe not "Date" and "StrinkTokenizer". But everything else is good, I
swear!

java.util.Properties and java.util.ResourceBundle are very similar. In
fact, I'm not entirely sure why they are not more closely related. You
can use the Properties class to load a properties file (using
Properties.load()), but you can use ResourceBundle.getBundle to load a
properties file from the classpath and /get the right Locale for those
properties/. This is important if you are considering providing
locale-specific properties (like translations of strings).

If you use ResourceBundle.getBundle, then you don't even need to worry
about ServletContext.getResourceAsStream or anything like that. Just put
your properties files into WEB-INF/classes, or into a JAR file in
WEB-INF/lib, and ResourceBundle will locate and load your file for you.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFulzX9CaO5/Lv0PARAokNAKCmmO2LHjXST9M6b7olPU2VsoMpugCaA+Su
nZCv9JO6xo0EvE1eeMs1Jcg=
=Gi6J
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by David Kerber <dc...@verizon.net>.
Christopher Schultz wrote:

>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Dave,
>
>David Kerber wrote:
>  
>
>>This is a followup to my question about startup parameters.  After
>>digging around a bit, it looks like my best bet is to use the Properties
>>class to read my settings from a disk file at startup.
>>    
>>
>
>That's generally how things are done.
>  
>
Thanks for confirming that.  Previously, I had been using the java 
Preferences class, which stores things in a way that is transparent to 
the java program (in the registry on windows, elsewhere on other OS's).  
It works, but is something of a pain for initial setup, because it has 
some strange ways of specifying key names, and won't find them if there 
are any mismatches.  I much prefer text files for configs, but for a 
long time didn't know there were built-in capabilities for this in java, 
and didn't want to bother to roll my own.

>Chuck's note about using ServletContext.getResourceAsStream is a good
>one. Since Properties files are always in ISO-8859-1, there's no really
>good reason to use a Reader. Just get the stream and allow
>Properties.load to read from it.
>
>ServletContext.getResourceAsStream always loads things relative to the
>context root (and can look in the WAR file, etc.), so Chuck's example of
>"WEB-INF/myConfig.properties" will load a file found in
>/your/path/to/deploy/your-webapp/WEB-ING/myConfig.properties".
>  
>
Yes, that's what I figured out by experimenting, but again, thanks for 
confirming that I interpreted things correctly.

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave,

David Kerber wrote:
> This is a followup to my question about startup parameters.  After
> digging around a bit, it looks like my best bet is to use the Properties
> class to read my settings from a disk file at startup.

That's generally how things are done.

Chuck's note about using ServletContext.getResourceAsStream is a good
one. Since Properties files are always in ISO-8859-1, there's no really
good reason to use a Reader. Just get the stream and allow
Properties.load to read from it.

ServletContext.getResourceAsStream always loads things relative to the
context root (and can look in the WAR file, etc.), so Chuck's example of
"WEB-INF/myConfig.properties" will load a file found in
/your/path/to/deploy/your-webapp/WEB-ING/myConfig.properties".

Hope that helps,
- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFulNL9CaO5/Lv0PARAp2qAJ4lilmNoyqLa5RZ9fEaUTZMlk//BQCdFeoy
S1zp75YBbtbIOquy9MyLUFY=
=xatx
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by Tim Funk <fu...@joedog.org>.
My preference is JNDI (<env-entry> ) for such things.

Then in web.xml - you can create a default entry in web.xml"
<env-entry>
   <env-entry-name>configFile</env-entry-name>
   <env-entry-value>/etc/stuff</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

Then it can be overridden in your context config:
<Context ...>
   <Environment name="configFile" value="/opt/config/etc/stuff"
          type="java.lang.String" override="false"/>
</Context>


Then looked up in java via:
***
import javax.naming.Context;
import javax.naming.InitialContext;

InitialContext initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
String myConfigFile = (String) envCtx.lookup("configFile");
***

More details:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html

Individual parameters can be set this way too. I don't like system 
properties since its global to the JVM and not local to the webapp. If 
you have a library which needs a different config (or version) which 
needs to live in a different webapp that relies on a system property - 
then you end up in trouble.

-Tim

David Kerber wrote:
> This is a followup to my question about startup parameters.  After 
> digging around a bit, it looks like my best bet is to use the Properties 
> class to read my settings from a disk file at startup.  The properties 
> api appears to be easy to use, and works very similarly to the 
> Preferences class.  My question is how to locate the file without 
> hard-coding its location.  I have looked at several possibilities, and 
> can't work out the details on any of them even after a bunch of 
> googling.  :-(
> 
> 1.  Pass the location of the file as a JVM -D argument at app startup.  
> But I can't figure how how to pick that value up inside my app.
> 
> 2.  Put the config file somewhere in the tree of my webapp, such at 
> docbase or in conf.  But again, I can't figure out how to pick that 
> location in a form I can use to read the file in with a FileReader object.
> 
> 3.  ???
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by David Kerber <dc...@verizon.net>.
David Kerber wrote:

> Caldarale, Charles R wrote:
>
>>> From: David Kerber [mailto:dckerber@verizon.net] Subject: Re: How to 
>>> locate a configuration file on disk at runtime
>>>
>>>    
>>>
>>>> 2.  Put the config file somewhere in the tree of my webapp, such at 
>>>> docbase or in conf.  But again, I can't figure out how to pick that 
>>>> location in a form I can use to read the file in with a FileReader 
>>>> object.
>>>>       
>>>
>>> I'd still like to figure this one out, though.
>>>     
>>
>>
>> Any particular reason it must be a FileReader?  If it can be any flavor
>> of InputStream, then ServletContext.getResourceAsStream(String path)
>> should work.  The path can be something like
>> "WEB-INF/myConfig.properties" or wherever else you'd like.
>>   
>
> IIRC, it can be any form of InputStream, so I'll give that a try.  
> Thanks!
>
> Dave 

This works, so now I have a couple of options; thanks Chuck!

D



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by David Kerber <dc...@verizon.net>.
Caldarale, Charles R wrote:
>> From: David Kerber [mailto:dckerber@verizon.net] 
>> Subject: Re: How to locate a configuration file on disk at runtime
>>
>>     
>>> 2.  Put the config file somewhere in the tree of my webapp, such at 
>>> docbase or in conf.  But again, I can't figure out how to pick that 
>>> location in a form I can use to read the file in with a FileReader 
>>> object.
>>>       
>> I'd still like to figure this one out, though.
>>     
>
> Any particular reason it must be a FileReader?  If it can be any flavor
> of InputStream, then ServletContext.getResourceAsStream(String path)
> should work.  The path can be something like
> "WEB-INF/myConfig.properties" or wherever else you'd like.
>   
IIRC, it can be any form of InputStream, so I'll give that a try.  Thanks!

Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: How to locate a configuration file on disk at runtime

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: David Kerber [mailto:dckerber@verizon.net] 
> Subject: Re: How to locate a configuration file on disk at runtime
> 
> > 2.  Put the config file somewhere in the tree of my webapp, such at 
> > docbase or in conf.  But again, I can't figure out how to pick that 
> > location in a form I can use to read the file in with a FileReader 
> > object.
> I'd still like to figure this one out, though.

Any particular reason it must be a FileReader?  If it can be any flavor
of InputStream, then ServletContext.getResourceAsStream(String path)
should work.  The path can be something like
"WEB-INF/myConfig.properties" or wherever else you'd like.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to locate a configuration file on disk at runtime

Posted by David Kerber <dc...@verizon.net>.
David Kerber wrote:
> This is a followup to my question about startup parameters.  After 
> digging around a bit, it looks like my best bet is to use the 
> Properties class to read my settings from a disk file at startup.  The 
> properties api appears to be easy to use, and works very similarly to 
> the Preferences class.  My question is how to locate the file without 
> hard-coding its location.  I have looked at several possibilities, and 
> can't work out the details on any of them even after a bunch of 
> googling.  :-(
>
> 1.  Pass the location of the file as a JVM -D argument at app 
> startup.  But I can't figure how how to pick that value up inside my app.
Figured this one out, once I finally hit on the right search terms.  
It's System.getProperty( "my.property.name", "DefaultValue"), where the 
JVM argument is -Dmy.property.name=MyPropertyValue.


>
> 2.  Put the config file somewhere in the tree of my webapp, such at 
> docbase or in conf.  But again, I can't figure out how to pick that 
> location in a form I can use to read the file in with a FileReader 
> object.
I'd still like to figure this one out, though.

>
> 3.  ???
>
> Any suggestions greatly appreciated!
>
> Dave



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org