Quantcast

Location of properties file for web app in Tomcat

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Location of properties file for web app in Tomcat

Flavius

Greetings,

I've spent some time searching online and reading other threads here, such as
http://www.nabble.com/where-to-put-config-file-in-a-webapp-td16448528.html#a16450373.

I have a question that wasn't quite answered there though.

I have a web app that needs to have property information set, such
as database IP, login, email server IP, login, etc.  Typically I would
put this in the WEB-INF/classes dir as a properties file.

My concern is this ends up being inside the war file distributed.  So
a user would have to expand the war to get at the properties file
and edit it.  If they configure tomcat to run the war without expanding
it, they now have to un jar it, edit the properties file, and jar it up
again.  I really don't want a consumer messing with the war at all,
or even going into the expanded dir.

I thought of just putting the properties file in the webapps dir, beside
the war itself and reading it with ClassLoader.getResourceAsStream();

1.  I don't see anybody else do this, so that suggests this is not the
best place to put the properties file.
2.  I was having some issues reading the properties file outside the war
file.

So my questions are:
1. Where is the best place to put the properties file
if I want it located outside the war itself?  

2. I can read it using ClassLoader.getResourceAsStream() or maybe
ClassLoader.getSystemClassLoader()?

I realize a consequence to this is if they run more than once instance
of the app on different urls, they would both read the same config file.  
That's a corner case I don't think we'll ever run into, although if we did,
I could probably just have it read the app's configured uri as part of the
properties file (uri1.properties, uri2.properties, etc).

My ultimate goal is to give these instructions to a network admin:

1.  Download and install Tomcat
2.  Place war file in <TomcatDir>/webapps
3.  Put the app.properties file at __________ and set your database and
email server info.
4.  Start tomcat

Thanks very much!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Location of properties file for web app in Tomcat

Caldarale, Charles R
> From: Flavius [mailto:[hidden email]]
> Subject: Location of properties file for web app in Tomcat
>
> 1. Where is the best place to put the properties file
> if I want it located outside the war itself?

I don't think there's a "best place" that would be applicable to all environments.  One mechanism that is known to work is to have the admin specify a system property at Tomcat startup indicating where the property file is located; the webapp looks at the system property during initialization and reads its properties from the specified location.  The name of the system property can be whatever you choose, and could even include the context path of the webapp to allow multiple instances to be deployed in one container.

 - 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 unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

Keshav Prasad
I think what Chuck mentioned is a nice approach. You could set an env
variable and read it off when app server initializes something like this-

String path = System.getenv("PROP_FILE_PATH");

Use this file path to read the properties file.

On Sat, Jan 31, 2009 at 11:19 AM, Caldarale, Charles R <
[hidden email]> wrote:

> > From: Flavius [mailto:[hidden email]]
> > Subject: Location of properties file for web app in Tomcat
> >
> > 1. Where is the best place to put the properties file
> > if I want it located outside the war itself?
>
> I don't think there's a "best place" that would be applicable to all
> environments.  One mechanism that is known to work is to have the admin
> specify a system property at Tomcat startup indicating where the property
> file is located; the webapp looks at the system property during
> initialization and reads its properties from the specified location.  The
> name of the system property can be whatever you choose, and could even
> include the context path of the webapp to allow multiple instances to be
> deployed in one container.
>
>  - 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 unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

Kirk True
In reply to this post by Caldarale, Charles R
Hi all,

Caldarale, Charles R wrote:

>> From: Flavius [mailto:[hidden email]]
>> Subject: Location of properties file for web app in Tomcat
>>
>> 1. Where is the best place to put the properties file
>> if I want it located outside the war itself?
>>    
>
> I don't think there's a "best place" that would be applicable to all environments.  One mechanism that is known to work is to have the admin specify a system property at Tomcat startup indicating where the property file is located; the webapp looks at the system property during initialization and reads its properties from the specified location.  The name of the system property can be whatever you choose, and could even include the context path of the webapp to allow multiple instances to be deployed in one container.
>
>
>  

+1

I had the same issue as the OP and the best I could come up with was to
change catalina.sh to include a system property that was then read by
the web app. But we did fall into the same problem of multiple instances
and we just made the system property that pointed to the file more
qualified.

Kirk
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

Flavius

Thanks guys.

I think I'll try that.  I had hoped to get around setting the system property
by simply requiring that the properties file exist in the same dir as the
war file.  Then the war file can simply ask, "where am I?" and use that
path to load the properties file.

