Albataur
Albataur

Reputation: 97

How to reload properties file without rebooting Tomcat

I load a property file from classpath with the method :

    String cheminFichier = new StringBuilder(100).append(classeBP.getPackage().getName().replace(".", "/")).append(File.separator).append(
        REPERTOIRE_MAPPING).append(nomFichier).append(".properties").toString();
    InputStream isMapping = Thread.currentThread().getContextClassLoader().getResourceAsStream(cheminFichier.toString());
    if (isMapping == null)
    {
        throw new ServiceMappingException("Erreur lors du chargement du mapping du service. Le fichier "
            + cheminFichier + " n'existe pas.");
    }
    else
    {
        Properties mapping = new Properties();
        try
        {
            mapping.load(isMapping);
        }
        catch (IOException e)
        ...
    }

Ok, it's work. But if I modify the content of the property file when Tomcat is running, changes are ignored. It's not hot-reloaded as with classes.

My context is configured with reloadable="true" option and the classloader returned by Thread.currentThread().getContextClassLoader() is the WEBAPP classloader (not the system classloader or other).

I read it's possible to use ServletContext.getResourceAsStream, but I haven't access to the servlet context.

It's Tomcat 5.5.

Any idea ? If not, do you have a solution for forcing to reload a specific resource (my property file) ?

Thanks !

Upvotes: 3

Views: 17475

Answers (3)

Stephen C
Stephen C

Reputation: 719576

Actually, your webapp does have access to the ServletContext at various points. For example you can get it using ServletRequest.getServletContext(), or from a ServletContextEvent obtained via a ServletContextListener callback.


This is a bit off topic, but you should realise that this code:

String cheminFichier = new StringBuilder(100).append(classeBP.getPackage().getName().replace(".", "/")).append(File.separator).append(
    REPERTOIRE_MAPPING).append(nomFichier).append(".properties").toString();

could / should be written as:

String cheminFichier = classeBP.getPackage().getName().replace(".", "/")) +
    File.separator + REPERTOIRE_MAPPING + nomFichier + ".properties";

There is no performance advantage here in explicitly using a StringBuilder here, because the Java compiler will compile the concatenation to the same code.

You only need to use a StringBuilder if the concatenation involves a loop.

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570595

You could use something like Commons Configuration that supports automatic reloading.

Upvotes: 5

Colin Hebert
Colin Hebert

Reputation: 93197

You can still use the Tomcat Manager to reload your application.

Upvotes: 2

Related Questions