You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "srd.pl" <sr...@yahoo.com> on 2010/12/04 12:58:25 UTC

Place .ini conf file inside the war package.

Hello,

I have a quick question considering my rest webservice. I would like to
place an .ini file with configuration parameters inside an war file, so that
I can change them wile the app is deployed on tomcat without recompiling.
Can any give an advice on how to do this? And how to open this file (what
path) in my java code. I am using maven so an advice on how to write a
special script in the pom.xml file would be great. 


-- 
View this message in context: http://old.nabble.com/Place-.ini-conf-file-inside-the-war-package.-tp30366628p30366628.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Place .ini conf file inside the war package.

Posted by Brian Braun <br...@gmail.com>.
http://commons.apache.org/configuration/ ?

On Sat, Dec 4, 2010 at 6:58 AM, srd.pl <sr...@yahoo.com> wrote:

>
> Hello,
>
> I have a quick question considering my rest webservice. I would like to
> place an .ini file with configuration parameters inside an war file, so
> that
> I can change them wile the app is deployed on tomcat without recompiling.
> Can any give an advice on how to do this? And how to open this file (what
> path) in my java code. I am using maven so an advice on how to write a
> special script in the pom.xml file would be great.
>
>
> --
> View this message in context:
> http://old.nabble.com/Place-.ini-conf-file-inside-the-war-package.-tp30366628p30366628.html
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Place .ini conf file inside the war package.

Posted by Rainer Jung <ra...@kippdata.de>.
On 04.12.2010 23:07, Pid wrote:
> On 12/4/10 5:41 PM, Mark Eggers wrote:
>> Read about using properties files. They're typically called<name>.properties.
>>
>> Snippet of code that can be placed in a ServletContextListener.
>>
>> String resource = "some.properties";
>> InputStream in = this.getClass().getClassLoader().getResourceAsStream(resource);
>> try {
>>       props.load(in);
>>       in.close();
>>      } catch (IOException ex) {
>>            ex.printStackTrace(); // ugly
>>      }
>>
>> Note that this is not a complete ServletContextListener. Normally I create a
>> separate class, have that class's constructor read the properties file, and then
>> use the ServletContextListener to create a servlet context attribute with the
>> information.
>
> You can use: ServletContext.getResourceAsStream()
>
> Note: once loaded you'll have to organise reloading yourself - the
> properties don't update just because you change the file on disk.

That's why he was pointed at commons-configuration. It already 
implements the ability to change configuration during runtime.

Regards,

Rainer

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


Re: Place .ini conf file inside the war package.

Posted by "srd.pl" <sr...@yahoo.com>.
Thank's guys. I am now loading the properties file, but in order to provide
changes in the file i need to restart the webservice on tomcat. I'll at the
appach commons closer for that. 
-- 
View this message in context: http://old.nabble.com/Place-.ini-conf-file-inside-the-war-package.-tp30366628p30394403.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: Place .ini conf file inside the war package.

Posted by Pid * <pi...@pidster.com>.
On 5 Dec 2010, at 19:20, Christopher Schultz
<ch...@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Pid,
>
> On 12/4/2010 5:07 PM, Pid wrote:
>> On 12/4/10 5:41 PM, Mark Eggers wrote:
>>> Read about using properties files. They're typically called <name>.properties.
>>>
>>> Snippet of code that can be placed in a ServletContextListener.
>>>
>>> String resource = "some.properties";
>>> InputStream in = this.getClass().getClassLoader().getResourceAsStream(resource);
>>> try {
>>>     props.load(in);
>>>     in.close();
>>>    } catch (IOException ex) {
>>>          ex.printStackTrace(); // ugly
>>>    }
>>>
>>> Note that this is not a complete ServletContextListener. Normally I create a
>>> separate class, have that class's constructor read the properties file, and then
>>> use the ServletContextListener to create a servlet context attribute with the
>>> information.
>>
>> You can use: ServletContext.getResourceAsStream()
>
> It's probably better to use ServletContext.getResource() (returns a URL)
> because then you might be able check it's last modified date.

I didn't know commons conf did runtime changes.  Fair point about URL.


p


>
> I haven't tried it, but a JarURLConnection might be nice enough to
> properly implement the getLastModifiedDate method.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkz75cMACgkQ9CaO5/Lv0PDfcgCeL1INqRVJTR/DrfqtbT4pJfXL
> VQUAoJ1PXqDjDpVL8NiTz+079TeoNtL3
> =f5Cd
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

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


Re: Place .ini conf file inside the war package.

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

Pid,

