You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chris Ward <cw...@horizon-asset.co.uk> on 2006/05/03 19:17:38 UTC

Tomcat caching proprties file loaded with : myClass.getResourceAsStream( "props.properties")

Hi all,

I've build a little util class for sucking in properties.  When I use it
from "command line" progs it seems to work okay, but when I call it from
a servlet it never seems to load an *updated*  version of the
.properties file.

I am using a HashMap to cache the Properties and associated timestamp
(of type long).  It detects a stale timestamp okay - then goes through
the Properties.load( ... ) but the returned Properties seem to always be
the ones it first loaded.

If I restaart Tomcat the newest version of the Properties file *is*
loaded.


Here's some of the util  code...

<snip>


    static private Properties loadPropertiesFile(Class callerClass,
                                                 String configName) {
        
        Properties appProperties = null;

		...
		if detect "stale" timestamp on disk property file 
		vs. the cache one, set "entry = null" - this works...
		...


                if (entry == null) {
                    // create and load properties 
                    appProperties = new Properties();
                    appProperties.load(callerClass.getResourceAsStream(
                        configName + ".properties"));
            
                    // stuff it in the cache
                    entry = new MapEntry(lastModified, appProperties);
                    propertiesCache.put(appName, entry);
                    
                    appProperties.list(System.out);
                    
                }

		...
		...


<snip>

And here's how I test it from a servlet...


<snip>


    public void doGet(HttpServletRequest req, HttpServletResponse res)
throws
        ServletException, IOException {
        PrintWriter out = res.getWriter();

        try {
            Properties prop = null;

            
            prop =
HAL_PropertiesUtils.getPropertiesHandler(this.getClass(),
"PropertiesUtilsServlet");
            out.println(prop.getProperty("name"));
        }
        catch (Exception ex) {
        }
    }

<snip>



Any wisdom on this?  Do I have to do something different/extra when
loading properties in this way under Tomcat?

I just do all this so the .properties files can be found in the same dir
as whatever class calls the util.

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


Re: Tomcat caching proprties file loaded with : myClass.getResourceAsStream( "props.properties")

Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
if you want to be able to reload it, I suggest that you load it using a 
FileInputstream, and not getResourceAsStream from the class loader.
if you load it through the class loader, the class loader doesn't reload 
it until restart.

Filip

Chris Ward wrote:
> Hi all,
>
> I've build a little util class for sucking in properties.  When I use it
> from "command line" progs it seems to work okay, but when I call it from
> a servlet it never seems to load an *updated*  version of the
> .properties file.
>
> I am using a HashMap to cache the Properties and associated timestamp
> (of type long).  It detects a stale timestamp okay - then goes through
> the Properties.load( ... ) but the returned Properties seem to always be
> the ones it first loaded.
>
> If I restaart Tomcat the newest version of the Properties file *is*
> loaded.
>
>
> Here's some of the util  code...
>
> <snip>
>
>
>     static private Properties loadPropertiesFile(Class callerClass,
>                                                  String configName) {
>         
>         Properties appProperties = null;
>
> 		...
> 		if detect "stale" timestamp on disk property file 
> 		vs. the cache one, set "entry = null" - this works...
> 		...
>
>
>                 if (entry == null) {
>                     // create and load properties 
>                     appProperties = new Properties();
>                     appProperties.load(callerClass.getResourceAsStream(
>                         configName + ".properties"));
>             
>                     // stuff it in the cache
>                     entry = new MapEntry(lastModified, appProperties);
>                     propertiesCache.put(appName, entry);
>                     
>                     appProperties.list(System.out);
>                     
>                 }
>
> 		...
> 		...
>
>
> <snip>
>
> And here's how I test it from a servlet...
>
>
> <snip>
>
>
>     public void doGet(HttpServletRequest req, HttpServletResponse res)
> throws
>         ServletException, IOException {
>         PrintWriter out = res.getWriter();
>
>         try {
>             Properties prop = null;
>
>             
>             prop =
> HAL_PropertiesUtils.getPropertiesHandler(this.getClass(),
> "PropertiesUtilsServlet");
>             out.println(prop.getProperty("name"));
>         }
>         catch (Exception ex) {
>         }
>     }
>
> <snip>
>
>
>
> Any wisdom on this?  Do I have to do something different/extra when
> loading properties in this way under Tomcat?
>
> I just do all this so the .properties files can be found in the same dir
> as whatever class calls the util.
>
> ---------------------------------------------------------------------
> 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