Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

having your own .properties file outside the WAR file, how?

Thread view: 
Ido de Lepper - 15 Dec 2004 12:13 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.