Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a properties file that resides inside a WAR 1

Status
Not open for further replies.

pgk

Programmer
Jul 19, 2002
262
US
Hi

I have written an API and bundled it as a jar file. This api makes use of two configurable properties file. Since this is an API, the 2 files reside outside the jar. To read these 2 files, I use the following code snippet:

String _CLASSPATH = System.getProperty("java.class.path");
StringTokenizer st = new StringTokenizer(CLASSPATH, ":");
while(l_StringTokenizer.hasMoreTokens()){
String l_path = l_StringTokenizer.nextToken();
String l_filePath = null;
File l_file = new File(l_path);
boolean l_isDirectory = l_file.isDirectory();
if(l_isDirectory){
System.out.println("PATH retrieved : " + l_path);
if( (l_path.substring(l_path.length() - 1, l_path.length())).equals("/") ) {
l_filePath = l_path + aPropFileName;
System.out.println("No / added to path");
}
else
{
l_filePath = l_path + "/" + aPropFileName;
System.out.println("/ added to path");
}
System.out.println("Final path : " + l_filePath);
File l_propFile = new File(l_filePath);
if(l_propFile.exists()) {
try {
prop.load(new FileInputStream(l_filePath));
l_fileLoaded = true;
System.out.println("File loaded successfully");
break;
}
catch(FileNotFoundException aFileNotFoundException)
{
System.out.println("FileNotFoundException exception :" + aFileNotFoundException);
throw aFileNotFoundException;
}
catch (IOException aIOException)
{
System.out.println("IO exception :" + aIOException);
throw aIOException;
}
}
}
}
if(l_fileLoaded == false){
throw new ApiPropertyFileNotFoundException("The Api general properties file " + aPropFileName + " was not found");
}


The problem with this code is that it is very system specific. If BEA changes the CLASSPATH attribute from java.class.path, then my api will fail.

Now the problem is: the application that will use this api is deployed as WAR file. So the properties file will also be bundled into this WAR. I would like to know:
1) how to locate a resource dynamically ( irrespective of whether it is a web server context or stand alone app )
2) how to read the contents of a WAR file ( my property file alone is located inside the WAR file of another app )
3) how to read the contents of a WAR file ( my property file and the api jar are located inside the WAR file of another app )

Before writing the current code for searching and reading a file, I tried using
InputStream l_is = (InputStream) (new Object()).getResourceAsStream(fileName);

This worked well in the stand alone app but failed in the WL environment.

Please help and sorry for the longgg post. Thanks in advance


Hope it helps. Let me know what happens.
With regards,
PGK
 
Answer to your Question 1 would be to use java.util.ResouorceBundle. Using ResourceBundle properties can be obtains as long as the property file is within the class path of the application.

I don't think it is a good idea to load properties file from a WAR file of a different application. This add dependency between the two, mind as well just combine the relevent modules in one application/package (jar).

Anyway, a WAR file is just a jar file with a different extension. You can treat the WAR file as a jar file, and use java.util.jar.* package to read file in a WAR file.
 
Hi,

Should not (new Object()).getClass().getResourceAsStream(propfilename) do the same as ResourceBundle? When I tested in a stand alone application, the getResource() method worked, provided the class path contained the exact directory in which the property file was located. However, it failed miserably in the WL environment evn though the class path was set properly. Any idea as to why this could have happened?

Thanks.

Hope it helps. Let me know what happens.
With regards,
PGK
 
(new Object()).getClass().getResourceAsStream(propfilename)using ClassLoader to locate the resource. My guess is under WL enviroment, a different ClassLoader rather than a default Java ClassLoader is being use, and it may have implemented getResourceAsStream() in a slightly different way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top