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