On 12/4/2010 5:07 PM, Pid wrote:
> On 12/4/10 5:41 PM, Mark Eggers wrote:
>> Read about using properties files. They're typically called <name>.properties.
>>
>> Snippet of code that can be placed in a ServletContextListener.
>>
>> String resource = "some.properties";
>> InputStream in = this.getClass().getClassLoader().getResourceAsStream(resource);
>> try {
>>      props.load(in);
>>      in.close();
>>     } catch (IOException ex) {
>>           ex.printStackTrace(); // ugly
>>     }
>>
>> Note that this is not a complete ServletContextListener. Normally I create a 
>> separate class, have that class's constructor read the properties file, and then 
>> use the ServletContextListener to create a servlet context attribute with the 
>> information.
> 
> You can use: ServletContext.getResourceAsStream()

It's probably better to use ServletContext.getResource() (returns a URL)
because then you might be able check it's last modified date.

I haven't tried it, but a JarURLConnection might be nice enough to
properly implement the getLastModifiedDate method.

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

iEYEARECAAYFAkz75cMACgkQ9CaO5/Lv0PDfcgCeL1INqRVJTR/DrfqtbT4pJfXL
VQUAoJ1PXqDjDpVL8NiTz+079TeoNtL3
=f5Cd
-----END PGP SIGNATURE-----

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


Re: Place .ini conf file inside the war package.

Posted by Pid <pi...@pidster.com>.
On 12/4/10 5:41 PM, Mark Eggers wrote:
> Read about using properties files. They're typically called <name>.properties.
> 
> Snippet of code that can be placed in a ServletContextListener.
> 
> String resource = "some.properties";
> InputStream in = this.getClass().getClassLoader().getResourceAsStream(resource);
> try {
>      props.load(in);
>      in.close();
>     } catch (IOException ex) {
>           ex.printStackTrace(); // ugly
>     }
> 
> Note that this is not a complete ServletContextListener. Normally I create a 
> separate class, have that class's constructor read the properties file, and then 
> use the ServletContextListener to create a servlet context attribute with the 
> information.

You can use: ServletContext.getResourceAsStream()

Note: once loaded you'll have to organise reloading yourself - the
properties don't update just because you change the file on disk.


p

> You can use an init parameter to read the resource name from web.xml, and then 
> pass that information to your class as an argument to the constructor. Catch 
> exceptions (such as file not found) appropriately.
> 
> The Apache commons configuration (mentioned in another message) provides a lot 
> of nice tools for reading properties files.
> 
> Place the some.properties file in src/main/resources of your Maven project. This 
> will get it packed up in WEB-INF/classes of your war file once you run maven 
> package.
> 
> Read the javadoc on Class.getResourceAsStream() concerning how to locate the 
> properties file.
> 
> . . . . just my two cents.
> 
> /mde/
> 
> ----- Original Message ----
> From: srd.pl <sr...@yahoo.com>
> To: users@tomcat.apache.org
> Sent: Sat, December 4, 2010 3:58:25 AM
> Subject: Place .ini conf file inside the war package.
> 
> 
> Hello,
> 
> I have a quick question considering my rest webservice. I would like to
> place an .ini file with configuration parameters inside an war file, so that
> I can change them wile the app is deployed on tomcat without recompiling.
> Can any give an advice on how to do this? And how to open this file (what
> path) in my java code. I am using maven so an advice on how to write a
> special script in the pom.xml file would be great. 
> 
> 


Re: Place .ini conf file inside the war package.

Posted by Mark Eggers <it...@yahoo.com>.
Read about using properties files. They're typically called <name>.properties.

Snippet of code that can be placed in a ServletContextListener.

String resource = "some.properties";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(resource);
try {
     props.load(in);
     in.close();
    } catch (IOException ex) {
          ex.printStackTrace(); // ugly
    }

Note that this is not a complete ServletContextListener. Normally I create a 
separate class, have that class's constructor read the properties file, and then 
use the ServletContextListener to create a servlet context attribute with the 
information.

You can use an init parameter to read the resource name from web.xml, and then 
pass that information to your class as an argument to the constructor. Catch 
exceptions (such as file not found) appropriately.

The Apache commons configuration (mentioned in another message) provides a lot 
of nice tools for reading properties files.

Place the some.properties file in src/main/resources of your Maven project. This 
will get it packed up in WEB-INF/classes of your war file once you run maven 
package.

Read the javadoc on Class.getResourceAsStream() concerning how to locate the 
properties file.

. . . . just my two cents.

/mde/

----- Original Message ----
From: srd.pl <sr...@yahoo.com>
To: users@tomcat.apache.org
Sent: Sat, December 4, 2010 3:58:25 AM
Subject: Place .ini conf file inside the war package.


Hello,

I have a quick question considering my rest webservice. I would like to
place an .ini file with configuration parameters inside an war file, so that
I can change them wile the app is deployed on tomcat without recompiling.
Can any give an advice on how to do this? And how to open this file (what
path) in my java code. I am using maven so an advice on how to write a
special script in the pom.xml file would be great. 


-- 
View this message in context: 
http://old.nabble.com/Place-.ini-conf-file-inside-the-war-package.-tp30366628p30366628.html

Sent from the Tomcat - User mailing list archive at Nabble.com.


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


      

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