Hello,
Im a bit confused about how to do the following:
I have a servlet myapp\hello.class
So in the WAR file it will have the following structure:
WEB-INF\classes\myapp\hello.class
Then I also have my own .properties file residing in
WEB-INF\classes\config\hello.properties
For reading the .properties file I use
ClassLoader loader = this.getClass().getClassLoader();
InputStream stream =
loader.getResourceAsStream("config/hello.properties");
Properties config = new Properties ();
config.load(stream);
This works fine, but when I deploy my WAR file to the application server
I want the .properties file outside the WAR file, so it makes it easier
to edit the file instead of rebuilding a new WAR file.
So my reading priority should be:
1. Check for hello.properties in shared location, classpath?
2. Check for hello.properties in WARFILE/config/
What is the best way of doing something like this, does java have
default support for this or do I have to write my own check meganism?
Does anybody have some sample code on how to do this, or is my view on
the matter not conform the java view on things, in other words is there
a better way?
Thanks in advance for any help :)
Cheers
Ido
Jacques Desmazieres - 15 Dec 2004 16:06 GMT
Hello,
I just faced this problem, and sorted it out.
The way I do this, is using an utility class, with the following method
(sorry for comments in french):
public static final InputStream getInputStreamFileSystemFirst(String
fileName) throws IOException
{
InputStream is = null;
URL url = null;
String filePath = PathTools.getPath(fileName);
//recherche sur le file system
try
{
url = new File(fileName).toURL();
is = url.openStream();
return is;
}
catch (MalformedURLException ignore)
{
}
catch (IOException ignore)
{
// le fichier n'est pas trouv? sur le file system
}
//recherche sur le file system (I don't remember what's the difference
with the previous method)
try
{
is = new FileInputStream(filePath);
return is;
}
catch (IOException ignore)
{
// le fichier n'est pas trouv? sur le file system
}
//recherche dans le classpath (this is the "right" way to retrieve the
classloader within a j2ee container)
url =
Thread.currentThread().getContextClassLoader().getResource(fileName);
if (url != null)
{
try
{
return url.openStream();
}
catch (IOException ioe)
{
// on ne fait rien car l'exception est lev?e juste apr?s,
syst?matiquement
}
}
throw new IOException("Impossible de trouver \"" + fileName + "\"");
}
Jacques Desmazieres
> Hello,
>
[quoted text clipped - 34 lines]
>
> Ido
Ido de Lepper - 17 Dec 2004 08:04 GMT
Thanks for your reply,
This methode I dont know PathTools, but can guess what it does :)
But what format can your filename be? I will have to look into your
code a bit closer. Thanks for the quick reaction!
Ido
> Hello,
>
[quoted text clipped - 58 lines]
>
> Jacques Desmazieres