It seems the ContextClassLoader should know where it's running from
on the physical path.  But I haven't figured that out yet.

Have a great day.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Location of properties file for web app in Tomcat

Caldarale, Charles R
> From: Flavius [mailto:[hidden email]]
> Subject: Re: Location of properties file for web app in Tomcat
>
> It seems the ContextClassLoader should know where it's running from
> on the physical path.  But I haven't figured that out yet.

As has been discussed on this list several times, the container (Tomcat) need not expose any of the file system to the webapps other a spec-defined workspace area.  You can't even be sure that your webapp resources will ever be in the underlying file system, even if there is one (think embedded controller).

 - 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 unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

Christopher Schultz-2
In reply to this post by Flavius
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Flavius,

Flavius wrote:
> My ultimate goal is to give these instructions to a network admin:
>
> 1.  Download and install Tomcat
> 2.  Place war file in <TomcatDir>/webapps
> 3.  Put the app.properties file at __________ and set your database and
> email server info.
> 4.  Start tomcat

Something even cooler would be to provide a simple text editor that
would load your properties file from the WAR and write it back into the
WAR when you saved it. Then you get the best of both worlds.

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

iEYEARECAAYFAkmLZg0ACgkQ9CaO5/Lv0PDzqQCfW6JwuP3UxbsEy1Hj1zHCPD6j
KacAoIRpTWwUo0hyTucngttCOgVEwVbc
=IalH
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

Flavio Crispim-2
Christopher Schultz wrote:

> Flavius,
>
> Flavius wrote:
>  
>> My ultimate goal is to give these instructions to a network admin:
>>
>> 1.  Download and install Tomcat
>> 2.  Place war file in <TomcatDir>/webapps
>> 3.  Put the app.properties file at __________ and set your database and
>> email server info.
>> 4.  Start tomcat
>>    
>
> Something even cooler would be to provide a simple text editor that
> would load your properties file from the WAR and write it back into the
> WAR when you saved it. Then you get the best of both worlds.
>
> - -chris
Just to mention, there are cases when a JNDI would do the trick and
sometimes (not always) like a system property would do.

regards

Flavio



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

Flavio Crispim-2
In reply to this post by Flavius
Christopher Schultz wrote:

Flavius,

Flavius wrote:

 My ultimate goal is to give these instructions to a network admin:

1.  Download and install Tomcat
2.  Place war file in <TomcatDir>/webapps
3.  Put the app.properties file at __________ and set your database and
email server info.
4.  Start tomcat


Something even cooler would be to provide a simple text editor that
would load your properties file from the WAR and write it back into the
WAR when you saved it. Then you get the best of both worlds.

- -chris

 Just to mention, there are cases when a JNDI would do the trick and
sometimes (not always) like a system property would do.

regards

Flavio
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

hbhartee
In reply to this post by Flavius
Hi,

There is a simple way of using properties file across different web applications in Tomcat.

> Jar the properties file (included in package folders) and drop it in jakarta-tomcat/common/lib directory
> If you do not want to jar, just drop the properties file (included in relevant package structure folders) to jakarta-tomcat/common/classes

The properties file would be available to all web applications after Tomcat is restarted.

R,
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Location of properties file for web app in Tomcat

ziggy25
I had the same problem before. I didnt want to configure the location of the
properties file at the tomcat instance level since i had more than one war
file. What i ended up doing is add the properties file configuration on the
application's context file. This meant each war file can point to its own
properties file.

Here is what i do. I add the following line in my META-INF/context.xml file.
(The path can be anywhere on the system)

<Environment name="APP_PROPERTIES"
        description="The APP Properties File" override="false"
        type="java.lang.String"
        value="/u/misc/web/conf/app.properties" />


Then in my code i just access the path using the following code

System.getenv("APP_PROPERTIES");


Note: On a windows environment just change the path to the windows
equivalent


Thanks
HTH






On Wed, Nov 18, 2009 at 2:20 PM, hbhartee <[hidden email]>wrote:

>
> Hi,
>
> There is a simple way of using properties file across different web
> applications in Tomcat.
>
> > Jar the properties file (included in package folders) and drop it in
> > jakarta-tomcat/common/lib directory
> > If you do not want to jar, just drop the properties file (included in
> > relevant package structure folders) to jakarta-tomcat/common/classes
>
> The properties file would be available to all web applications after Tomcat
> is restarted.
>
> R,
> --
> View this message in context:
> http://old.nabble.com/Location-of-properties-file-for-web-app-in-Tomcat-tp21767095p26408179.html
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Loading...