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 !
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.